diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-21 10:41:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-06-21 10:41:53 -0500 |
| commit | 7497db57e8a44769d1a098ad189247ea4e980a1d (patch) | |
| tree | 189ff962d5125fe2526e4f79dccb009379725fdf /src/slskd/Telemetry | |
| parent | 8e152ed6eccd5650179f2875eef4aa21243c8c15 (diff) | |
| parent | 28fa7202724da10075dbddc3244d5d5528b4bdad (diff) | |
| download | slskd-7497db57e8a44769d1a098ad189247ea4e980a1d.tar.gz | |
Merge pull request #1769 from slskd/reports
Collect and broadcast real-time transfer metrics, add a footer to the UI to display them
Diffstat (limited to 'src/slskd/Telemetry')
| -rw-r--r-- | src/slskd/Telemetry/API/MetricsHub.cs | 60 | ||||
| -rw-r--r-- | src/slskd/Telemetry/Metrics.cs | 316 | ||||
| -rw-r--r-- | src/slskd/Telemetry/MetricsService.cs | 96 | ||||
| -rw-r--r-- | src/slskd/Telemetry/TelemetryService.cs | 18 |
4 files changed, 489 insertions, 1 deletions
diff --git a/src/slskd/Telemetry/API/MetricsHub.cs b/src/slskd/Telemetry/API/MetricsHub.cs new file mode 100644 index 00000000..5dcd4ecc --- /dev/null +++ b/src/slskd/Telemetry/API/MetricsHub.cs @@ -0,0 +1,60 @@ +// <copyright file="MetricsHub.cs" company="JP Dillingham"> +// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ +// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ +// █__ --█ █__ --█ ◄█ - █ +// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ +// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ +// │ Copyright (c) JP Dillingham. +// │ +// │ 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, version 3. +// │ +// │ 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/. +// │ +// │ This program is distributed with Additional Terms pursuant to Section 7 +// │ of the AGPLv3. See the LICENSE file in the root directory of this +// │ project for the complete terms and conditions. +// │ +// │ https://slskd.org +// │ +// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ +// │ SPDX-FileCopyrightText: JP Dillingham +// │ SPDX-License-Identifier: AGPL-3.0-only +// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ +// </copyright> + +namespace slskd.Telemetry; + +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.SignalR; +using Serilog; + +public interface IMetricsHub +{ + Task Update(object metrics); +} + +[Authorize] +public class MetricsHub : Hub<IMetricsHub> +{ + public MetricsHub(MetricsService metricsService) + { + Metrics = metricsService; + } + + private static ILogger Log { get; } = Serilog.Log.ForContext<MetricsHub>(); + private MetricsService Metrics { get; } + + public override async Task OnConnectedAsync() + { + await Clients.Caller.Update(Metrics.Current()); + } +}
\ No newline at end of file diff --git a/src/slskd/Telemetry/Metrics.cs b/src/slskd/Telemetry/Metrics.cs index 33e16873..822f6aeb 100644 --- a/src/slskd/Telemetry/Metrics.cs +++ b/src/slskd/Telemetry/Metrics.cs @@ -33,6 +33,7 @@ namespace slskd.Telemetry; using System; +using System.Threading; using Prometheus; /// <summary> @@ -45,6 +46,31 @@ using Prometheus; /// </remarks> public static class Metrics { + private static SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + + /// <summary> + /// Updates metrics under a mutex to prevent mismatches and under/overruns. + /// </summary> + /// <remarks> + /// This method uses a <see cref="SemaphoreSlim"/> for synchronization and it is therefore safe to do async work + /// inside of <paramref name="work"/>, though it is discouraged for causing un-needed contention. + /// </remarks> + /// <param name="work">Updates to execute.</param> + /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> + public static void Update(Action work, CancellationToken cancellationToken = default) + { + SyncRoot.Wait(cancellationToken); + + try + { + work.Invoke(); + } + finally + { + SyncRoot.Release(); + } + } + /// <summary> /// Metrics related to search requests and responses. /// </summary> @@ -265,6 +291,296 @@ public static class Metrics } /// <summary> + /// Metrics related to transfers. + /// </summary> + public static class Transfers + { + /// <summary> + /// Metrics related to uploads. + /// </summary> + public static class Uploads + { + /// <summary> + /// Metrics related to in-progress uploads. + /// </summary> + public static class InProgress + { + /// <summary> + /// Gets a gauge representing the current number of unique users with in-progress uploads. + /// </summary> + public static Gauge Users { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_in_progress_users_current", + help: "Current number of unique users with in-progress uploads"); + + /// <summary> + /// Gets a gauge representing the current number of in-progress uploads. + /// </summary> + public static Gauge Files { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_in_progress_files_current", + help: "Current number of in-progress uploads"); + + /// <summary> + /// Gets a gauge representing the current total size, in bytes, of in-progress uploads. + /// </summary> + public static Gauge Bytes { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_in_progress_bytes_current", + help: "Current total size, in bytes, of in-progress uploads"); + + /// <summary> + /// Gets a gauge representing the current total speed, in bytes per second, of in-progress uploads. + /// </summary> + public static ExponentialMovingAverage CurrentTotalSpeed { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentTotalSpeedGauge.Set(value)); + public static Gauge CurrentTotalSpeedGauge { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_in_progress_speed_total_current", + help: "Current total speed, in bytes per second, of in-progress uploads"); + + /// <summary> + /// Gets a gauge representing the current average speed, in bytes per second, of in-progress uploads. + /// </summary> + public static ExponentialMovingAverage CurrentAverageSpeed { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentAverageSpeedGauge.Set(value)); + public static Gauge CurrentAverageSpeedGauge { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_in_progress_speed_average_current", + help: "Current average speed, in bytes per second, of in-progress uploads"); + } + + /// <summary> + /// Metrics related to queued uploads. + /// </summary> + public static class Queued + { + /// <summary> + /// Gets a gauge representing the current number of unique users with queued uploads. + /// </summary> + public static Gauge Users { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_queued_users_current", + help: "Current number of unique users with queued uploads"); + + /// <summary> + /// Gets a gauge representing the current number of queued uploads. + /// </summary> + public static Gauge Files { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_queued_files_current", + help: "Current number of queued uploads"); + + /// <summary> + /// Gets a gauge representing the current total size, in bytes, of queued uploads. + /// </summary> + public static Gauge Bytes { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_queued_bytes_current", + help: "Current total size, in bytes, of queued uploads"); + } + + /// <summary> + /// Metrics related to completed uploads. + /// </summary> + public static class Completed + { + /// <summary> + /// Gets a counter representing the total number of uploads that completed successfully. + /// </summary> + public static Counter Succeeded { get; } = Prometheus.Metrics.CreateCounter( + name: "slskd_transfers_uploads_completed_succeeded", + help: "Total number of uploads that completed successfully"); + + /// <summary> + /// Gets a counter representing the total number of bytes transferred by successfully completed uploads. + /// </summary> + public static Counter Bytes { get; } = Prometheus.Metrics.CreateCounter( + name: "slskd_transfers_uploads_completed_bytes", + help: "Total number of bytes transferred by successfully completed uploads"); + + /// <summary> + /// Gets a counter representing the total number of uploads that failed. + /// </summary> + public static Counter Failed { get; } = Prometheus.Metrics.CreateCounter( + name: "slskd_transfers_uploads_completed_failed", + help: "Total number of uploads that failed"); + + /// <summary> + /// Gets a gauge representing the current average speed, in bytes per second, of completed downloads. + /// </summary> + public static ExponentialMovingAverage CurrentAverageSpeed { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentAverageSpeedGauge.Set(value)); + public static Gauge CurrentAverageSpeedGauge { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_uploads_completed_speed_average_current", + help: "Current average speed, in bytes per second, of completed uploads"); + + /// <summary> + /// Gets a histogram representing the the average speed of completed uploads, in bytes per second. + /// </summary> + public static Histogram AverageSpeed { get; } = Prometheus.Metrics.CreateHistogram( + name: "slskd_transfers_uploads_completed_speed_average", + help: "The average speed of completed uploads, in bytes per second", + new HistogramConfiguration + { + Buckets = [ + 1_024, // 1 KB/s + 10_240, // 10 KB/s + 51_200, // 50 KB/s + 102_400, // 100 KB/s + 204_800, // 200 KB/s + 307_200, // 300 KB/s + 409_600, // 400 KB/s + 512_000, // 500 KB/s + 1_048_576, // 1 MB/s + 2_097_152, // 2 MB/s + 4_194_304, // 4 MB/s + 6_291_456, // 6 MB/s + 10_485_760, // 10 MB/s + 26_214_400, // 25 MB/s + 52_428_800, // 50 MB/s + 104_857_600, // 100 MB/s + 262_144_000, // 250 MB/s + 524_288_000, // 500 MB/s + 1_073_741_824, // 1 GB/s + ], + }); + } + } + + /// <summary> + /// Metrics related to downloads. + /// </summary> + public static class Downloads + { + /// <summary> + /// Metrics related to in-progress downloads. + /// </summary> + public static class InProgress + { + /// <summary> + /// Gets a gauge representing the current number of unique users with in-progress downloads. + /// </summary> + public static Gauge Users { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_in_progress_users_current", + help: "Current number of unique users with in-progress downloads"); + + /// <summary> + /// Gets a gauge representing the current number of in-progress downloads. + /// </summary> + public static Gauge Files { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_in_progress_files_current", + help: "Current number of in-progress downloads"); + + /// <summary> + /// Gets a gauge representing the current total size, in bytes, of in-progress downloads. + /// </summary> + public static Gauge Bytes { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_in_progress_bytes_current", + help: "Current total size, in bytes, of in-progress downloads"); + + /// <summary> + /// Gets a gauge representing the current total speed, in bytes per second, of in-progress downloads. + /// </summary> + public static ExponentialMovingAverage CurrentTotalSpeed { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentTotalSpeedGauge.Set(value)); + public static Gauge CurrentTotalSpeedGauge { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_in_progress_speed_total_current", + help: "Current total speed, in bytes per second, of in-progress downloads"); + + /// <summary> + /// Gets a gauge representing the current average speed, in bytes per second, of in-progress downloads. + /// </summary> + public static ExponentialMovingAverage CurrentAverageSpeed { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentAverageSpeedGauge.Set(value)); + public static Gauge CurrentAverageSpeedGauge { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_in_progress_speed_average_current", + help: "Current average speed, in bytes per second, of in-progress downloads"); + } + + /// <summary> + /// Metrics related to queued downloads. + /// </summary> + public static class Queued + { + /// <summary> + /// Gets a gauge representing the current number of unique users with queued downloads. + /// </summary> + public static Gauge Users { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_queued_users_current", + help: "Current number of unique users with queued downloads"); + + /// <summary> + /// Gets a gauge representing the current number of queued downloads. + /// </summary> + public static Gauge Files { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_queued_files_current", + help: "Current number of queued downloads"); + + /// <summary> + /// Gets a gauge representing the current total size, in bytes, of queued downloads. + /// </summary> + public static Gauge Bytes { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_queued_bytes_current", + help: "Current total size, in bytes, of queued downloads"); + } + + /// <summary> + /// Metrics related to completed downloads. + /// </summary> + public static class Completed + { + /// <summary> + /// Gets a counter representing the total number of downloads that completed successfully. + /// </summary> + public static Counter Succeeded { get; } = Prometheus.Metrics.CreateCounter( + name: "slskd_transfers_downloads_completed_succeeded", + help: "Total number of downloads that completed successfully"); + + /// <summary> + /// Gets a counter representing the total number of bytes transferred by successfully completed downloads. + /// </summary> + public static Counter Bytes { get; } = Prometheus.Metrics.CreateCounter( + name: "slskd_transfers_downloads_completed_bytes", + help: "Total number of bytes transferred by successfully completed downloads"); + + /// <summary> + /// Gets a counter representing the total number of downloads that failed. + /// </summary> + public static Counter Failed { get; } = Prometheus.Metrics.CreateCounter( + name: "slskd_transfers_downloads_completed_failed", + help: "Total number of downloads that failed"); + + /// <summary> + /// Gets a gauge representing the current average speed, in bytes per second, of completed downloads. + /// </summary> + public static ExponentialMovingAverage CurrentAverageSpeed { get; } = new ExponentialMovingAverage(smoothingFactor: 0.5, onUpdate: value => CurrentAverageSpeedGauge.Set(value)); + public static Gauge CurrentAverageSpeedGauge { get; } = Prometheus.Metrics.CreateGauge( + name: "slskd_transfers_downloads_completed_speed_average_current", + help: "Current average speed, in bytes per second, of completed downloads"); + + /// <summary> + /// Gets a histogram representing the the average speed of completed downloads, in bytes per second. + /// </summary> + public static Histogram AverageSpeed { get; } = Prometheus.Metrics.CreateHistogram( + name: "slskd_transfers_downloads_completed_speed_average", + help: "The average speed of completed downloads, in bytes per second", + new HistogramConfiguration + { + Buckets = [ + 1_024, // 1 KB/s + 10_240, // 10 KB/s + 51_200, // 50 KB/s + 102_400, // 100 KB/s + 204_800, // 200 KB/s + 307_200, // 300 KB/s + 409_600, // 400 KB/s + 512_000, // 500 KB/s + 1_048_576, // 1 MB/s + 2_097_152, // 2 MB/s + 4_194_304, // 4 MB/s + 6_291_456, // 6 MB/s + 10_485_760, // 10 MB/s + 26_214_400, // 25 MB/s + 52_428_800, // 50 MB/s + 104_857_600, // 100 MB/s + 262_144_000, // 250 MB/s + 524_288_000, // 500 MB/s + 1_073_741_824, // 1 GB/s + ], + }); + } + } + } + + /// <summary> /// Metrics related to the distributed network. /// </summary> public static class DistributedNetwork diff --git a/src/slskd/Telemetry/MetricsService.cs b/src/slskd/Telemetry/MetricsService.cs new file mode 100644 index 00000000..1c69ef85 --- /dev/null +++ b/src/slskd/Telemetry/MetricsService.cs @@ -0,0 +1,96 @@ +// <copyright file="MetricsService.cs" company="JP Dillingham"> +// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ +// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ +// █__ --█ █__ --█ ◄█ - █ +// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ +// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ +// │ Copyright (c) JP Dillingham. +// │ +// │ 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, version 3. +// │ +// │ 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/. +// │ +// │ This program is distributed with Additional Terms pursuant to Section 7 +// │ of the AGPLv3. See the LICENSE file in the root directory of this +// │ project for the complete terms and conditions. +// │ +// │ https://slskd.org +// │ +// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ +// │ SPDX-FileCopyrightText: JP Dillingham +// │ SPDX-License-Identifier: AGPL-3.0-only +// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ +// </copyright> + +namespace slskd.Telemetry; + +using Serilog; + +/// <summary> +/// Metrics. +/// </summary> +public class MetricsService +{ + private ILogger Log { get; } = Serilog.Log.ForContext<MetricsService>(); + + public object Current() + { + var metrics = new + { + Downloads = new + { + InProgress = new + { + Files = Metrics.Transfers.Downloads.InProgress.Files.Value, + Users = Metrics.Transfers.Downloads.InProgress.Users.Value, + AverageSpeed = Metrics.Transfers.Downloads.InProgress.CurrentAverageSpeed.Value, + TotalSpeed = Metrics.Transfers.Downloads.InProgress.CurrentTotalSpeed.Value, + }, + Queued = new + { + Files = Metrics.Transfers.Downloads.Queued.Files.Value, + Users = Metrics.Transfers.Downloads.Queued.Users.Value, + Bytes = Metrics.Transfers.Downloads.Queued.Bytes.Value, + }, + Completed = new + { + Succeeded = Metrics.Transfers.Downloads.Completed.Succeeded.Value, + Failed = Metrics.Transfers.Downloads.Completed.Failed.Value, + Bytes = Metrics.Transfers.Downloads.Completed.Bytes.Value, + }, + }, + Uploads = new + { + InProgress = new + { + Files = Metrics.Transfers.Uploads.InProgress.Files.Value, + Users = Metrics.Transfers.Uploads.InProgress.Users.Value, + AverageSpeed = Metrics.Transfers.Uploads.InProgress.CurrentAverageSpeed.Value, + TotalSpeed = Metrics.Transfers.Uploads.InProgress.CurrentTotalSpeed.Value, + }, + Queued = new + { + Files = Metrics.Transfers.Uploads.Queued.Files.Value, + Users = Metrics.Transfers.Uploads.Queued.Users.Value, + Bytes = Metrics.Transfers.Uploads.Queued.Bytes.Value, + }, + Completed = new + { + Succeeded = Metrics.Transfers.Uploads.Completed.Succeeded.Value, + Failed = Metrics.Transfers.Uploads.Completed.Failed.Value, + Bytes = Metrics.Transfers.Uploads.Completed.Bytes.Value, + }, + }, + }; + + return metrics; + } +}
\ No newline at end of file diff --git a/src/slskd/Telemetry/TelemetryService.cs b/src/slskd/Telemetry/TelemetryService.cs index 74800126..5085d6ed 100644 --- a/src/slskd/Telemetry/TelemetryService.cs +++ b/src/slskd/Telemetry/TelemetryService.cs @@ -30,16 +30,32 @@ // ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ // </copyright> +using Microsoft.AspNetCore.SignalR; + namespace slskd.Telemetry; public class TelemetryService { - public TelemetryService(PrometheusService prometheusService, ReportsService reportsService) + public TelemetryService( + PrometheusService prometheusService, + ReportsService reportsService, + MetricsService metricsService, + IHubContext<MetricsHub, IMetricsHub> metricsHub) { Prometheus = prometheusService; Reports = reportsService; + Metrics = metricsService; + + MetricsHub = metricsHub; + + Clock.EverySecond += (_, _) => + { + MetricsHub.Clients.All.Update(Metrics.Current()); + }; } public PrometheusService Prometheus { get; } public ReportsService Reports { get; } + public MetricsService Metrics { get; } + private IHubContext<MetricsHub, IMetricsHub> MetricsHub { get; } }
\ No newline at end of file |
