diff options
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; + } } } |
