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

Pass the IServiceProvider in ConfigureResource, WithTracing & WithMetrics #4228

Closed
megla-tlanghorst opened this issue Feb 24, 2023 · 4 comments · Fixed by #4230 or #4261
Closed

Pass the IServiceProvider in ConfigureResource, WithTracing & WithMetrics #4228

megla-tlanghorst opened this issue Feb 24, 2023 · 4 comments · Fixed by #4230 or #4261
Labels
enhancement New feature or request

Comments

@megla-tlanghorst
Copy link

Feature Request

Is your feature request related to a problem?

We need an IServiceProvider in ConfigureResource and WithTracing (and WithMetrics if we'd use it) for stuff like the options pattern.

Describe the solution you'd like:

A service provider being provided (as an option) to the respective callbacks.

Describe alternatives you've considered.

For ConfigureResource you could maybe get it done with an extension method, but you're basically rewriting library code at that point. For WithTracing I don't see a solution, without accessing private stuff via reflection.

Additional Context

@CodeBlanch
Copy link
Member

Just for anyone landing on this issue see #4139 (comment) for some code you can use to accomplish these tasks today using the 1.4 API.

@antmeehan
Copy link

@cijothomas Can we please re-open this as #4261 doesn't appear to fix this issue (in the way #4228 attempted)?

@CodeBlanch
Copy link
Member

We won't be adding top-level extensions which expose IServiceProvider for now because they are dangerous.

If you want to use IServiceProvider to do resource detection we support stuff like this:

appBuilder.Services.AddOpenTelemetry()
    .ConfigureResource(b => b.AddDetector(sp => new MyResourceDetector(sp)));

internal sealed class MyResourceDetector : IResourceDetector
{
    private readonly IServiceProvider serviceProvider;

    public MyResourceDetector(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public Resource Detect()
    {
        var options = this.serviceProvider.GetRequiredService<IOptions<MyOptions>>().Value;

        Dictionary<string, object> values = new();

        if (options.AddSomeResourceInfo)
        {
            // add keys
        }

        if (options.AddSomeOtherResourceStuff)
        {
            // add keys
        }

        return new(values);
    }
}

If you want to access IServiceProvider to configure logging/tracing/metrics we have this shape:

services.AddOpenTelemetry().WithTracing();
services.ConfigureOpenTelemetryTracerProvider((sp, builder) => { 
   // Configure TracerProviderBuilder using IServiceProvider but watch out IServiceCollection is no longer available...
});

Do those work for your use case?

@antmeehan
Copy link

Thanks @CodeBlanch for the detailed response. I was actually already using ConfigureOpenTelemetryTracerProvider to add attributes which is working except they aren't being applied to log traces (it seems to only be applying the default server role etc). I'm observing the same behaviour using the AddDetector you've suggested so I'm guessing I'm doing something wrong on my side.

bart-vmware added a commit to SteeltoeOSS/Steeltoe that referenced this issue Apr 16, 2024
- EnableGrpcAspNetCoreSupport was replaced by environment variable OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION, see https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs
- Replace usage of IDeferredTracerProviderBuilder, see open-telemetry/opentelemetry-dotnet#4228
bart-vmware added a commit to SteeltoeOSS/Steeltoe that referenced this issue Apr 17, 2024
* Update OpenTelemetry packages
- EnableGrpcAspNetCoreSupport was replaced by environment variable OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION, see https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs
- Replace usage of IDeferredTracerProviderBuilder, see open-telemetry/opentelemetry-dotnet#4228

* Update src/Management/src/Wavefront/Exporters/TagExtensions.cs

Co-authored-by: Tim Hess <tim.hess@broadcom.com>

---------

Co-authored-by: Tim Hess <tim.hess@broadcom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment