public static bool SetMdcState(MappedDiagnosticsContextSetterProxy mdc, Tracer tracer)
        {
            var removeSpanId = false;

            mdc.Set(CorrelationIdentifier.ServiceKey, tracer.DefaultServiceName ?? string.Empty);
            mdc.Set(CorrelationIdentifier.VersionKey, tracer.Settings.ServiceVersion ?? string.Empty);
            mdc.Set(CorrelationIdentifier.EnvKey, tracer.Settings.Environment ?? string.Empty);

            var spanContext = tracer.DistributedSpanContext;

            if (spanContext is not null)
            {
                // For mismatch version support we need to keep requesting old keys.
                var hasTraceId = spanContext.TryGetValue(SpanContext.Keys.TraceId, out string traceId) ||
                                 spanContext.TryGetValue(HttpHeaderNames.TraceId, out traceId);
                var hasSpanId = spanContext.TryGetValue(SpanContext.Keys.ParentId, out string spanId) ||
                                spanContext.TryGetValue(HttpHeaderNames.ParentId, out spanId);
                if (hasTraceId && hasSpanId)
                {
                    removeSpanId = true;
                    mdc.Set(CorrelationIdentifier.TraceIdKey, traceId);
                    mdc.Set(CorrelationIdentifier.SpanIdKey, spanId);
                }
            }

            return(removeSpanId);
        }
Пример #2
0
        public static bool SetMdcState(MappedDiagnosticsContextSetterProxy mdc, Tracer tracer)
        {
            var removeSpanId = false;

            mdc.Set(CorrelationIdentifier.ServiceKey, tracer.DefaultServiceName ?? string.Empty);
            mdc.Set(CorrelationIdentifier.VersionKey, tracer.Settings.ServiceVersion ?? string.Empty);
            mdc.Set(CorrelationIdentifier.EnvKey, tracer.Settings.Environment ?? string.Empty);

            var spanContext = tracer.DistributedSpanContext;

            if (spanContext is not null &&
                spanContext.TryGetValue(HttpHeaderNames.TraceId, out string traceId) &&
                spanContext.TryGetValue(HttpHeaderNames.ParentId, out string spanId))
            {
                removeSpanId = true;
                mdc.Set(CorrelationIdentifier.TraceIdKey, traceId);
                mdc.Set(CorrelationIdentifier.SpanIdKey, spanId);
            }

            return(removeSpanId);
        }