-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 Initial Metrics #2499
Draft
eerhardt
wants to merge
5
commits into
StackExchange:main
Choose a base branch
from
eerhardt:AddMetrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Initial Metrics #2499
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8dd98fa
Initial Metrics implementations
eerhardt fbafc9d
Add NonPreferredEndpointCount
eerhardt 8837b21
Implement completed failed counters.
eerhardt a926218
Rename the metrics following OTel naming
eerhardt 837345f
Refactor RedisMetrics to be instance based and allow tests to inject …
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
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
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,19 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
namespace StackExchange.Redis; | ||
|
||
public partial class ConnectionMultiplexer | ||
{ | ||
internal RedisMetrics Metrics { get; } | ||
|
||
private static RedisMetrics GetMetrics(ConfigurationOptions configuration) | ||
{ | ||
if (configuration.MeterFactory is not null) | ||
{ | ||
return new RedisMetrics(configuration.MeterFactory()); | ||
} | ||
|
||
return RedisMetrics.Default; | ||
} | ||
} | ||
#endif |
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
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
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
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,79 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace StackExchange.Redis; | ||
|
||
internal sealed class RedisMetrics | ||
{ | ||
private static readonly double s_tickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency; | ||
// cache these boxed boolean values so we don't allocate on each usage. | ||
private static readonly object s_trueBox = true; | ||
private static readonly object s_falseBox = false; | ||
|
||
private readonly Meter _meter; | ||
private readonly Counter<long> _operationCount; | ||
private readonly Histogram<double> _messageDuration; | ||
private readonly Counter<long> _nonPreferredEndpointCount; | ||
|
||
public static readonly RedisMetrics Default = new RedisMetrics(); | ||
|
||
public RedisMetrics(Meter? meter = null) | ||
{ | ||
_meter = meter ?? new Meter("StackExchange.Redis"); | ||
|
||
_operationCount = _meter.CreateCounter<long>( | ||
"db.redis.operation.count", | ||
description: "The number of operations performed."); | ||
|
||
_messageDuration = _meter.CreateHistogram<double>( | ||
"db.redis.duration", | ||
unit: "s", | ||
description: "Measures the duration of outbound message requests."); | ||
|
||
_nonPreferredEndpointCount = _meter.CreateCounter<long>( | ||
"db.redis.non_preferred_endpoint.count", | ||
description: "Indicates the total number of messages dispatched to a non-preferred endpoint, for example sent to a primary when the caller stated a preference of replica."); | ||
} | ||
|
||
public void IncrementOperationCount(string endpoint) | ||
{ | ||
_operationCount.Add(1, | ||
new KeyValuePair<string, object?>("endpoint", endpoint)); | ||
} | ||
|
||
public void OnMessageComplete(Message message, IResultBox? result) | ||
{ | ||
// The caller ensures we can don't record on the same resultBox from two threads. | ||
// 'result' can be null if this method is called for the same message more than once. | ||
if (result is not null && _messageDuration.Enabled) | ||
{ | ||
// Stopwatch.GetElapsedTime is only available in net7.0+ | ||
// https://github.com/dotnet/runtime/blob/ae068fec6ede58d2a5b343c5ac41c9ca8715fa47/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs#L129-L137 | ||
var now = Stopwatch.GetTimestamp(); | ||
var duration = new TimeSpan((long)((now - message.CreatedTimestamp) * s_tickFrequency)); | ||
|
||
var tags = new TagList | ||
{ | ||
{ "db.redis.async", result.IsAsync ? s_trueBox : s_falseBox }, | ||
{ "db.redis.faulted", result.IsFaulted ? s_trueBox : s_falseBox } | ||
// TODO: can we pass endpoint here? | ||
// should we log the Db? | ||
// { "db.redis.database_index", message.Db }, | ||
}; | ||
|
||
_messageDuration.Record(duration.TotalSeconds, tags); | ||
} | ||
} | ||
|
||
public void IncrementNonPreferredEndpointCount(string endpoint) | ||
{ | ||
_nonPreferredEndpointCount.Add(1, | ||
new KeyValuePair<string, object?>("endpoint", endpoint)); | ||
} | ||
} | ||
|
||
#endif |
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,44 @@ | ||
#if NET6_0_OR_GREATER | ||
#pragma warning disable TBD // MetricCollector is for evaluation purposes only and is subject to change or removal in future updates. | ||
|
||
using Microsoft.Extensions.Telemetry.Testing.Metering; | ||
using System.Diagnostics.Metrics; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace StackExchange.Redis.Tests; | ||
|
||
public class MetricsTests : TestBase | ||
{ | ||
public MetricsTests(ITestOutputHelper output) : base(output) { } | ||
|
||
[Fact] | ||
public async Task SimpleCommandDuration() | ||
{ | ||
var options = ConfigurationOptions.Parse(GetConfiguration()); | ||
|
||
using var meter = new Meter("StackExchange.Redis.Tests"); | ||
using var collector = new MetricCollector<double>(meter, "db.redis.duration"); | ||
|
||
options.MeterFactory = () => meter; | ||
|
||
using var conn = await ConnectionMultiplexer.ConnectAsync(options, Writer); | ||
var db = conn.GetDatabase(); | ||
|
||
RedisKey key = Me(); | ||
string? g1 = await db.StringGetAsync(key); | ||
Assert.Null(g1); | ||
|
||
await collector.WaitForMeasurementsAsync(1); | ||
|
||
Assert.Collection(collector.GetMeasurementSnapshot(), | ||
measurement => | ||
{ | ||
// Built-in | ||
Assert.Equal(true, measurement.Tags["db.redis.async"]); | ||
Assert.Equal(false, measurement.Tags["db.redis.faulted"]); | ||
}); | ||
} | ||
} | ||
#endif |
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.
for upto 3 tags, its faster to pass them directly.
https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/metrics#instruments
When reporting measurements with 3 tags or less, pass the tags directly to the instrument API.