Skip to content

Support starting operations using a parent ActivityContext (OTEL TraceContext) to simplify setting the various underlying fields. #2987

@Euan-McVie

Description

@Euan-McVie

Is your feature request related to a problem?

  • We have a fairly large enterprise solution that we want to begin transferring over to use OTEL instead of AI SDK.
  • To avoid a "big bang" updated we are looking to update modules/components individually.
  • We aim to standardise passing around the (non-HTTP) distributed traces using an OTEL TraceContext using the built in ActivityContext.
  • This allows OTEL code to use activitySource.StartActivity(_activityName, ActivityKind.Internal, message.ParentContext ?? default);
  • There is no complete equivalent available for the AI SDK.

Describe the solution you'd like.
An overload to TelemetryClient.StartOperation that accepts the operationName and a parentActivityContext similar to StartOperation<T>(string operationName, string operationId, string parentOperationId = null) or the other equivalents that accept parent Ids, but that correctly unpack the W3C trace context.

Describe alternatives you've considered.
Custom extension method using an Activity similar to service bus documented approach.

    public static IOperationHolder<TOperationTelemetry> StartOperation<TOperationTelemetry>(
        this TelemetryClient telemetryClient,
        string operationName,
        ActivityContext? parentContext)
        where TOperationTelemetry : OperationTelemetry, new()
    {
        if (!parentContext.HasValue)
            return telemetryClient.StartOperation<TOperationTelemetry>(operationName);

        var context = parentContext.Value;

#pragma warning disable CA2000 // Dispose objects before losing scope
        var activity = new Activity(operationName)
        {
            TraceStateString = context.TraceState,
            // HasRemoteParent = context.IsRemote (Can't set this without using ActivitySource, but unclear if AI SDK uses it anyway.)
        }
        .SetParentId(context.TraceId, context.SpanId, context.TraceFlags);
#pragma warning restore CA2000 // Dispose objects before losing scope

        return telemetryClient.StartOperation<TOperationTelemetry>(activity);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions