-
Notifications
You must be signed in to change notification settings - Fork 772
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
[di] Expose a detached TracerProviderBuilder extension on IServiceCollection which may modify services. #4508
[di] Expose a detached TracerProviderBuilder extension on IServiceCollection which may modify services. #4508
Conversation
…on which may modify services.
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #4508 +/- ##
==========================================
+ Coverage 85.12% 85.19% +0.06%
==========================================
Files 317 317
Lines 12602 12594 -8
==========================================
+ Hits 10728 10729 +1
+ Misses 1874 1865 -9
|
A bit more detail for anyone curious... This throws today: builder.Services.AddOpenTelemetry()
.WithTracing(builder => builder.AddConsoleExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddOtlpExporter()); This can be used as a workaround: builder.Services.AddOpenTelemetry()
.WithTracing(builder => builder.AddConsoleExporter());
builder.Services.AddOpenTelemetry()
.WithTracing(builder => builder.AddOtlpExporter()); However there are two issues with the workaround:
|
...uilderExtensions/Trace/OpenTelemetryDependencyInjectionTracingServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
...uilderExtensions/Trace/OpenTelemetryDependencyInjectionTracingServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
/// cref="TracerProviderBuilder"/> used to create the <see | ||
/// cref="TracerProvider"/> for the <see cref="IServiceCollection"/> being | ||
/// configured. | ||
/// cref="TracerProviderBuilder"/>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good improvement! Was confusing before "used to configure ... used to create the ... for the ... being configured" 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems ok to me. It is new public API, but at least it is simply an additional overload of an existing method and not an entirely new method (for example, won't increase the number of methods one might see in intellisense).
Also, it seems this new overload is better than the previous one. If I'm understanding things correctly, I'd imagine this new overload would serve many of the scenarios the existing method serves plus more. The existing method seems like an even more advanced scenario where you know for sure you need a handle on the IServiceProvider.
Is the plan to also add this for meter and logger providers, too? |
…/CodeBlanch/opentelemetry-dotnet into di-configuretracing-withservices
Yes sir! I'll do a PR for each. |
/// cref="TracerProviderBuilder"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls | ||
/// can be chained.</returns> | ||
public static IServiceCollection ConfigureOpenTelemetryTracerProvider( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many helper extensions register services and may throw if invoked inside the configuration
--> This part is bit unclear as end users may not know which extensions register services versus which ones do not.
Could we add some guidance here for end users? some message saying - if you don't need access to ServiceProvider then use the other overload.
…lection which may modify services. (open-telemetry#4508)
Fixes #4503
Changes
IServiceCollection.ConfigureOpenTelemetryTracerProvider
extension which doesn't requireIServiceProvider
and allows forIServiceCollection
modification.Public API Changes
namespace OpenTelemetry.Trace { public static class OpenTelemetryDependencyInjectionTracingServiceCollectionExtensions { + public static IServiceCollection ConfigureOpenTelemetryTracerProvider(this IServiceCollection services, Action<TracerProviderBuilder> configure) {} } }
Details
We have this pattern for configuring providers using the
IServiceCollection
: https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/trace/extending-the-sdk#iservicecollection-extension-methodsWe refer to this as "detached" configuration.
Today the API passes the
IServiceProvider
to the configuration delegate.This type of stuff will work fine:
This will throw a NotSupportedException:
The reason for that is once the
IServiceProvider
is available, we can no longer add services.#4503 shows some users want to do detached things but don't really care about the
IServiceProvider
.What this PR does is add a detached extension which may modify services.
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes