Example #1
0
        public void InsertDistributedTraceHeaders <T>(IInternalTransaction transaction, T carrier, Action <T, string, string> setter)
        {
            if (setter == null)
            {
                Log.Debug("setHeaders argument is null.");
                return;
            }

            try
            {
                var timestamp = DateTime.UtcNow;
                if (!_configurationService.Configuration.ExcludeNewrelicHeader)
                {
                    //set "Newrelic" header
                    var distributedTracePayload = GetOutboundHeader(DistributedTraceHeaderType.NewRelic, transaction, timestamp);
                    if (!string.IsNullOrWhiteSpace(distributedTracePayload))
                    {
                        setter(carrier, Constants.DistributedTracePayloadKey, distributedTracePayload);
                    }
                }

                var createOutboundTraceContextHeadersSuccess = false;
                try
                {
                    var traceparent = GetOutboundHeader(DistributedTraceHeaderType.W3cTraceparent, transaction, timestamp);
                    var tracestate  = GetOutboundHeader(DistributedTraceHeaderType.W3cTracestate, transaction, timestamp);

                    createOutboundTraceContextHeadersSuccess = string.IsNullOrEmpty(tracestate) ? false : true;

                    if (createOutboundTraceContextHeadersSuccess)
                    {
                        setter(carrier, Constants.TraceParentHeaderKey, traceparent);
                        setter(carrier, Constants.TraceStateHeaderKey, tracestate);
                        transaction.TransactionMetadata.HasOutgoingTraceHeaders = true;
                    }
                }
                catch (Exception ex)
                {
                    _agentHealthReporter.ReportSupportabilityTraceContextCreateException();
                    Log.Error(ex);
                }

                if (createOutboundTraceContextHeadersSuccess && _configurationService.Configuration.PayloadSuccessMetricsEnabled)
                {
                    _agentHealthReporter.ReportSupportabilityTraceContextCreateSuccess();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }