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

[Instrumentation.StackExchangeRedis] Support when IConnectionMultiplexer is added with keyed service #1885

Merged
merged 15 commits into from
Jun 29, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentati
OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentationOptions.StackExchangeRedisInstrumentationOptions() -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, object! serviceKey) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, StackExchange.Redis.IConnectionMultiplexer! connection) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, StackExchange.Redis.IConnectionMultiplexer! connection, System.Action<OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentationOptions!>! configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string? name, StackExchange.Redis.IConnectionMultiplexer? connection, System.Action<OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentationOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string? name, object! serviceKey, System.Action<OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentationOptions!>! configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string? name, StackExchange.Redis.IConnectionMultiplexer? connection, object? serviceKey, System.Action<OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentationOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentationOptions!>! configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentation!>! configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureRedisInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<System.IServiceProvider!, OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisInstrumentation!>! configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Add support for instrumenting `IConnectionMultiplexer`
which is added with service key.
([#1885](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1885))

## 1.0.0-rc9.15

Released 2024-Jun-18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class TracerProviderBuilderExtensions
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddRedisInstrumentation(
this TracerProviderBuilder builder)
=> AddRedisInstrumentation(builder, name: null, connection: null, configure: null);
=> AddRedisInstrumentation(builder, name: null, connection: null, configure: null, serviceKey: null);

/// <summary>
/// Enables automatic data collection of outgoing requests to Redis.
Expand All @@ -40,7 +40,42 @@ public static TracerProviderBuilder AddRedisInstrumentation(
{
Guard.ThrowIfNull(connection);

return AddRedisInstrumentation(builder, name: null, connection, configure: null);
return AddRedisInstrumentation(builder, name: null, connection, configure: null, serviceKey: null);
}

/// <summary>
/// Enables automatic data collection of outgoing requests to Redis.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="serviceKey">Optional service key used to retrieve the <see cref="IConnectionMultiplexer"/> to instrument from the <see cref="IServiceProvider" />.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddRedisInstrumentation(
this TracerProviderBuilder builder,
object serviceKey)
{
Guard.ThrowIfNull(serviceKey);

return AddRedisInstrumentation(builder, name: null, connection: null, serviceKey, configure: null);
}

/// <summary>
/// Enables automatic data collection of outgoing requests to Redis.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="serviceKey">Optional service key used to retrieve the <see cref="IConnectionMultiplexer"/> to instrument from the <see cref="IServiceProvider" />.</param>
/// <param name="configure">Callback to configure options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddRedisInstrumentation(
Comment on lines +52 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these new overloads have tests covering them?

this TracerProviderBuilder builder,
string? name,
object serviceKey,
Action<StackExchangeRedisInstrumentationOptions> configure)
{
Guard.ThrowIfNull(serviceKey);
Guard.ThrowIfNull(configure);

return AddRedisInstrumentation(builder, name: name, connection: null, serviceKey, configure);
}

/// <summary>
Expand All @@ -54,12 +89,12 @@ public static TracerProviderBuilder AddRedisInstrumentation(
/// <param name="configure">Callback to configure options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddRedisInstrumentation(
this TracerProviderBuilder builder,
Action<StackExchangeRedisInstrumentationOptions> configure)
this TracerProviderBuilder builder,
Action<StackExchangeRedisInstrumentationOptions> configure)
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
{
Guard.ThrowIfNull(configure);

return AddRedisInstrumentation(builder, name: null, connection: null, configure);
return AddRedisInstrumentation(builder, name: null, connection: null, serviceKey: null, configure);
}

/// <summary>
Expand All @@ -77,7 +112,7 @@ public static TracerProviderBuilder AddRedisInstrumentation(
Guard.ThrowIfNull(connection);
Guard.ThrowIfNull(configure);

return AddRedisInstrumentation(builder, name: null, connection, configure);
return AddRedisInstrumentation(builder, name: null, connection, serviceKey: null, configure);
}

/// <summary>
Expand All @@ -91,12 +126,14 @@ public static TracerProviderBuilder AddRedisInstrumentation(
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="connection">Optional <see cref="IConnectionMultiplexer"/> to instrument.</param>
/// <param name="serviceKey">Optional service key used to retrieve the <see cref="IConnectionMultiplexer"/> to instrument from the <see cref="IServiceProvider" />.</param>
/// <param name="configure">Optional callback to configure options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddRedisInstrumentation(
this TracerProviderBuilder builder,
string? name,
IConnectionMultiplexer? connection,
object? serviceKey,
Action<StackExchangeRedisInstrumentationOptions>? configure)
{
Guard.ThrowIfNull(builder);
Expand All @@ -119,7 +156,9 @@ public static TracerProviderBuilder AddRedisInstrumentation(
{
var instrumentation = sp.GetRequiredService<StackExchangeRedisInstrumentation>();

connection ??= sp.GetService<IConnectionMultiplexer>();
connection ??= serviceKey == null
? sp.GetService<IConnectionMultiplexer>()
: sp.GetKeyedService<IConnectionMultiplexer>(serviceKey);

if (connection != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public void SuccessfulCommandTestWithKey(string value)

[Trait("CategoryName", "RedisIntegrationTests")]
[SkipUnlessEnvVarFoundTheory(RedisEndPointEnvVarName)]
[InlineData("value1")]
public void SuccessfulCommandTest(string value)
[InlineData("value1", null)]
[InlineData("value1", "serviceKey")]
public void SuccessfulCommandTest(string value, string? serviceKey)
{
var connectionOptions = new ConfigurationOptions
{
Expand All @@ -97,14 +98,24 @@ public void SuccessfulCommandTest(string value)
using (Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
services.TryAddSingleton<IConnectionMultiplexer>(sp =>
if (serviceKey is null)
{
services.TryAddSingleton<IConnectionMultiplexer>(sp =>
{
return connection = ConnectionMultiplexer.Connect(connectionOptions);
});
}
else
{
return connection = ConnectionMultiplexer.Connect(connectionOptions);
});
services.TryAddKeyedSingleton<IConnectionMultiplexer>(serviceKey, (sp, key) =>
{
return connection = ConnectionMultiplexer.Connect(connectionOptions);
});
}
})
.AddInMemoryExporter(exportedItems)
.SetSampler(sampler)
.AddRedisInstrumentation(c => c.SetVerboseDatabaseStatements = false)
.AddRedisInstrumentation(null, null, serviceKey, c => c.SetVerboseDatabaseStatements = false)
.Build())
{
Assert.NotNull(connection);
Expand Down
Loading