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] AspNetCore instrumentation Query redaction does not respect the environment variable set in runtime #6060

Open
jundayin opened this issue Jan 15, 2025 · 3 comments
Labels
pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package question Further information is requested

Comments

@jundayin
Copy link

Package

OpenTelemetry

Package Version

Package Name Version
OpenTelemetry.Api 1.10.0
OpenTelemetry 1.10.0
OpenTelemetry.Instrumentation.AspNetCore 1.10.0
OpenTelemetry.Exporter.Console 1.10.0
OpenTelemetry.Extensions.Hosting 1.10.0

Runtime Version

net8.0

Description

The query attributes redaction does not respect the environment variable set in runtime.

Steps to Reproduce

using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Environment.SetEnvironmentVariable("OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION", "true"); // This does not work
            
var envVar = Environment.GetEnvironmentVariable(
    "OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION"); // This is true

var envVars = Environment.GetEnvironmentVariables(); // Contains the env var

builder.Services
    .AddOpenTelemetry()
    .WithTracing(builder =>
    {
        builder.AddAspNetCoreInstrumentation();
        builder.AddConsoleExporter();
    });

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapGet("/search", (string query) =>
{
    return $"You searched for: {query}";
})
.WithName("Search")
.WithOpenApi();

app.Run();

Expected Result

When the environment variable is successfully set during runtime before OpenTelemetry is registered, the Query redaction should be disabled

Actual Result

The query redaction is still being enabled

Additional Context

Please refer this repo https://github.com/jundayin/OpenTelemetryEnvRepro

@jundayin jundayin added bug Something isn't working needs-triage New issues which have not been classified or triaged by a community member labels Jan 15, 2025
@github-actions github-actions bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Jan 15, 2025
@rajkumar-rangaraj
Copy link
Contributor

In ASP.NET Core, the recommended way to configure environment variables is by using builder.Configuration.AddEnvironmentVariables(). This ensures that environment variables are properly loaded into the application's configuration during startup.

Did you try using this approach to set the environment variable?

Reference: ASP.NET Core Configuration - Environment Variables

OpenTelemetry .NET relies on IConfiguration to load environment variables, making it more adaptable to the ASP.NET Core configuration model.

@rajkumar-rangaraj rajkumar-rangaraj added question Further information is requested and removed bug Something isn't working needs-triage New issues which have not been classified or triaged by a community member labels Jan 15, 2025
@jundayin
Copy link
Author

jundayin commented Jan 15, 2025

Thanks, that explains why it does not work in my repro since the host configuration is already built. If I move the SetEnvironmentVariable before CreateBuilder it works.

I guess I have to inform my SDK consumer to add the env var or appSetting explicitly. Is it possible to expose these kinds of configuration through code so that we don't need such hard-coded value everywhere

@CEbbinghaus
Copy link

I would like to point out that this is not possible for non ASP.Net Core apps...

The application I am having the problem with is a command-line...

@jundayin jundayin reopened this Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants