public void OnNext(DiagnosticListener value)
        {
            var subscribedAny = false;


            if (_listener != null)
            {
                if (value.Name == _listener.Name)
                {
                    var listenerType = _listener.GetType();
                    if (_agent is null || _agent.SubscribedListeners.Add(listenerType) || _listener.AllowDuplicates)
                    {
                        _sourceSubscription = new SubscribedListenerDisposable(value.Subscribe(_listener), () => _agent.SubscribedListeners.Remove(listenerType));
                        _logger.Debug()
                        ?.Log("Subscribed {DiagnosticListenerType} to `{DiagnosticListenerName}' events source",
                              listenerType.FullName, value.Name);
                        subscribedAny = true;
                    }
                    else
                    {
                        _logger.Debug()?.Log("{DiagnosticListenerType} already subscribed to `{DiagnosticListenerName}' events source",
                                             listenerType.FullName, value.Name);
                    }
                }
            }
Exemplo n.º 2
0
        private void ProcessStartEvent(TRequest request, Uri requestUrl)
        {
            Logger.Trace()?.Log("Processing start event... Request URL: {RequestUrl}", Http.Sanitize(requestUrl));

            var transaction = _agent.Tracer.CurrentTransaction;

            if (transaction == null)
            {
                Logger.Debug()?.Log("No current transaction, skip creating span for outgoing HTTP request");
                return;
            }

            var span = ExecutionSegmentCommon.StartSpanOnCurrentExecutionSegment(_agent, $"{RequestGetMethod(request)} {requestUrl.Host}",
                                                                                 ApiConstants.TypeExternal, ApiConstants.SubtypeHttp, InstrumentationFlag.HttpClient, true);

            if (!ProcessingRequests.TryAdd(request, span))
            {
                // Consider improving error reporting - see https://github.com/elastic/apm-agent-dotnet/issues/280
                Logger.Error()?.Log("Failed to add to ProcessingRequests - ???");
                return;
            }

            if (!RequestHeadersContain(request, TraceContext.TraceParentHeaderName))
            {
                // We call TraceParent.BuildTraceparent explicitly instead of DistributedTracingData.SerializeToString because
                // in the future we might change DistributedTracingData.SerializeToString to use some other internal format
                // but here we want the string to be in W3C 'traceparent' header format.
                RequestHeadersAdd(request, TraceContext.TraceParentHeaderName, TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData));
            }

            if (transaction is Transaction t)
            {
                if (t.ConfigSnapshot.UseElasticTraceparentHeader)
                {
                    if (!RequestHeadersContain(request, TraceContext.TraceParentHeaderNamePrefixed))
                    {
                        RequestHeadersAdd(request, TraceContext.TraceParentHeaderNamePrefixed,
                                          TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData));
                    }
                }
            }

            if (!RequestHeadersContain(request, TraceContext.TraceStateHeaderName) && transaction.OutgoingDistributedTracingData.HasTraceState)
            {
                RequestHeadersAdd(request, TraceContext.TraceStateHeaderName,
                                  TraceContext.BuildTraceState(transaction.OutgoingDistributedTracingData));
            }

            if (!span.ShouldBeSentToApmServer)
            {
                return;
            }

            span.Context.Http = new Http {
                Method = RequestGetMethod(request)
            };
            span.Context.Http.SetUrl(requestUrl);
        }
        public void OnNext(DiagnosticListener value)
        {
            var subscribedAny = false;

            foreach (var listener in _listeners)
            {
                if (value.Name == listener.Name)
                {
                    _sourceSubscription.Add(value.Subscribe(listener));

                    _logger.Debug()
                    ?.Log("Subscribed {DiagnosticListenerType} to `{DiagnosticListenerName}' events source",
                          listener.GetType().FullName, value.Name);
                    subscribedAny = true;
                }
            }

            if (!subscribedAny)
            {
                _logger.Trace()
                ?.Log(
                    "There are no listeners in the current batch ({DiagnosticListeners}) that would like to subscribe to `{DiagnosticListenerName}' events source",
                    string.Join(", ", _listeners.Select(listener => listener.GetType().FullName)),
                    value.Name);
            }
        }
Exemplo n.º 4
0
        internal Transaction StartTransactionInternal(string name, string type, DistributedTracingData distributedTracingData = null)
        {
            var retVal = new Transaction(_logger, name, type, Sampler, distributedTracingData, _sender, _configurationReader,
                                         CurrentExecutionSegmentsContainer)
            {
                Service = _service
            };

            _logger.Debug()?.Log("Starting {TransactionValue}", retVal);
            return(retVal);
        }
Exemplo n.º 5
0
        internal Transaction StartTransactionInternal(string name, string type, DistributedTracingData distributedTracingData = null)
        {
            var retVal = new Transaction(_logger, name, type, Sampler, distributedTracingData, _sender)
            {
                Service = _service
            };

            Agent.TransactionContainer.Transactions.Value = retVal;
            _logger.Debug()?.Log("Starting {TransactionValue}", retVal);
            return(retVal);
        }
Exemplo n.º 6
0
        internal Transaction StartTransactionInternal(string name, string type, DistributedTracingData distributedTracingData = null, bool ignoreActivity = false)
        {
            var currentConfig = _configProvider.CurrentSnapshot;
            var retVal        = new Transaction(_logger, name, type, new Sampler(currentConfig.TransactionSampleRate), distributedTracingData
                                                , _sender, currentConfig, CurrentExecutionSegmentsContainer, ignoreActivity)
            {
                Service = _service
            };

            _logger.Debug()?.Log("Starting {TransactionValue}", retVal);
            return(retVal);
        }
Exemplo n.º 7
0
        internal Transaction StartTransactionInternal(string name, string type)
        {
            var retVal = new Transaction(_logger, name, type, _sender)
            {
                Name    = name,
                Type    = type,
                Service = _service
            };

            Agent.TransactionContainer.Transactions.Value = retVal;
            _logger.Debug()?.Log("Starting {TransactionValue}", retVal);
            return(retVal);
        }
Exemplo n.º 8
0
        private Transaction StartTransactionInternal(string name, string type, DistributedTracingData distributedTracingData = null,
                                                     bool ignoreActivity = false, long?timestamp = null, string id = null, string traceId = null
                                                     )
        {
            var currentConfig = _configurationProvider.CurrentSnapshot;
            var retVal        = new Transaction(_logger, name, type, new Sampler(currentConfig.TransactionSampleRate), distributedTracingData
                                                , _sender, currentConfig, CurrentExecutionSegmentsContainer, _apmServerInfo, _breakdownMetricsProvider, ignoreActivity, timestamp, id)
            {
                Service = _service
            };

            _logger.Debug()?.Log("Starting {TransactionValue}", retVal);
            return(retVal);
        }
        private void ProcessStartEvent(object eventValue, TRequest request, Uri requestUrl)
        {
            _logger.Trace()?.Log("Processing start event... Request URL: {RequestUrl}", requestUrl);
            if (Agent.TransactionContainer.Transactions == null || Agent.TransactionContainer.Transactions.Value == null)
            {
                _logger.Debug()?.Log("No active transaction, skip creating span for outgoing HTTP request");
                return;
            }

            var transaction = Agent.TransactionContainer.Transactions.Value;

            var span = transaction.StartSpanInternal(
                $"{RequestGetMethod(request)} {requestUrl.Host}",
                ApiConstants.TypeExternal,
                ApiConstants.SubtypeHttp);

            if (!ProcessingRequests.TryAdd(request, span))
            {
                // Sergey_Kleyman_TODO: Implement error handling
                _logger.Error()?.Log("Failed to add to ProcessingRequests - ???");
                return;
            }

            if (!RequestHeadersContain(request, TraceParent.TraceParentHeaderName))
            {
                // We call TraceParent.BuildTraceparent explicitly instead of DistributedTracingData.SerializeToString because
                // in the future we might change DistributedTracingData.SerializeToString to use some other internal format
                // but here we want the string to be in W3C 'traceparent' header format.
                RequestHeadersAdd(request, TraceParent.TraceParentHeaderName, TraceParent.BuildTraceparent(span.OutgoingDistributedTracingData));
            }

            if (transaction.IsSampled)
            {
                span.Context.Http = new Http
                {
                    Url    = requestUrl.ToString(),
                    Method = RequestGetMethod(request)
                };

                var frames      = new StackTrace(true).GetFrames();
                var stackFrames = StacktraceHelper.GenerateApmStackTrace(frames, _logger, span.Name);
                span.StackTrace = stackFrames;
            }
        }
        private void ProcessStartEvent(TRequest request, Uri requestUrl)
        {
            Logger.Trace()?.Log("Processing start event... Request URL: {RequestUrl}", requestUrl);

            var transaction = _agent.Tracer.CurrentTransaction;

            if (transaction == null)
            {
                Logger.Debug()?.Log("No current transaction, skip creating span for outgoing HTTP request");
                return;
            }

            var currentExecutionSegment = _agent.Tracer.CurrentSpan ?? (IExecutionSegment)transaction;
            var span = currentExecutionSegment.StartSpan(
                $"{RequestGetMethod(request)} {requestUrl.Host}",
                ApiConstants.TypeExternal,
                ApiConstants.SubtypeHttp);

            if (!ProcessingRequests.TryAdd(request, span))
            {
                // Consider improving error reporting - see https://github.com/elastic/apm-agent-dotnet/issues/280
                Logger.Error()?.Log("Failed to add to ProcessingRequests - ???");
                return;
            }

            if (!RequestHeadersContain(request, TraceParent.TraceParentHeaderName))
            {
                // We call TraceParent.BuildTraceparent explicitly instead of DistributedTracingData.SerializeToString because
                // in the future we might change DistributedTracingData.SerializeToString to use some other internal format
                // but here we want the string to be in W3C 'traceparent' header format.
                RequestHeadersAdd(request, TraceParent.TraceParentHeaderName, TraceParent.BuildTraceparent(span.OutgoingDistributedTracingData));
            }

            if (transaction.IsSampled)
            {
                span.Context.Http = new Http {
                    Url = requestUrl.ToString(), Method = RequestGetMethod(request)
                }
            }
            ;
        }