diff --git a/VoidCat/Model/Extensions.cs b/VoidCat/Model/Extensions.cs index cdc7e19..d2f4647 100644 --- a/VoidCat/Model/Extensions.cs +++ b/VoidCat/Model/Extensions.cs @@ -232,5 +232,5 @@ public static class Extensions => !string.IsNullOrEmpty(settings.Redis); public static bool HasPrometheus(this VoidSettings settings) - => settings.Prometheus != null; + => settings.Prometheus?.Url != null; } \ No newline at end of file diff --git a/VoidCat/Model/VoidSettings.cs b/VoidCat/Model/VoidSettings.cs index 6c382b8..8fe7fe6 100644 --- a/VoidCat/Model/VoidSettings.cs +++ b/VoidCat/Model/VoidSettings.cs @@ -11,12 +11,12 @@ namespace VoidCat.Model /// Data directory to store files in /// public string DataDirectory { get; init; } = "./data"; - + /// /// Size in bytes to split uploads into chunks /// public ulong? UploadSegmentSize { get; init; } = null; - + /// /// Tor configuration /// @@ -60,26 +60,26 @@ namespace VoidCat.Model /// Virus scanner settings /// public VirusScannerSettings? VirusScanner { get; init; } - + /// /// Request header to unmask in the logs, otherwise all are masked /// public IEnumerable? RequestHeadersLog { get; init; } - + /// /// hCaptcha settings /// public CaptchaSettings? CaptchaSettings { get; init; } - + /// /// Postgres database connection string /// public string? Postgres { get; init; } - + /// /// Prometheus server for querying metrics /// - public Uri? Prometheus { get; init; } + public PrometheusSettings? Prometheus { get; init; } /// /// Select where to store metadata, if not set "local-disk" will be used @@ -152,4 +152,11 @@ namespace VoidCat.Model public string? SiteKey { get; init; } public string? Secret { get; init; } } + + public sealed class PrometheusSettings + { + public Uri? Url { get; init; } + public string? EgressQuery { get; init; } + public string? IngressQuery { get; init; } + } } \ No newline at end of file diff --git a/VoidCat/Services/Stats/PrometheusStatsReporter.cs b/VoidCat/Services/Stats/PrometheusStatsReporter.cs index a61451e..a4a00be 100644 --- a/VoidCat/Services/Stats/PrometheusStatsReporter.cs +++ b/VoidCat/Services/Stats/PrometheusStatsReporter.cs @@ -11,25 +11,25 @@ public class PrometheusStatsReporter : ITimeSeriesStatsReporter { private readonly ILogger _logger; private readonly HttpClient _client; + private readonly VoidSettings _settings; public PrometheusStatsReporter(ILogger logger, HttpClient client, VoidSettings settings) { _client = client; + _settings = settings; _logger = logger; - _client.BaseAddress = settings.Prometheus; + _client.BaseAddress = settings.Prometheus!.Url; } public async ValueTask> GetBandwidth(DateTime start, DateTime end) { - var q = "increase(egress{file=\"\"}[1d])"; - return await QueryInner(q, start, end); + return await QueryInner(string.Format(_settings.Prometheus?.EgressQuery ?? string.Empty, string.Empty), start, end); } public async ValueTask> GetBandwidth(Guid id, DateTime start, DateTime end) { - var q = $"increase(egress{{file=\"{id}\"}}[1d])"; - return await QueryInner(q, start, end); + return await QueryInner(string.Format(_settings.Prometheus?.EgressQuery ?? string.Empty, id.ToString()), start, end); } private async Task> QueryInner(string query, DateTime start, DateTime end)