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); }
public static void RemoveMdcState(MappedDiagnosticsContextSetterProxy mdc, bool removeTraceIds) { mdc.Remove(CorrelationIdentifier.ServiceKey); mdc.Remove(CorrelationIdentifier.VersionKey); mdc.Remove(CorrelationIdentifier.EnvKey); if (removeTraceIds) { mdc.Remove(CorrelationIdentifier.TraceIdKey); mdc.Remove(CorrelationIdentifier.SpanIdKey); } }
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); }