diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-08-25 22:01:50 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-08-25 22:01:50 -0500 |
| commit | c9ae44e33e3d8b7b8ab8b11f3f094e05dfe9fea5 (patch) | |
| tree | 2684588297fbabb34d4d8187b289ef377bb41099 /src | |
| parent | 339086d048cbb2ce224a6b6eef2ce0e10be42a34 (diff) | |
| download | slskd-c9ae44e33e3d8b7b8ab8b11f3f094e05dfe9fea5.tar.gz | |
add Measure() overloads
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Metrics.cs | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/src/slskd/Core/Metrics.cs b/src/slskd/Core/Metrics.cs index 51fc75db..e3fbfe01 100644 --- a/src/slskd/Core/Metrics.cs +++ b/src/slskd/Core/Metrics.cs @@ -1,18 +1,55 @@ -using Prometheus; +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using Prometheus; namespace slskd { public static class Metrics { public static Histogram SearchResponseLatency { get; } = Prometheus.Metrics - .CreateHistogram("slskd_search_response_latency", "Histogram of received order values (in USD).", + .CreateHistogram($"slskd_search_response_latency", "The time taken to resolve a response to an incoming search request, in milliseconds", new HistogramConfiguration { - // We divide measurements in 10 buckets of $100 each, up to $1000. Buckets = Histogram.ExponentialBuckets(1, 2, 10), }); - public static Counter SearchRequestsReceived { get; } = Prometheus.Metrics.CreateCounter("slskd_search_requests_received", "total number of search requests"); - public static Counter SearchResponsesSent { get; } = Prometheus.Metrics.CreateCounter("slskd_search_responses_sent", "total number of search responses sent"); + public static Counter SearchRequestsReceived { get; } = Prometheus.Metrics.CreateCounter("slskd_search_requests_received", "Total number of search requests received"); + public static Counter SearchResponsesSent { get; } = Prometheus.Metrics.CreateCounter("slskd_search_responses_sent", "Total number of search responses sent"); + + public static Counter BrowseResponsesSent { get; } = Prometheus.Metrics.CreateCounter("slskd_browse_responses_sent", "Total number of browse responses sent"); + public static Histogram BrowseResponseLatency { get; } = Prometheus.Metrics.CreateHistogram("slskd_browse_response_latency", "The time taken to resolve a response to an incoming browse request, in milliseconds", + new HistogramConfiguration + { + Buckets = Histogram.ExponentialBuckets(1, 2, 10), + }); + + public static T Measure<T>(Histogram histogram, Func<T> action) + { + var sw = new Stopwatch(); + sw.Start(); + + var result = action(); + + sw.Stop(); + + histogram.Observe(sw.ElapsedMilliseconds); + + return result; + } + + public static async Task<T> MeasureAsync<T>(Histogram histogram, Func<Task<T>> action) + { + var sw = new Stopwatch(); + sw.Start(); + + var result = await action(); + + sw.Stop(); + + histogram.Observe(sw.ElapsedMilliseconds); + + return result; + } } } |
