Skip to content

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

Open
@ovidiubuligan

Description

@ovidiubuligan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcomp:instrumentation.stackexchangeredisThings related to OpenTelemetry.Instrumentation.StackExchangeRedis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions