Skip to content

Commit 2be1478

Browse files
committed
Implement completed failed counters.
1 parent 75f0b1e commit 2be1478

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/StackExchange.Redis/Message.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ public void Complete()
374374

375375
// set the completion/performance data
376376
performance?.SetCompleted();
377+
#if NET6_0_OR_GREATER
378+
RedisMetrics.Instance.OnMessageComplete(currBox);
379+
#endif
377380

378381
currBox?.ActivateContinuations();
379382
}
@@ -441,7 +444,7 @@ internal static Message Create(int db, CommandFlags flags, RedisCommand command,
441444
3 => new CommandKeyKeyValueValueValueMessage(db, flags, command, key0, key1, values[0], values[1], values[2]),
442445
4 => new CommandKeyKeyValueValueValueValueMessage(db, flags, command, key0, key1, values[0], values[1], values[2], values[3]),
443446
5 => new CommandKeyKeyValueValueValueValueValueMessage(db, flags, command, key0, key1, values[0], values[1], values[2], values[3], values[4]),
444-
6 => new CommandKeyKeyValueValueValueValueValueValueMessage(db, flags, command, key0, key1, values[0], values[1], values[2], values[3],values[4],values[5]),
447+
6 => new CommandKeyKeyValueValueValueValueValueValueMessage(db, flags, command, key0, key1, values[0], values[1], values[2], values[3], values[4], values[5]),
445448
7 => new CommandKeyKeyValueValueValueValueValueValueValueMessage(db, flags, command, key0, key1, values[0], values[1], values[2], values[3], values[4], values[5], values[6]),
446449
_ => new CommandKeyKeyValuesMessage(db, flags, command, key0, key1, values),
447450
};

src/StackExchange.Redis/RedisMetrics.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,37 @@ internal sealed class RedisMetrics
99
{
1010
private readonly Meter _meter;
1111
private readonly Counter<long> _operationCount;
12-
//private readonly Counter<long> _completedAsynchronously;
13-
//private readonly Counter<long> _completedSynchronously;
14-
//private readonly Counter<long> _failedSynchronously;
12+
private readonly Counter<long> _completedAsynchronously;
13+
private readonly Counter<long> _completedSynchronously;
14+
private readonly Counter<long> _failedAsynchronously;
15+
private readonly Counter<long> _failedSynchronously;
1516
private readonly Counter<long> _nonPreferredEndpointCount;
1617

1718
public static readonly RedisMetrics Instance = new RedisMetrics();
1819

19-
public RedisMetrics()
20+
private RedisMetrics()
2021
{
2122
_meter = new Meter("StackExchange.Redis");
2223

2324
_operationCount = _meter.CreateCounter<long>(
2425
"redis-operation-count",
2526
description: "The number of operations performed.");
2627

27-
//_completedAsynchronously = _meter.CreateCounter<long>(
28-
// "redis-completed-asynchronously",
29-
// description: "The number of operations that have been completed asynchronously.");
28+
_completedAsynchronously = _meter.CreateCounter<long>(
29+
"redis-completed-asynchronously",
30+
description: "The number of operations that have been completed asynchronously.");
3031

31-
//_completedSynchronously = _meter.CreateCounter<long>(
32-
// "redis-completed-synchronously",
33-
// description: "The number of operations that have been completed synchronously.");
32+
_completedSynchronously = _meter.CreateCounter<long>(
33+
"redis-completed-synchronously",
34+
description: "The number of operations that have been completed synchronously.");
3435

35-
//_failedSynchronously = _meter.CreateCounter<long>(
36-
// "redis-failed-synchronously",
37-
// description: "The number of operations that failed to complete asynchronously.");
36+
_failedAsynchronously = _meter.CreateCounter<long>(
37+
"redis-failed-asynchronously",
38+
description: "The number of operations that failed to complete asynchronously.");
39+
40+
_failedSynchronously = _meter.CreateCounter<long>(
41+
"redis-failed-synchronously",
42+
description: "The number of operations that failed to complete synchronously.");
3843

3944
_nonPreferredEndpointCount = _meter.CreateCounter<long>(
4045
"redis-non-preferred-endpoint-count",
@@ -50,6 +55,27 @@ public void IncrementOperationCount(string endpoint)
5055
}
5156
}
5257

58+
public void OnMessageComplete(IResultBox? result)
59+
{
60+
if (result is not null &&
61+
(_completedAsynchronously.Enabled ||
62+
_completedSynchronously.Enabled ||
63+
_failedAsynchronously.Enabled ||
64+
_failedSynchronously.Enabled))
65+
{
66+
Counter<long> counter = (result.IsFaulted, result.IsAsync) switch
67+
{
68+
(false, true) => _completedAsynchronously,
69+
(false, false) => _completedSynchronously,
70+
(true, true) => _failedAsynchronously,
71+
(true, false) => _failedSynchronously,
72+
};
73+
74+
// TODO: can we pass endpoint here?
75+
counter.Add(1);
76+
}
77+
}
78+
5379
public void IncrementNonPreferredEndpointCount(string endpoint)
5480
{
5581
if (_nonPreferredEndpointCount.Enabled)

0 commit comments

Comments
 (0)