/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. *--------------------------------------------------------------------------------------------*/ using System.Diagnostics; namespace GitHub.Copilot; internal static class TelemetryHelpers { internal static (string? Traceparent, string? Tracestate) GetTraceContext() { return Activity.Current is { } activity ? (activity.Id, activity.TraceStateString) : (null, null); } /// /// Sets to reflect the trace context from the given /// W3C / headers. /// The runtime already owns the execute_tool span; this just ensures /// user code runs under the correct parent so any child activities are properly parented. /// Dispose the returned to restore the previous . /// /// /// Because this Activity is not created via an , it will not /// be sampled or exported by any standard OpenTelemetry exporter — it is invisible in /// trace backends. It exists only to carry the remote parent context through /// so that child activities created by user tool /// handlers are parented to the CLI's span. /// internal static Activity? RestoreTraceContext(string? traceparent, string? tracestate) { if (traceparent is not null && ActivityContext.TryParse(traceparent, tracestate, out ActivityContext parent)) { Activity activity = new("copilot.tool_handler"); activity.SetParentId(parent.TraceId, parent.SpanId, parent.TraceFlags); if (tracestate is not null) { activity.TraceStateString = tracestate; } activity.Start(); return activity; } return null; } }