Skip to content

[bug] Adding a baggage entry for the current trace and replace key and value is not possible via Baggage API #6057

@HHobeck

Description

@HHobeck

Package

OpenTelemetry.Api

Package Version

Package Name Version
OpenTelemetry.Api 1.10.0
OpenTelemetry 1.10.0
OpenTelemetry.Exporter.OpenTelemetryProtocol 1.10.0
OpenTelemetry.Extensions.Hosting 1.10.0
OpenTelemetry.Instrumentation.AspNetCore 1.10.1
OpenTelemetry.Instrumentation.Http 1.10.0

Runtime Version

net8.0

Description

Baggage allows you to add a key with a value as an attribute to every subsequent child span of the current application context. The documentation of the OpenTelemetry .NET API -> Beggage API states:

Baggage API allows users to add context to metric, traces, and logs. Baggage can be propagated out of proc using Propagators. OpenTelemetry SDK ships a BaggagePropagator and enables it by default.

and

The recommended way to add Baggage is to use the Baggage.SetBaggage() API. OpenTelemetry users should not use the Activity.AddBaggage method.

If I try this feature using the Baggage API this is not possible for a simple ASP.NET Core Web API.

Steps to Reproduce

Program.cs:

using OpenTelemetry;
using OpenTelemetry.Trace;
using System.Net;
using WebApplication1.Controllers;

namespace WebApplication1
{
    public class Program
    {
        public static void Main(string[] arguments)
        {
            WebApplicationBuilder applicationBuilder = WebApplication.CreateBuilder(arguments);

            // Add services to the container.
            OpenTelemetryBuilder openTelemetryBuilder = applicationBuilder.Services.AddOpenTelemetry();
            openTelemetryBuilder.WithTracing(builder => builder
                .AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddOtlpExporter()
            );

            HttpClient.DefaultProxy = new WebProxy();
            applicationBuilder.Services.AddHttpClient(nameof(WeatherForecastController), configure =>
            {
                configure.BaseAddress = new Uri("http://localhost:5091/");
            });
            applicationBuilder.Services.AddControllers();
            applicationBuilder.Services.AddEndpointsApiExplorer();
            applicationBuilder.Services.AddSwaggerGen();

            WebApplication application = applicationBuilder.Build();
            if (application.Environment.IsDevelopment())
            {
                application.UseSwagger();
                application.UseSwaggerUI();
            }
            application.UseHttpsRedirection();
            application.UseAuthorization();
            application.MapControllers();
            application.Run();
        }
    }
}

WeatherForecastController.cs:

using Microsoft.AspNetCore.Mvc;
using OpenTelemetry;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController(IHttpClientFactory httpClientFactory) : ControllerBase
    {
        [HttpGet("A", Name = "GetWeatherForecastA")]
        public void GetA()
        {
            Baggage.Current.SetBaggage("MyValue", "HelloWorld-1");
            string? myValue = Baggage.Current.GetBaggage("MyValue"); // this call returns null

            using HttpClient httpClient = httpClientFactory.CreateClient(nameof(WeatherForecastController));

            using HttpResponseMessage httpResponseMessage = httpClient
                .GetAsync("/WeatherForecast/B")
                .GetAwaiter().GetResult();
            httpResponseMessage.EnsureSuccessStatusCode();
        }

        [HttpGet("B", Name = "GetWeatherForecastB")]
        public void GetB()
        {
            string? myValue = Baggage.Current.GetBaggage("MyValue"); // this call returns null
        }
    }
}

Expected Result

The baggage value should be:

  • returned when calling the GetBaggage function
  • part of the trace record in otel exporter
  • propageted as Header (Correlation-Context or PropagationObject??) of all sub calls via HttpClient
  • part in all child spans

Actual Result

  1. Baggage.Current.GetBaggage("MyValue") returns null
  2. The value MyValue is not part of the trace record
  3. The value will be not propagated via Http Header to sub request calls
  4. Is not part of the child activity

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-triageNew issues which have not been classified or triaged by a community memberpkg:OpenTelemetry.ApiIssues related to OpenTelemetry.Api NuGet package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions