aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-08-02 18:11:02 -0500
committerJP Dillingham <jp@dillingham.me>2025-08-02 18:11:02 -0500
commit9fc1af5fbd7f3524bcdce3841f2ce4f129176953 (patch)
tree777920908a0b57fcbf33913c7347868b1970a775
parentda0386576b08237eb6ecf0fd115bcb9b27888398 (diff)
downloadslskd-9fc1af5fbd7f3524bcdce3841f2ce4f129176953.tar.gz
add real time count of search requests
-rw-r--r--src/slskd/Application.cs1
-rw-r--r--src/slskd/Common/TimedCounter.cs89
-rw-r--r--src/slskd/Core/Metrics.cs8
3 files changed, 98 insertions, 0 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 1532e33e..3040b106 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -1466,6 +1466,7 @@ namespace slskd
Metrics.Search.ResponseLatency.Observe(sw.ElapsedMilliseconds);
Metrics.Search.CurrentResponseLatency.Update(sw.ElapsedMilliseconds);
+ Metrics.Search.RequestCount.CountUp();
if (results.Any())
{
diff --git a/src/slskd/Common/TimedCounter.cs b/src/slskd/Common/TimedCounter.cs
new file mode 100644
index 00000000..c65ca7e3
--- /dev/null
+++ b/src/slskd/Common/TimedCounter.cs
@@ -0,0 +1,89 @@
+// <copyright file="TimedCounter.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+using System;
+using System.Timers;
+
+namespace slskd
+{
+ /// <summary>
+ /// A counter that counts up over a set time period.
+ /// </summary>
+ public class TimedCounter : IDisposable
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="TimedCounter"/> class.
+ /// </summary>
+ /// <param name="interval">The interval at which the OnElapsed delegate is called and the count resets.</param>
+ /// <param name="onElapsed">The delagate to invoke with the current count when the timer elapses.</param>
+ public TimedCounter(TimeSpan interval, Action<long> onElapsed)
+ {
+ OnElapsed = onElapsed;
+
+ Timer = new Timer(interval);
+ Timer.Elapsed += Elapsed;
+ Timer.Start();
+ }
+
+ /// <summary>
+ /// Gets the current count.
+ /// </summary>
+ public long Count { get; private set; } = 0;
+
+ private Timer Timer { get; }
+ private Action<long> OnElapsed { get; }
+ private bool Disposed { get; set; }
+
+ /// <summary>
+ /// Counts up by the specified <paramref name="count"/>.
+ /// </summary>
+ /// <param name="count">The number to add to the count.</param>
+ public void CountUp(long count = 1)
+ {
+ Count += count;
+ }
+
+ /// <summary>
+ /// Disposes this instance.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!Disposed)
+ {
+ if (disposing)
+ {
+ Timer.Dispose();
+ }
+
+ Disposed = true;
+ }
+ }
+
+ private void Elapsed(object sender, ElapsedEventArgs args)
+ {
+ var count = Count;
+ Count = 0;
+ OnElapsed?.Invoke(count);
+ }
+ }
+}
diff --git a/src/slskd/Core/Metrics.cs b/src/slskd/Core/Metrics.cs
index 7e0b0f6e..67de4cb8 100644
--- a/src/slskd/Core/Metrics.cs
+++ b/src/slskd/Core/Metrics.cs
@@ -17,6 +17,8 @@
namespace slskd
{
+ using System;
+
using System.IO;
using System.Threading.Tasks;
using Prometheus;
@@ -57,6 +59,11 @@ namespace slskd
public static ExponentialMovingAverage CurrentResponseLatency { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentResponseLatencyGauge.Set(value));
/// <summary>
+ /// Gets an automatically resetting counter of the number of search requests received.
+ /// </summary>
+ public static TimedCounter RequestCount { get; } = new TimedCounter(TimeSpan.FromSeconds(1), onElapsed: count => CurrentRequestRateGauge.Set(count));
+
+ /// <summary>
/// Gets a counter representing the total number of search requests received.
/// </summary>
public static Counter RequestsReceived { get; } = Prometheus.Metrics.CreateCounter("slskd_search_requests_received", "Total number of search requests received");
@@ -67,6 +74,7 @@ namespace slskd
public static Counter ResponsesSent { get; } = Prometheus.Metrics.CreateCounter("slskd_search_responses_sent", "Total number of search responses sent");
private static Gauge CurrentResponseLatencyGauge { get; } = Prometheus.Metrics.CreateGauge("slskd_search_response_latency_current", "The average time taken to resolve a response to an incoming search request, in milliseconds");
+ private static Gauge CurrentRequestRateGauge { get; } = Prometheus.Metrics.CreateGauge("slskd_search_request_rate_current", "Number of search requests received in the last second");
}
public static class Browse