-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrated io metrics from Counter to ObservableCounter instrument
- Loading branch information
1 parent
e6d52cd
commit 2a1f904
Showing
7 changed files
with
67 additions
and
40 deletions.
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
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,27 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace EventStore.Core.Telemetry { | ||
public class CounterMetric { | ||
private readonly List<CounterSubMetric> _subMetrics = new(); | ||
private readonly object _lock = new(); | ||
|
||
public CounterMetric(Meter meter, string name, string unit = null) { | ||
meter.CreateObservableCounter(name, Observe, unit); | ||
} | ||
|
||
public void Add(CounterSubMetric subMetric) { | ||
lock (_lock) { | ||
_subMetrics.Add(subMetric); | ||
} | ||
} | ||
|
||
private IEnumerable<Measurement<long>> Observe() { | ||
lock (_lock) { | ||
foreach (CounterSubMetric subMetric in _subMetrics) { | ||
yield return subMetric.Observe(); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,24 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using System.Threading; | ||
|
||
namespace EventStore.Core.Telemetry { | ||
public class CounterSubMetric<T> where T : struct { | ||
private readonly Counter<T> _metric; | ||
private readonly KeyValuePair<string, object> _tag; | ||
namespace EventStore.Core.Telemetry; | ||
|
||
public CounterSubMetric(Counter<T> metric, KeyValuePair<string, object> tag) { | ||
_metric = metric; | ||
_tag = tag; | ||
} | ||
public class CounterSubMetric { | ||
private readonly KeyValuePair<string, object>[] _tags; | ||
private long _counter; | ||
|
||
public void Add(T delta) { | ||
_metric.Add(delta, _tag); | ||
} | ||
public CounterSubMetric(CounterMetric metric, KeyValuePair<string, object>[] tags) { | ||
_tags = tags; | ||
metric.Add(this); | ||
} | ||
|
||
public void Add(long delta) { | ||
Interlocked.Add(ref _counter, delta); | ||
} | ||
|
||
public Measurement<long> Observe() { | ||
return new Measurement<long>(Interlocked.Read(ref _counter), _tags); | ||
} | ||
} |
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