Skip to content

[Shim] Start/StopOperation with tests#3011

Merged
rajkumar-rangaraj merged 2 commits intodevelopfrom
rajrang/startoperation
Nov 10, 2025
Merged

[Shim] Start/StopOperation with tests#3011
rajkumar-rangaraj merged 2 commits intodevelopfrom
rajrang/startoperation

Conversation

@rajkumar-rangaraj
Copy link
Member

@rajkumar-rangaraj rajkumar-rangaraj commented Nov 7, 2025

Fix Issue # .

Changes

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

API Surface

StartOperation<T> overloads modified:

  • 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
}

@rajkumar-rangaraj
Copy link
Member Author

@copilot summarize the changes in this PR

Copy link

Copilot AI commented Nov 7, 2025

@rajkumar-rangaraj I've opened a new pull request, #3012, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes multiple test files as part of a shim-related effort for Start/StopOperation functionality. The changes involve deleting test files covering various SDK components including telemetry configuration, tracing, extensibility, and platform implementation.

Reviewed Changes

Copilot reviewed 104 out of 173 changed files in this pull request and generated no comments.

Show a summary per file
File Description
FailOnAssertSetup.cs Removes test setup for converting Debug.Assert into test failures (NETFRAMEWORK only)
TelemetrySinkTests.cs Removes tests for telemetry sink functionality including processor chains and multi-sink scenarios
TelemetryConfigurationTest.cs Removes comprehensive tests for TelemetryConfiguration including Active property, CreateDefault method, and connection string handling
TelemetryConfigurationFactoryTest.cs Removes extensive tests for configuration factory initialization and XML parsing
Multiple Tracing Test Files Removes various tracing-related tests including diagnostics, event throttling, heartbeat, and self-diagnostics
Multiple Implementation Test Files Removes tests for core implementation classes like snapshotting collections, telemetry processors, and platform-specific functionality

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@xiang17 xiang17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.

@rajkumar-rangaraj rajkumar-rangaraj merged commit 2c615f6 into develop Nov 10, 2025
16 checks passed
@rajkumar-rangaraj rajkumar-rangaraj deleted the rajrang/startoperation branch November 10, 2025 23:02
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.

4 participants