Skip to content

Conversation

Copy link

Copilot AI commented Nov 7, 2025

Fix Issue # .

Changes

Implements StartOperation extension methods for TelemetryClient to enable manual operation tracking with proper W3C trace context propagation and Activity lifecycle management.

API Surface

Three StartOperation<T> overloads added:

  • StartOperation<T>(operationName, operationId, parentOperationId) - Explicit trace/span ID control for distributed tracing scenarios
  • StartOperation<T>(operationTelemetry) - Initialize from pre-configured telemetry object
  • StartOperation<T>(activity) - Link existing Activity to telemetry (e.g., from message queues, manual instrumentation)

Returns IOperationHolder<T> that auto-tracks telemetry on Dispose().

Core Fixes

Parent ID handling: Only set Context.Operation.ParentId when non-default to prevent invalid "0000000000000000" values on root operations.

Ambient activity suppression: Root operations (no parent span) now suppress Activity.Current during creation to prevent incorrect parent relationships. Suppressed activity restored on dispose if still active.

Operation name consistency: Set on both Activity tags and Context.Operation.Name for reliable retrieval.

Activity integration: Baggage and tags from existing Activity copied to telemetry properties for context preservation.

Supporting Changes

  • OperationHolder<T> tracks and restores suppressed ambient activities
  • DependencyTelemetry and ExceptionTelemetry initialize Properties dictionary in constructors
  • Test suite refactored for clarity and coverage of all overloads
  • BasicConsoleApp updated with working examples

Usage Example

// Simple operation tracking
using (var operation = telemetryClient.StartOperation<RequestTelemetry>("ProcessOrder"))
{
    // Inner operations auto-parent
    using (var dep = telemetryClient.StartOperation<DependencyTelemetry>("QueryDatabase"))
    {
        dep.Telemetry.Type = "SQL";
        // ... perform work
    } // Auto-tracked on dispose
}

// Explicit distributed trace context
var traceId = ActivityTraceId.CreateFromString("4bf92f3577b34da6a3ce929d0e0e4736");
var parentSpan = ActivitySpanId.CreateFromString("00f067aa0ba902b7");
using (var op = telemetryClient.StartOperation<RequestTelemetry>(
    "IncomingRequest", 
    traceId.ToHexString(), 
    parentSpan.ToHexString()))
{
    // Maintains distributed trace context from upstream caller
}

// Link existing Activity
var activity = activitySource.StartActivity("CustomOperation", ActivityKind.Server);
using (var op = telemetryClient.StartOperation<RequestTelemetry>(activity))
{
    // Telemetry synchronized with Activity lifecycle
}

Checklist

  • I ran Unit Tests locally.
  • CHANGELOG.md updated with one line description of the fix, and a link to the original issue if available.

For significant contributions please make sure you have completed the following items:

  • Design discussion issue #
  • Changes in public surface reviewed

The PR will trigger build, unit tests, and functional tests automatically. Please follow these instructions to build and test locally.

Notes for authors:

  • FxCop and other analyzers will fail the build. To see these errors yourself, compile localy using the Release configuration.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add start/stop operation with tests [Shim] Implement StartOperation/StopOperation with W3C trace context support Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants