-
Notifications
You must be signed in to change notification settings - Fork 480
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
Support redis instrumentation for keyed service #1157
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2289ab7
Support redis instrumentation for keyed service
Kahbazi 5de74ec
Use the extension method
Kahbazi 8944e40
Use UnsafeAccessor
Kahbazi a28b83b
Review Feedback
Kahbazi c1ad49d
Merge remote-tracking branch 'upstream/main' into kahbazi/RedisKeyedS…
eerhardt afef485
Add end-to-end activity test for keyed redis tracing.
eerhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Components/Aspire.StackExchange.Redis/TempOpenTelemetry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
using OpenTelemetry.Instrumentation.StackExchangeRedis; | ||
using OpenTelemetry.Trace; | ||
using StackExchange.Redis; | ||
|
||
namespace Aspire.StackExchange.Redis; | ||
|
||
// This is a temporary solution to add instrumentation for keyed service. | ||
// This class needs to get removed once this issue got solved and published. | ||
// https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1451 | ||
|
||
// This code has been copied from here | ||
//https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/c3a4f335f5a1f4fc784d44cc99b5bf44e872a66c/src/OpenTelemetry.Instrumentation.StackExchangeRedis/TracerProviderBuilderExtensions.cs#L109 | ||
internal static class TempOpenTelemetry | ||
{ | ||
public static TracerProviderBuilder AddRedisInstrumentationWithKeyedService( | ||
this TracerProviderBuilder builder, | ||
object serviceKey) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
|
||
builder.ConfigureServices(services => | ||
{ | ||
services.TryAddSingleton(sp => Create(sp)); | ||
}); | ||
|
||
return builder | ||
.AddSource("OpenTelemetry.Instrumentation.StackExchangeRedis") | ||
.AddInstrumentation(sp => | ||
{ | ||
var instrumentation = sp.GetRequiredService<StackExchangeRedisInstrumentation>(); | ||
|
||
var connection = sp.GetKeyedService<IConnectionMultiplexer>(serviceKey); | ||
|
||
if (connection != null) | ||
{ | ||
instrumentation.AddConnection(Options.DefaultName, connection); | ||
} | ||
|
||
return instrumentation; | ||
}); | ||
} | ||
|
||
private static StackExchangeRedisInstrumentation Create(IServiceProvider sp) | ||
{ | ||
// StackExchangeRedisInstrumentation has an internal constructor | ||
var instrumentation = Activator.CreateInstance( | ||
typeof(StackExchangeRedisInstrumentation), | ||
BindingFlags.NonPublic | BindingFlags.Instance, | ||
null, | ||
new object[] { sp.GetRequiredService<IOptionsMonitor<StackExchangeRedisInstrumentationOptions>>() }, | ||
null) as StackExchangeRedisInstrumentation; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use UnsafeAccessor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
return instrumentation!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
? What happens if there are more than 1 keyed Redis connections?
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.
The current implementation for non-keyed redis connection uses Options.DefaultName I wanted to keep the implementation consistence with it.
I think the
name
is used to get named options forStackExchangeRedisInstrumentationOptions
https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/fbfd66495359a88738bc0f5611bc7728c568ce5a/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisInstrumentation.cs#L44
and for the
Thread
name responsible for creating redisActivity
.https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/fbfd66495359a88738bc0f5611bc7728c568ce5a/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisConnectionInstrumentation.cs#L56
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 is the part I was missing.
I think this fine for now, but when we take open-telemetry/opentelemetry-dotnet-contrib#1457, we should probably be specifying the name, so someone can configure these options separately from the other Redis connections.