Skip to content
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

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

Open
HHobeck opened this issue Jan 10, 2025 · 0 comments
Labels
bug Something isn't working needs-triage New issues which have not been classified or triaged by a community member pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package

Comments

@HHobeck
Copy link

HHobeck commented Jan 10, 2025

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

@HHobeck HHobeck added bug Something isn't working needs-triage New issues which have not been classified or triaged by a community member labels Jan 10, 2025
@github-actions github-actions bot added the pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage New issues which have not been classified or triaged by a community member pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package
Projects
None yet
Development

No branches or pull requests

1 participant