Add PrometheusSettings

This commit is contained in:
Kieran 2022-08-28 21:28:51 +01:00
parent 23816a0c07
commit 1a99170b28
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 20 additions and 13 deletions

View File

@ -232,5 +232,5 @@ public static class Extensions
=> !string.IsNullOrEmpty(settings.Redis); => !string.IsNullOrEmpty(settings.Redis);
public static bool HasPrometheus(this VoidSettings settings) public static bool HasPrometheus(this VoidSettings settings)
=> settings.Prometheus != null; => settings.Prometheus?.Url != null;
} }

View File

@ -11,12 +11,12 @@ namespace VoidCat.Model
/// Data directory to store files in /// Data directory to store files in
/// </summary> /// </summary>
public string DataDirectory { get; init; } = "./data"; public string DataDirectory { get; init; } = "./data";
/// <summary> /// <summary>
/// Size in bytes to split uploads into chunks /// Size in bytes to split uploads into chunks
/// </summary> /// </summary>
public ulong? UploadSegmentSize { get; init; } = null; public ulong? UploadSegmentSize { get; init; } = null;
/// <summary> /// <summary>
/// Tor configuration /// Tor configuration
/// </summary> /// </summary>
@ -60,26 +60,26 @@ namespace VoidCat.Model
/// Virus scanner settings /// Virus scanner settings
/// </summary> /// </summary>
public VirusScannerSettings? VirusScanner { get; init; } public VirusScannerSettings? VirusScanner { get; init; }
/// <summary> /// <summary>
/// Request header to unmask in the logs, otherwise all are masked /// Request header to unmask in the logs, otherwise all are masked
/// </summary> /// </summary>
public IEnumerable<string>? RequestHeadersLog { get; init; } public IEnumerable<string>? RequestHeadersLog { get; init; }
/// <summary> /// <summary>
/// hCaptcha settings /// hCaptcha settings
/// </summary> /// </summary>
public CaptchaSettings? CaptchaSettings { get; init; } public CaptchaSettings? CaptchaSettings { get; init; }
/// <summary> /// <summary>
/// Postgres database connection string /// Postgres database connection string
/// </summary> /// </summary>
public string? Postgres { get; init; } public string? Postgres { get; init; }
/// <summary> /// <summary>
/// Prometheus server for querying metrics /// Prometheus server for querying metrics
/// </summary> /// </summary>
public Uri? Prometheus { get; init; } public PrometheusSettings? Prometheus { get; init; }
/// <summary> /// <summary>
/// Select where to store metadata, if not set "local-disk" will be used /// 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? SiteKey { get; init; }
public string? Secret { 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; }
}
} }

View File

@ -11,25 +11,25 @@ public class PrometheusStatsReporter : ITimeSeriesStatsReporter
{ {
private readonly ILogger<PrometheusStatsReporter> _logger; private readonly ILogger<PrometheusStatsReporter> _logger;
private readonly HttpClient _client; private readonly HttpClient _client;
private readonly VoidSettings _settings;
public PrometheusStatsReporter(ILogger<PrometheusStatsReporter> logger, HttpClient client, VoidSettings settings) public PrometheusStatsReporter(ILogger<PrometheusStatsReporter> logger, HttpClient client, VoidSettings settings)
{ {
_client = client; _client = client;
_settings = settings;
_logger = logger; _logger = logger;
_client.BaseAddress = settings.Prometheus; _client.BaseAddress = settings.Prometheus!.Url;
} }
public async ValueTask<IReadOnlyList<BandwidthPoint>> GetBandwidth(DateTime start, DateTime end) public async ValueTask<IReadOnlyList<BandwidthPoint>> GetBandwidth(DateTime start, DateTime end)
{ {
var q = "increase(egress{file=\"\"}[1d])"; return await QueryInner(string.Format(_settings.Prometheus?.EgressQuery ?? string.Empty, string.Empty), start, end);
return await QueryInner(q, start, end);
} }
public async ValueTask<IReadOnlyList<BandwidthPoint>> GetBandwidth(Guid id, DateTime start, DateTime end) public async ValueTask<IReadOnlyList<BandwidthPoint>> GetBandwidth(Guid id, DateTime start, DateTime end)
{ {
var q = $"increase(egress{{file=\"{id}\"}}[1d])"; return await QueryInner(string.Format(_settings.Prometheus?.EgressQuery ?? string.Empty, id.ToString()), start, end);
return await QueryInner(q, start, end);
} }
private async Task<IReadOnlyList<BandwidthPoint>> QueryInner(string query, DateTime start, DateTime end) private async Task<IReadOnlyList<BandwidthPoint>> QueryInner(string query, DateTime start, DateTime end)