From c79bbfa014c3d7fdc6e0cf03409b594887cb493f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 Nov 2025 18:32:56 -0600 Subject: docs, error handling --- src/slskd/Telemetry/API/MetricsController.cs | 57 ++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 16 deletions(-) (limited to 'src/slskd/Telemetry/API') diff --git a/src/slskd/Telemetry/API/MetricsController.cs b/src/slskd/Telemetry/API/MetricsController.cs index de689314..3ed7c1e2 100644 --- a/src/slskd/Telemetry/API/MetricsController.cs +++ b/src/slskd/Telemetry/API/MetricsController.cs @@ -25,6 +25,7 @@ using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Serilog; /// /// Telemetry. @@ -45,6 +46,7 @@ public class MetricsController : ControllerBase } private TelemetryService Telemetry { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// Gets KPIs for the app, based on the Prometheus dashboard @@ -60,8 +62,8 @@ public class MetricsController : ControllerBase /// Other /// * process_start_time_seconds = unix timestamp of start time. /// - private List KpiRegexes { get; } = new List - { + private List KpiRegexes { get; } = + [ new Regex("slskd_.*", RegexOptions.Compiled), new Regex("node_(?!cpu)", RegexOptions.Compiled), new Regex("process_.*", RegexOptions.Compiled), @@ -69,42 +71,65 @@ public class MetricsController : ControllerBase new Regex("system_runtime_[cpu_usage|working_set|alloc_total]", RegexOptions.Compiled), new Regex("system_net_sockets.*", RegexOptions.Compiled), new Regex("microsoft_aspnetcore_server_kestrel_[current|total]_connections", RegexOptions.Compiled), - }; + ]; /// /// Gets all application metrics. /// /// - /// Returns a list of all application metrics, in Prometheus format. JSON formatted metrics can be retrieved - /// by setting the 'Accept' header to 'application/json'. + /// If the 'Accept' header is set to 'text/plain', the response is in Prometheus format. Otherwise if 'application/json' is set, + /// the metrics are formatted into a dictionary. /// - /// + /// A flat Prometheus-formatted list of all metrics given text/plain, a dictionary keyed by metric name otherwise. + /// The request completed successfully. + /// An error occurred. [HttpGet("")] [Authorize(Policy = AuthPolicy.Any)] [Produces("text/plain", "application/json")] [ProducesResponseType(typeof(string), 200, "text/plain")] [ProducesResponseType(typeof(Dictionary), 200, "application/json")] + [ProducesResponseType(typeof(string), 500)] public async Task Get() { - if (Request.Headers.Accept.ToString().Equals("application/json", StringComparison.OrdinalIgnoreCase)) + try { - Dictionary dict = await Telemetry.Prometheus.GetMetricsAsObject(); - return Ok(dict); - } + if (Request.Headers.Accept.ToString().Equals("application/json", StringComparison.OrdinalIgnoreCase)) + { + Dictionary dict = await Telemetry.Prometheus.GetMetricsAsObject(); + return Ok(dict); + } - var response = await Telemetry.Prometheus.GetMetricsAsString(); - return Content(response, "text/plain"); + var response = await Telemetry.Prometheus.GetMetricsAsString(); + return Content(response, "text/plain"); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching metrics: {Message}", ex.Message); + return StatusCode(500, ex.Message); + } } /// - /// Gets gets key performance indicators for the application. + /// Gets gets key performance indicators (KPIs) for the application. /// - /// + /// A dictionary keyed by metric name. + /// The request completed successfully. + /// An error occurred. [HttpGet("kpis")] [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary), 200)] + [ProducesResponseType(typeof(string), 500)] public async Task GetKpis() { - Dictionary dict = await Telemetry.Prometheus.GetMetricsAsObject(include: KpiRegexes); - return Ok(dict); + try + { + Dictionary dict = await Telemetry.Prometheus.GetMetricsAsObject(include: KpiRegexes); + return Ok(dict); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching KPIs: {Message}", ex.Message); + return StatusCode(500, ex.Message); + } } } -- cgit v1.2.3