question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Instrumentation.StackExchangeRedis Should Resolve IConnectionMultiplexer from DI

See original GitHub issue

Feature Request

I have registered IConnectionMultiplexer in my DI container for health checks, a Redis implementation of IDistributedCache and other reasons. It would be nice if OpenTelemetry.Instrumentation.StackExchangeRedis contained an overload for the AddRedisInstrumentation function that allows you to resolve IConnectionMultiplexer from DI:

public static TracerProviderBuilder AddRedisInstrumentation(
    this TracerProviderBuilder builder,
    Func<IConnectionMultiplexer, IServiceBuilder> getConnection,
    Action<StackExchangeRedisCallsInstrumentationOptions> configureOptions = null);

Another overload that assumes that IConnectionMultiplexer is registered in DI would also be helpful:

public static TracerProviderBuilder AddRedisInstrumentation(
    this TracerProviderBuilder builder,
    Action<StackExchangeRedisCallsInstrumentationOptions> configureOptions = null);

Is your feature request related to a problem?

I have to create a new connection specifically for Open Telemetry.

Describe the solution you’d like:

I would like to reuse the connection I’m using elsewhere.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
evilpilafcommented, May 7, 2021

@trejjam it’s still possible, just a bit different, for rc4 you can do this:

services.AddOpenTelemetryTracing(builder => {
    builder.Configure((provider, b) => {
        var connectionMultiplexer = provider.GetRequiredService<IConnectionMultiplexer>();
        b.AddRedisInstrumentation(connectionMultiplexer);
    });
});

Though I agree it’s a bit more cumbersome and the previous approach was better.

2reactions
CodeBlanchcommented, Jun 12, 2021

We’re trying to do something here! It is tricky because we’re supporting .NET Framework where IServiceCollection probably isn’t being used. The reason the API changed is we introduced this thing called IDeferredTracerProviderBuilder. Right now it is part of SDK. We’re trying to figure out how to get it into API. Once it is there, it should be possible to add an extension like this in OpenTelemetry.Instrumentation.StackExchangeRedis:

        public static TracerProviderBuilder AddRedisInstrumentation(
            this TracerProviderBuilder builder,
            Action<StackExchangeRedisCallsInstrumentationOptions> configureOptions = null)
        {
            if (!(builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder))
            {
                throw new NotSupportedException("IConnectionMultiplexer could not be resolved automatically.");
            }

            return deferredTracerProviderBuilder.Configure((sp, builder) =>
            {
                StackExchangeRedisCallsInstrumentationOptions options = sp.GetOptions<StackExchangeRedisCallsInstrumentationOptions>();
                configureOptions?.Invoke(options);

                builder
                    .AddInstrumentation(() => new StackExchangeRedisCallsInstrumentation(
                        sp.GetService<IConnectionMultiplexer>() ?? throw new InvalidOperationException("IConnectionMultiplexer could not be found on IServiceProvider."),
                        options))
                    .AddSource(StackExchangeRedisCallsInstrumentation.ActivitySourceName);
            });
        }

That would allow it to automatically resolve IConnectionMultiplexer (& options) automatically through the IServiceProvider and users won’t need to use the more cumbersome syntax shown above!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redis Cache Calls In OpenTelemetry in DotNet
So the solution is to create it explicitly and pass and register in DI (if needed). So the code will be next: IConnectionMultiplexer...
Read more >
StackExchange.Redis.Extensions
In a basic console app when DI resolves in constructor a error log raised: "Redis connection error restored." It comes from ConnectionRestored in ......
Read more >
Dependency Injected StackExchange.Redis Client
I could not figure out how to add IDatabase to services. My CacheClient looks like this: public class CacheClient : ICacheClient { private ......
Read more >
OpenTelemetry.Instrumentation.StackExchangeRedis 1.0.0 ...
This is a prerelease version of OpenTelemetry.Instrumentation.StackExchangeRedis. .NET CLI; Package Manager; PackageReference; Paket CLI ...
Read more >
Handling Transient Failures in Azure Using Polly
In my experience I have found that these Redis timeout failures do resolve with in 1 or 2 retries. If retry strategy is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found