Skip to content

Connecting manually instrumented spans with automatically instrumented OTel spans #5106

@aldy505

Description

@aldy505

Description

SInce we have IDisposable implemented for ISpan interface (PR #4627), is there any way to connect a parent of manually instrumented spans with a child span of automatically instrumented OTel span?

This is my current setup:

builder.Services.AddOpenTelemetry()
    .WithTracing(tracerProviderBuilder =>
        tracerProviderBuilder
            .AddSource("MyProgram")
            .AddAspNetCoreInstrumentation()
            .AddRedisInstrumentation()
            .AddHttpClientInstrumentation()
            .AddGrpcClientInstrumentation()
            .AddNpgsql()
            .AddSentry()
    );

builder.WebHost.UseSentry(options => {
    options.SampleRate = (float?)1.0;
    options.TracesSampleRate = 1.0;
    options.SendDefaultPii = true;
    options.UseOpenTelemetry();
    options.DisableSentryHttpMessageHandler = true;
});

With this current setup, I always use ActivitySource on my application-level methods, like so:

public class PasswordManagementService() {
    private static readonly ActivitySource ActivitySource = new("MyProgram");

    public async Task ChangePassword(ChangePasswordRequest request, CancellationToken cancellationToken) {
        using Activity? activity = ActivitySource.StartActivity();
        // do things
        // queries to PostgreSQL here
    }
}

The span is connected, but sadly it won't connect the errors perfectly. What I want to do is to directly use the StartSpan() method from Sentry, while still having the trace of PostgreSQL query from OTel being a child of the said span. Therefore the code will become:

public class PasswordManagementService() {
    public async Task ChangePassword(ChangePasswordRequest request, CancellationToken cancellationToken) {
        using var span = SentrySdk.StartSpan(operation: "function", description: "ChangePassword");
        // do things
        // queries to PostgreSQL here (using OTel, as I want to capture it automatically)
    }
}

Is there any workaround for this other than creating the span for PostgreSQL database operation manually?

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .net codeDocsSpans
    No fields configured for issues without a type.

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions