opentelemetry-dotnet-contrib
opentelemetry-dotnet-contrib copied to clipboard
[Exporter.Geneva] Add DI support for GenevaLogExporter
Issue with OpenTelemetry.Exporter.Geneva
List of all OpenTelemetry NuGet packages and version that you are using:
Runtime version:
- net6.0
Is this a feature request or a bug?
- [x] Feature Request
- [ ] Bug
What is the expected behavior?
It would be great if we can add DI support for GenevaLogExporter, similar to what we have for GenevaMetricExporter and GenevaTraceExporter. This is possible now that https://github.com/open-telemetry/opentelemetry-dotnet/pull/3504 is released.
Could you please let us which method you want to see on Geneva Log Exporter in DI?
Yes, I'm talking about OpenTelemetryLoggerOptions.AddGenevaLogExporter specifically. Within the extension method GenevaExporterOptions is currently created via a constructor, but I think now we can use OpenTelemetryLoggerOptions.ConfigureProvider to get it from the DI container. What I would like to achieve with this is to bind GenevaExporterOptions to an IConfiguration outside of AddGenevaLogExporter.
Hey, I wonder if is this available yet? I would like to have this:
configure.AddOpenTelemetry(options => options.AddProcessor<Custom_Log_Scrubber_Rule_List>());
, where I can add custom scrubbing rules through Host builder @CodeBlanch
Update for everyone. This is not available yet. Sorry! We're waiting on the OpenTelemetry Specification to declare log API/SDK specs stable before we do anything. Hoping that will be soon and then we can have parity in logs with the DI API surface we have for tracing & metrics. Current plan is for 1.6 to ship the SDK support and then we can update things like GenevaExporter.
That being said, you can accomplish some stuff today with a bit of ugly code!
Here is a startup snippet which accomplishes what some of the above is requesting...
appBuilder.Logging.AddOpenTelemetry();
appBuilder.Services.AddSingleton<Custom_Log_Scrubber_Rule_List>();
appBuilder.Services.AddOptions<OpenTelemetryLoggerOptions>().Configure<IServiceProvider>((options, sp) =>
{
options.AddProcessor(sp.GetRequiredService<Custom_Log_Scrubber_Rule_List>());
options.AddGenevaLogExporter(genevaOptions =>
{
appBuilder.Configuration.GetSection("GenevaExporter").Bind(genevaOptions);
});
});
The key bit of knowledge is that OpenTelemetryLoggerOptions is requested through the Options API today which means you can use the IServiceProvider to modify it.
Update...
-
For help configuring
GenevaExporterOptionsusing services retrieved through the DI container see: https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Exporter.Geneva/README.md#logging -
For the question about injecting a processor using DI...
A new API was released in the 1.7.0 OpenTelemetry SDK which makes the above slightly easier. You can now do this:
appBuilder.Services.AddSingleton<MyCustomProcessor>(); appBuilder.Logging.AddOpenTelemetry(options => options.AddProcessor( (sp, processor) => sp.GetRequiredService<MyCustomProcessor>()); });