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

Add overload to AddOpenTelemetryTracing which has IServiceProvider parameter #3086

Closed
adnang opened this issue Mar 24, 2022 · 3 comments
Closed
Labels
enhancement New feature or request pkg:OpenTelemetry.Extensions.Hosting Issues related to OpenTelemetry.Extensions.Hosting NuGet package Stale Issues and pull requests which have been flagged for closing due to inactivity

Comments

@adnang
Copy link

adnang commented Mar 24, 2022

To consume other dependencies when setting up tracing for a Host, the README at the documentation for https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Extensions.Hosting/README.md suggests to use the TracerProviderBuilder.Configure method which accepts an Action with IServiceProvider parameter.

This is quite confusing and easy to miss for newcomers, since its not clear from tools like intelliisense that this option is available.
Also, you can recursively call this .Configure() method with a new Action - which has unclear behaviour from just looking at it:

services.AddOpenTelemetryTracing(builder =>
{
    builder.Configure((sp, b) => b.Configure((sp2, b2) => ...))

This design is also not consistent with other libraries extensions to IServiceCollection, which generally have an overload of the top-level method to provide access to the IServiceProvider.

Could we remove .Configure() from the public API in a future version, and instead have an overload:

public static IServiceCollection AddOpenTelemetryTracing(
    this IServiceCollection services, 
    Action<IServiceProvider, TracerProviderBuilder> configure)

which does the same thing as .Configure(Action<IServiceProvider, TracerProviderBuilder>) under the hood

@adnang adnang added the enhancement New feature or request label Mar 24, 2022
@adnang adnang changed the title Add overload to AddOpenTelemetryTracing which has IServicProvider lamda parameter Add overload to AddOpenTelemetryTracing which has IServiceProvider parameter Mar 24, 2022
@CodeBlanch
Copy link
Member

CodeBlanch commented Mar 25, 2022

@adnang

Also, you can recursively call this .Configure() method with a new Action

builder.Configure((sp, b) => b.Configure((sp2, b2) => ...))

Totally supported and works fine! Code link:

// Note: Not using a foreach loop because additional actions can be
// added during each call.
for (int i = 0; i < this.configurationActions.Count; i++)
{
this.configurationActions[i](serviceProvider, this);
}

Could we remove .Configure() from the public API in a future version

I didn't dig into this fully, but I don't think so. The reason is we need it is for library extensibility. Imagine in your startup you want to do something like this...

builder.AddCustomInstrumentationLibrary();

That extension could be authored like this:

public static TracerProviderBuilder AddCustomInstrumentationLibrary(this TracerProviderBuilder builder)
{
        (tracerProviderBuilder.GetServices()
            ?? throw new NotSupportedException(
                "CustomInstrumentationLibrary requires a hosting TracerProviderBuilder instance."))
            .AddHostedService<CustomInstrumentationHostedService>()
            .AddSingleton<CustomInstrumentationService>();

        return tracerProviderBuilder.Configure((sp, tracerProviderBuilder) =>
        {
            tracerProviderBuilder.AddInstrumentation(sp.GetRequiredService<CustomInstrumentationService>());
        });
}

Basically we are two-phase. Libraries are able to register services they need and also act on the final services when they are available.

Could we remove .Configure() from the public API in a future version, and instead have an overload:

public static IServiceCollection AddOpenTelemetryTracing(
    this IServiceCollection services, 
    Action<IServiceProvider, TracerProviderBuilder> configure)

I would love to find a way to do this, and I am open to suggestions/contributions, but given the above, I don't know if it is possible. Using the proposed extension with that library code...

services.AddOpenTelemetryTracing((sp, builder) =>
   // This is now broken. Because IServiceProvider has already been created at this point and services are closed. 
   // Essentially AddCustomInstrumentationLibrary can't register the services it depends on.
   builder.AddCustomInstrumentationLibrary()
);

Copy link
Contributor

This issue was marked stale due to lack of activity and will be closed in 7 days. Commenting will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 25, 2024
Copy link
Contributor

github-actions bot commented Oct 3, 2024

Closed as inactive. Feel free to reopen if this issue is still a concern.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg:OpenTelemetry.Extensions.Hosting Issues related to OpenTelemetry.Extensions.Hosting NuGet package Stale Issues and pull requests which have been flagged for closing due to inactivity
Projects
None yet
Development

No branches or pull requests

3 participants