open-telemetry / opentelemetry-dotnet

The OpenTelemetry .NET Client
https://opentelemetry.io
Apache License 2.0
3.18k stars 753 forks source link

[question] `UseOtlpExporter` & `OtlpExporterOptions` #5802

Closed lvde0 closed 1 month ago

lvde0 commented 1 month ago

What is the question?

Is it possible to use UseOtlpExporter together with OtlpExporterOptions configured via .Configure<OtlpExporterOptions>? According to the documentation it's only possible to pass aprotocol and endpoint directly to UseOtlpExporter or use the environment variables. However setting OTEL_EXPORTER_OTLP_HEADERS does not seem to work.

Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", $"x-otlp-api-key={apiKey}");

The same works though when I am configuring the options directly via:

.WithLogging(l => l.AddOtlpExporter(k =>
{
    k.Endpoint = new Uri(otlpEndpoint!);
    k.Headers = $"x-otlp-api-key={apiKey}";
}))

Additional context

No response

vishweshbankwar commented 1 month ago

@lvde0 - Do you have a repro? It should work.

lvde0 commented 1 month ago

@vishweshbankwar

 services
     .AddOpenTelemetry()
     .ConfigureResource(
         resource => resource.AddService(serviceName, serviceInstanceId: serviceInstanceId))
     .WithMetrics(metrics => metrics
         .AddAspNetCoreInstrumentation()
         .AddRuntimeInstrumentation()
         .AddProcessInstrumentation()
         .AddMeter("Microsoft.AspNetCore.Hosting")
         .AddMeter("Microsoft.AspNetCore.Server.Kestrel"))
     .WithTracing(tracing =>
             tracing
                 .AddAspNetCoreInstrumentation()
                 .AddHttpClientInstrumentation()
                 .AddEntityFrameworkCoreInstrumentation()
                 .SetSampler(new AlwaysOnSampler()))
     .UseOtlpExporter();

 services.Configure<OtlpExporterOptions>(k => k.Endpoint = new Uri(otlpEndpoint));

Seems that the action in Configure is never called.

It seems that internally OtlpExporterBuilderOptions are used, but these are marked internal. UseOtlpExporter also passes null configuration to OtlpExporterBuilder so binding from config is also not possible :/

CodeBlanch commented 1 month ago

This is not supported. The reason for that is because it is ambiguous. If you did this...

services.Configure<OtlpExporterOptions>(k => k.Endpoint = new Uri(otlpEndpoint));

Are you intending to configure the exporter for tracing, metrics, logging, or all?

My original design/intention with UseOtlpExporter was this would be supported:

services.Configure<OtlpExporterBuilderOptions>(o => ...);

OtlpExporterBuilderOptions more correctly models the hierarchy and doesn't suffer from the ambiguity.

There are also a bunch of "Configure" helper methods defined on OtlpExporterBuilder to support configuring things from code: https://github.com/open-telemetry/opentelemetry-dotnet/blob/1b3f1894d95fd01fe475245ec60ef273d5f77bb6/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Builder/OtlpExporterBuilder.cs#L16

And there was this extension to bind OtlpExporterBuilderOptions to IConfiguration: https://github.com/open-telemetry/opentelemetry-dotnet/blob/1b3f1894d95fd01fe475245ec60ef273d5f77bb6/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Builder/OpenTelemetryBuilderOtlpExporterExtensions.cs#L98

What happened is during the development of UseOtlpExpoter the scope was reduced and all of this was made internal.

I'm going to close this issue. But if you want any of this stuff, please open a feature request issue for what you want. Happy to explore making some of this public or exposing other APIs to enable other scenarios. The reason I left it all internal and didn't remove it is I suspected users would ask for more features 😄