Esempio n. 1
0
        internal Span StartSpan(IApmAgent agent, IDbCommand dbCommand, InstrumentationFlag instrumentationFlag, string subType = null,
                                bool captureStackTraceOnStart = false
                                )
        {
            var spanName = dbCommand.CommandText.Replace(Environment.NewLine, " ");

            return(ExecutionSegmentCommon.StartSpanOnCurrentExecutionSegment(agent, spanName, ApiConstants.TypeDb, subType, instrumentationFlag,
                                                                             captureStackTraceOnStart));
        }
Esempio n. 2
0
        internal ISpan StartSpan(IApmAgent agent, IDbCommand dbCommand, InstrumentationFlag instrumentationFlag, string subType = null,
                                 bool captureStackTraceOnStart = false
                                 )
        {
            var spanName = GetDbSpanName(dbCommand);

            return(ExecutionSegmentCommon.StartSpanOnCurrentExecutionSegment(agent, spanName, ApiConstants.TypeDb, subType, instrumentationFlag,
                                                                             captureStackTraceOnStart, true));
        }
Esempio n. 3
0
        public Span(
            string name,
            string type,
            string parentId,
            string traceId,
            Transaction enclosingTransaction,
            IPayloadSender payloadSender,
            IApmLogger logger,
            ICurrentExecutionSegmentsContainer currentExecutionSegmentsContainer,
            Span parentSpan = null,
            InstrumentationFlag instrumentationFlag = InstrumentationFlag.None,
            bool captureStackTraceOnStart           = false
            )
        {
            InstrumentationFlag = instrumentationFlag;
            Timestamp           = TimeUtils.TimestampNow();
            Id      = RandomGenerator.GenerateRandomBytesAsString(new byte[8]);
            _logger = logger?.Scoped($"{nameof(Span)}.{Id}");

            _payloadSender = payloadSender;
            _currentExecutionSegmentsContainer = currentExecutionSegmentsContainer;
            _parentSpan           = parentSpan;
            _enclosingTransaction = enclosingTransaction;
            Name = name;
            Type = type;

            ParentId = parentId;
            TraceId  = traceId;

            if (IsSampled)
            {
                // Started and dropped spans should be counted only for sampled transactions
                if (enclosingTransaction.SpanCount.IncrementTotal() > ConfigSnapshot.TransactionMaxSpans &&
                    ConfigSnapshot.TransactionMaxSpans >= 0)
                {
                    _isDropped = true;
                    enclosingTransaction.SpanCount.IncrementDropped();
                }
                else
                {
                    enclosingTransaction.SpanCount.IncrementStarted();

                    if (captureStackTraceOnStart)
                    {
                        _stackFrames = new EnhancedStackTrace(new StackTrace(true)).GetFrames();
                    }
                }
            }

            _currentExecutionSegmentsContainer.CurrentSpan = this;

            _logger.Trace()
            ?.Log("New Span instance created: {Span}. Start time: {Time} (as timestamp: {Timestamp}). Parent span: {Span}",
                  this, TimeUtils.FormatTimestampForLog(Timestamp), Timestamp, _parentSpan);
        }
Esempio n. 4
0
        internal Span StartSpanInternal(string name, string type, string subType = null, string action = null,
                                        InstrumentationFlag instrumentationFlag  = InstrumentationFlag.None, bool captureStackTraceOnStart = false
                                        )
        {
            var retVal = new Span(name, type, Id, TraceId, _enclosingTransaction, _payloadSender, _logger, _currentExecutionSegmentsContainer, this,
                                  instrumentationFlag, captureStackTraceOnStart);

            if (!string.IsNullOrEmpty(subType))
            {
                retVal.Subtype = subType;
            }

            if (!string.IsNullOrEmpty(action))
            {
                retVal.Action = action;
            }

            _logger.Trace()?.Log("Starting {SpanDetails}", retVal.ToString());
            return(retVal);
        }
Esempio n. 5
0
        public Span(
            string name,
            string type,
            string parentId,
            string traceId,
            Transaction enclosingTransaction,
            IPayloadSender payloadSender,
            IApmLogger logger,
            ICurrentExecutionSegmentsContainer currentExecutionSegmentsContainer,
            IApmServerInfo apmServerInfo,
            Span parentSpan = null,
            InstrumentationFlag instrumentationFlag = InstrumentationFlag.None,
            bool captureStackTraceOnStart           = false
            )
        {
            InstrumentationFlag = instrumentationFlag;
            Timestamp           = TimeUtils.TimestampNow();
            Id      = RandomGenerator.GenerateRandomBytesAsString(new byte[8]);
            _logger = logger?.Scoped($"{nameof(Span)}.{Id}");

            _payloadSender = payloadSender;
            _currentExecutionSegmentsContainer = currentExecutionSegmentsContainer;
            _parentSpan           = parentSpan;
            _enclosingTransaction = enclosingTransaction;
            _apmServerInfo        = apmServerInfo;
            Name = name;
            Type = type;

            ParentId = parentId;
            TraceId  = traceId;

            if (IsSampled)
            {
                SampleRate = enclosingTransaction.SampleRate;
                // Started and dropped spans should be counted only for sampled transactions
                if (enclosingTransaction.SpanCount.IncrementTotal() > ConfigSnapshot.TransactionMaxSpans &&
                    ConfigSnapshot.TransactionMaxSpans >= 0)
                {
                    _isDropped = true;
                    enclosingTransaction.SpanCount.IncrementDropped();
                }
                else
                {
                    enclosingTransaction.SpanCount.IncrementStarted();

                    // In some cases capturing the stacktrace in End() results in a stack trace which is not very useful.
                    // In such cases we capture the stacktrace on span start.
                    // These are typically async calls - e.g. capturing stacktrace for outgoing HTTP requests in the
                    // System.Net.Http.HttpRequestOut.Stop
                    // diagnostic source event produces a stack trace that does not contain the caller method in user code - therefore we
                    // capture the stacktrace is .Start
                    if (captureStackTraceOnStart && ConfigSnapshot.StackTraceLimit != 0 && ConfigSnapshot.SpanFramesMinDurationInMilliseconds != 0)
                    {
                        RawStackTrace = new StackTrace(true);
                    }
                }
            }
            else
            {
                SampleRate = 0;
            }

            _currentExecutionSegmentsContainer.CurrentSpan = this;

            _logger.Trace()
            ?.Log("New Span instance created: {Span}. Start time: {Time} (as timestamp: {Timestamp}). Parent span: {Span}",
                  this, TimeUtils.FormatTimestampForLog(Timestamp), Timestamp, _parentSpan);
        }
Esempio n. 6
0
 static DbSpanFactory()
 {
     _type = typeof(T);
     _instrumentationFlag = GetInstrumentationFlag(_type.FullName);
 }