-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace Hanekawa.Application; | ||
|
||
public class Metrics<T> where T : class | ||
{ | ||
private readonly Counter<long> _counter; | ||
private readonly Histogram<double> _histogram; | ||
|
||
public Metrics(IMeterFactory meterFactory) | ||
{ | ||
var meter = meterFactory.Create(nameof(T)); | ||
|
||
_counter = meter.CreateCounter<long>($"hanekawa.{nameof(T)}.request.counter"); | ||
_histogram = meter.CreateHistogram<double>($"hanekawa.{nameof(T)}.request.duration"); | ||
} | ||
|
||
public void IncrementCounter() => _counter.Add(1); | ||
public TrackedDuration MeasureDuration() => new(TimeProvider.System, _histogram); | ||
} | ||
|
||
public class TrackedDuration : IDisposable | ||
{ | ||
private readonly long _requestStartTime; | ||
private readonly Histogram<double> _histogram; | ||
private readonly TimeProvider _timeProvider; | ||
|
||
public TrackedDuration(TimeProvider timeProvider, Histogram<double> histogram) | ||
{ | ||
_requestStartTime = timeProvider.GetTimestamp(); | ||
_timeProvider = timeProvider; | ||
_histogram = histogram; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
var elapsed = _timeProvider.GetElapsedTime(_requestStartTime); | ||
_histogram.Record(elapsed.TotalMilliseconds); | ||
} | ||
} |
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
Oops, something went wrong.