Skip to content

[bug] Not possible to process Baggage for Redis Spans #2681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ovidiubuligan opened this issue Apr 2, 2025 · 2 comments
Open

[bug] Not possible to process Baggage for Redis Spans #2681

ovidiubuligan opened this issue Apr 2, 2025 · 2 comments
Labels
bug Something isn't working comp:instrumentation.stackexchangeredis Things related to OpenTelemetry.Instrumentation.StackExchangeRedis

Comments

@ovidiubuligan
Copy link

Component

OpenTelemetry.Instrumentation.StackExchangeRedis

Package Version

Package Name Version
OpenTelemetry.Api 1.11.2
OpenTelemetry 1.11.2

Runtime Version

net8.0

Description

I have the following BaggageProcessor that adds certain baggage items as attributes to all spans .

    public class BaggageSpanProcessor : BaseProcessor<Activity>
    {

        /// Span processor that adds <see cref="Baggage"/> fields  that start with 'MYPREFIX'  to every new span  as tags
        /// based on baggate values
        /// <inheritdoc />
        public override void OnEnd(Activity activity)
        {
            foreach (var entry in Baggage.Current)
            {
                if (entry.Key.StartsWith("MYPREFIX"))
                {
                    activity.SetTag(entry.Key, entry.Value);
                }
            }
        }
    }

This processor works for all spans except for the redis instrumentation generated spans due to how the activities are created in a separate thread.

Steps to Reproduce

Simplified code to illustrate

using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;
using StackExchange.Redis;
using OpenTelemetry.Instrumentation.StackExchangeRedis;
using System;

public class BaggageSpanProcessor : BaseProcessor<Activity>
{
    /// Span processor that adds <see cref="Baggage"/> fields  that start with 'MYPREFIX'  to every new span  as tags
    /// based on baggate values
    /// <inheritdoc />
    public override void OnEnd(Activity activity)
    {
        foreach (var entry in Baggage.Current)
        {
            if (entry.Key.StartsWith("MYPREFIX"))
            {
                activity.SetTag(entry.Key, entry.Value);
            }
        }
    }
}



var tracer = tracerProvider.GetTracer("ExampleTracer");
var connection = ConnectionMultiplexer.Connect("localhost:6379");
var ActivitySource = new ActivitySource("ExampleTracer");

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddSource("ExampleTracer")
    .AddProcessor(new BaggageSpanProcessor())
    .AddRedisInstrumentation(connection, options =>
    {
        // Tried this hack but no baggage information or paret object present
        options.Enrich = (activity, profiledCommand) =>
        {
            foreach (var baggageItem in activity.Baggage)
            {
                if (baggageItem.Key.StartsWith("MYPREFIX"))
                {
                    activity.SetTag(baggageItem.Key, baggageItem.Value);
                }
            }
        };
    })
    .AddConsoleExporter()
    .Build();



using (var activity = ActivitySource.StartActivity("ExampleOperation"))
{
    // Add baggage items
    Baggage.SetBaggage("MYPREFIX.user", "user123");
    Baggage.SetBaggage("MYPREFIX.session", "session456");

    // Execute Redis commands
    var db = connection.GetDatabase();
    db.StringSet("key", "value");
    var value = db.StringGet("key");
}

Console.WriteLine("Press any key to exit...");
Console.ReadLine();

Expected Result

Redis Operation spans to be enriched with new span attributes from the BaggageSpanProcessor

Actual Result

Not attributes added by BaggageSpanProcessor.

I was not able to find a workaround .

Additional Context

No response

@ovidiubuligan ovidiubuligan added the bug Something isn't working label Apr 2, 2025
@github-actions github-actions bot added the comp:instrumentation.stackexchangeredis Things related to OpenTelemetry.Instrumentation.StackExchangeRedis label Apr 2, 2025
Copy link
Contributor

github-actions bot commented Apr 2, 2025

Tagging component owner(s).

@matt-hensley

@matt-hensley
Copy link
Contributor

Thanks for the code sample, made this quick to investigate.

I went through the instrumentation code and do not see a quick fix.

Is this something you'd like to work on @ovidiubuligan ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working comp:instrumentation.stackexchangeredis Things related to OpenTelemetry.Instrumentation.StackExchangeRedis
Projects
None yet
Development

No branches or pull requests

2 participants