Skip to content

Conversation

@jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented Jan 15, 2025

Resolves #3887:

Manual Testing

Can be tested manually by altering our OTel Console sample to look something like this:

var activitySource = new ActivitySource("Sentry.Samples.OpenTelemetry.Console");

SentrySdk.Init(options =>
{
    // You can set here in code, or you can set it in the SENTRY_DSN environment variable.
    // See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
    options.Dsn = "https://[email protected]/5428537";

    options.Debug = true;
    options.TracesSampleRate = 1.0;
    options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddSource(activitySource.Name)
    .AddHttpClientInstrumentation()
    .AddProcessor(new FilteringProcessor()) // <-- Adds a processor to filter out unwanted activities
    .AddSentry() // <-- Configure OpenTelemetry to send traces to Sentry
    .Build();

Console.WriteLine("Hello World!");

// Finally we can use OpenTelemetry to instrument our code. This activity will be captured as a Sentry transaction.
using (var activity = activitySource.StartActivity("Main"))
{
    // This creates a span called "Task 1" within the transaction
    using (var task = activitySource.StartActivity("Task 1"))
    {
        task?.SetTag("Answer", 42);
        Thread.Sleep(100); // simulate some work
        Console.WriteLine("Task 1 completed");
        task?.SetStatus(Status.Ok);
    }

    // Since we use `AddHttpClientInstrumentation` when initializing OpenTelemetry, the following Http request will also
    // be captured as a Sentry span
    var httpClient = new HttpClient();
    var html = await httpClient.GetStringAsync("https://example.com/");
    Console.WriteLine(html);
}

using (var activity = activitySource.StartActivity("Hide Me"))
{
    // This activity will be filtered out by the processor
    Console.WriteLine("This activity will be filtered out");
    Thread.Sleep(100); // simulate some work
}

Console.WriteLine("Goodbye cruel world...");

internal sealed class FilteringProcessor : BaseProcessor<Activity>
{
    // Filters out some activities
    public override void OnEnd(Activity activity)
    {
        // If the activity does not have a parent that matches the specified kinds or tags, mark it as not recorded
        if (activity.DisplayName == "Hide Me")
        {
            Console.WriteLine($"Filtering out activity: {activity.OperationName} {activity.DisplayName}");
            activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
            activity.IsAllDataRequested = false;
        }
    }
}

@jamescrosswell jamescrosswell marked this pull request as ready for review January 15, 2025 03:49
@jamescrosswell jamescrosswell changed the base branch from main to tidy-verify January 16, 2025 07:59
@jamescrosswell jamescrosswell changed the base branch from tidy-verify to main January 16, 2025 07:59
@jamescrosswell jamescrosswell requested review from aritchie and removed request for bitsandfoxes and vaind January 30, 2025 23:39
@jamescrosswell jamescrosswell merged commit 9b29d1f into main Feb 4, 2025
22 checks passed
@jamescrosswell jamescrosswell deleted the filtered-otel-spans branch February 4, 2025 21:09
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.

Filtering topmost Activities via Processor is not respected by Sentry Exporter

4 participants