diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-18 08:56:51 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-18 08:56:51 -0600 |
| commit | c6b61a76059170875df06bb4281d72a84f004fb9 (patch) | |
| tree | fe12ecb32cd789148a91341e6a90406122f9b507 /src | |
| parent | fbad7874abc8717dec820980eacce8dd22e8a932 (diff) | |
| parent | 1378c0498a2206ebc0cc39045f6f1bc330a22ce6 (diff) | |
| download | slskd-c6b61a76059170875df06bb4281d72a84f004fb9.tar.gz | |
Merge pull request #1588 from slskd/metrics
Add Prometheus metrics for enqueue operations
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 42 | ||||
| -rw-r--r-- | src/slskd/Telemetry/Metrics.cs | 58 |
2 files changed, 89 insertions, 11 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 1a18790f..97d3392c 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -40,7 +40,6 @@ namespace slskd using Serilog.Events; using slskd.Configuration; using slskd.Core.API; - using slskd.Events; using slskd.Files; using slskd.Integrations.Pushbullet; using slskd.Messaging; @@ -83,6 +82,12 @@ namespace slskd private static readonly string ApplicationShutdownTransferExceptionMessage = "Application shut down"; +#pragma warning disable SA1306 // Field names should begin with lower-case letter + + private static int EnqueueRequestQueueDepth = 0; +#pragma warning restore SA1306 // Field names should begin with lower-case letter + + public Application( OptionsAtStartup optionsAtStartup, IOptionsMonitor<Options> optionsMonitor, @@ -537,20 +542,28 @@ namespace slskd // todo: consider moving this somewhere else; it's pretty long and complicated private async Task EnqueueDownload(string username, IPEndPoint endpoint, string filename) { + Metrics.Enqueue.RequestsReceived.Inc(1); + + if (Users.IsBlacklisted(username, endpoint.Address)) + { + Log.Information("Rejected enqueue request for blacklisted user {Username} ({IP})", username, endpoint.Address); + throw new DownloadEnqueueException("File not shared."); + } + var stopwatch = new Stopwatch(); var decisionStopwatch = new Stopwatch(); + Interlocked.Increment(ref EnqueueRequestQueueDepth); + await EnqueueRequestRateLimiter.WaitAsync(); try { - stopwatch.Start(); + Interlocked.Decrement(ref EnqueueRequestQueueDepth); - if (Users.IsBlacklisted(username, endpoint.Address)) - { - Log.Information("Rejected enqueue request for blacklisted user {Username} ({IP})", username, endpoint.Address); - throw new DownloadEnqueueException("File not shared."); - } + Metrics.Enqueue.CurrentRequestQueueDepth.Set(EnqueueRequestQueueDepth); + + stopwatch.Start(); // in order to properly determine if the requested file would exceed any limits, we need to know the size of the file // it helps, too, that this will tell us whether the file is even shared. @@ -747,12 +760,21 @@ namespace slskd } await Transfers.Uploads.EnqueueAsync(username, filename); - } - finally - { + + Metrics.Enqueue.RequestsAccepted.Inc(1); + decisionStopwatch.Stop(); + + Metrics.Enqueue.DecisionLatency.Observe(decisionStopwatch.ElapsedMilliseconds); + Metrics.Enqueue.CurrentDecisionLatency.Update(decisionStopwatch.ElapsedMilliseconds); + stopwatch.Stop(); + Metrics.Enqueue.ResponseLatency.Observe(stopwatch.ElapsedMilliseconds); + Metrics.Enqueue.CurrentResponseLatency.Update(stopwatch.ElapsedMilliseconds); + } + finally + { EnqueueRequestRateLimiter.Release(); Log.Debug("EnqueueDownload for {Username}/{Filename} completed in {ElapsedOverall}ms, decision made in {ElapsedDecision}ms", username, filename, stopwatch.ElapsedMilliseconds, decisionStopwatch.ElapsedMilliseconds); diff --git a/src/slskd/Telemetry/Metrics.cs b/src/slskd/Telemetry/Metrics.cs index 7bc6fd25..33d2bc40 100644 --- a/src/slskd/Telemetry/Metrics.cs +++ b/src/slskd/Telemetry/Metrics.cs @@ -83,7 +83,7 @@ public static class Metrics "The time taken to resolve a response to an incoming browse request, in milliseconds", new HistogramConfiguration { - Buckets = Histogram.ExponentialBuckets(1, 2, 10), + Buckets = Histogram.ExponentialBuckets(start: 1, factor: 2, count: 10), }); /// <summary> @@ -105,6 +105,62 @@ public static class Metrics } /// <summary> + /// Metrics related to incoming enqueue requests and responses. + /// </summary> + public static class Enqueue + { + /// <summary> + /// Gets a histogram representing the time taken to evaluate an incoming request to enqueue a file against configured limits, in milliseconds. + /// </summary> + public static Histogram DecisionLatency { get; } = Prometheus.Metrics.CreateHistogram( + "slskd_enqueue_decision_latency", + "The time taken to evaluate an incoming request to enqueue a file against configured limits, in milliseconds", + new HistogramConfiguration + { + Buckets = Histogram.ExponentialBuckets(start: 1, factor: 2, count: 16), // up to ~2 seconds + }); + + /// <summary> + /// Gets an EMA representing the average time taken to evaluate an incoming request to enqueue a file against configured limits, in milliseconds. + /// </summary> + public static ExponentialMovingAverage CurrentDecisionLatency { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentDecisionLatencyGauge.Set(value)); + + /// <summary> + /// Gets a histogram representing the total time taken to resolve a response to an incoming request to enqueue a file, in milliseconds. + /// </summary> + public static Histogram ResponseLatency { get; } = Prometheus.Metrics.CreateHistogram( + "slskd_enqueue_response_latency", + "The total time taken to resolve a response to an incoming request to enqueue a file, in milliseconds", + new HistogramConfiguration + { + Buckets = Histogram.ExponentialBuckets(start: 1, factor: 2, count: 16), // up to ~2 seconds + }); + + /// <summary> + /// Gets an EMA representing the average total time taken to resolve a response to an incoming request to enqueue a file, in milliseconds. + /// </summary> + public static ExponentialMovingAverage CurrentResponseLatency { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentResponseLatencyGauge.Set(value)); + + /// <summary> + /// Gets a counter representing the total number of incoming enqueue requests received. + /// </summary> + public static Counter RequestsReceived { get; } = Prometheus.Metrics.CreateCounter("slskd_enqueue_requests_received", "Total number of incoming enqueue requests received"); + + /// <summary> + /// Gets a counter representing the total number of incoming enqueue requests accepted. + /// </summary> + public static Counter RequestsAccepted { get; } = Prometheus.Metrics.CreateCounter("slskd_enqueue_requests_accepted", "Total number of incoming enqueue requests accepted"); + + /// <summary> + /// Gets a gauge representing the number of incoming enqueue requests waiting to be processed. + /// </summary> + public static Gauge CurrentRequestQueueDepth { get; } = Prometheus.Metrics.CreateGauge("slskd_enqueue_request_queue_depth_current", "The number of incoming enqueue requests waiting to be processed"); + + private static Gauge CurrentDecisionLatencyGauge { get; } = Prometheus.Metrics.CreateGauge("slskd_enqueue_decision_latency_current", "The average time taken to evaluate an incoming request to enqueue a file against configured limits, in milliseconds"); + private static Gauge CurrentResponseLatencyGauge { get; } = Prometheus.Metrics.CreateGauge("slskd_enqueue_response_latency_current", "The average total time taken to resolve a response to an incoming request to enqueue a file, in milliseconds"); + } + + /// <summary> /// Metrics related to the distributed network. /// </summary> public static class DistributedNetwork |
