From e7ec9b8aa6cf024a76f19c91c70915b31cad07d5 Mon Sep 17 00:00:00 2001 From: Ruberoid Date: Tue, 22 Jul 2025 01:51:44 +0300 Subject: [PATCH] added prometheus metrics. --- Directory.Packages.props | 7 ++++--- .../Handlers/Implementation/UpdateHandler.cs | 5 +++++ src/Nocr.TelegramListener.Host/Infrastructure/Startup.cs | 9 ++++++++- .../Nocr.TelegramListener.Host.csproj | 1 + 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 1776e14..3e160e1 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,6 +8,7 @@ + @@ -24,10 +25,10 @@ - + - - + + \ No newline at end of file diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/UpdateHandler.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/UpdateHandler.cs index 2534c7a..d808c62 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/UpdateHandler.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/Handlers/Implementation/UpdateHandler.cs @@ -18,6 +18,8 @@ public sealed class UpdateHandler : IUpdateHandler public async Task HandleUpdate(Update update, CancellationToken cancellationToken = default) { + TelegramListenerMetrics.UpdatesReceived.Inc(); + TelegramListenerMetrics.LastUpdateTimestamp.Set(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); try { switch (update) @@ -29,9 +31,12 @@ public sealed class UpdateHandler : IUpdateHandler _logger.LogInformation($"Update of type `{update.GetType().Name}` fired."); break; } + + TelegramListenerMetrics.UpdatesProcessed.Inc(); } catch (Exception ex) { + TelegramListenerMetrics.UpdatesErrors.Inc(); _logger.LogError(ex, "Critical error in HandleUpdate."); throw; } diff --git a/src/Nocr.TelegramListener.Host/Infrastructure/Startup.cs b/src/Nocr.TelegramListener.Host/Infrastructure/Startup.cs index bc03edb..10f1c57 100644 --- a/src/Nocr.TelegramListener.Host/Infrastructure/Startup.cs +++ b/src/Nocr.TelegramListener.Host/Infrastructure/Startup.cs @@ -3,6 +3,7 @@ using Nocr.TelegramListener.AppServices; using Nocr.TelegramListener.Async.Api.Contracts.Events; using Nocr.TelegramListener.Core.Dates; using Nocr.TelegramListener.Core.Options; +using Prometheus; using Rebus.Bus; using Rebus.Config; using Rebus.Routing.TypeBased; @@ -46,6 +47,12 @@ public class Startup // TODO: BackgroundService bus.Advanced.Topics.Subscribe(Constants.RoutingKeys.Subscriptions).GetAwaiter().GetResult(); - app.UseHealthChecks("/health"); + app.UseHttpMetrics(); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapHealthChecks("/health"); + endpoints.MapMetrics(); + }); } } \ No newline at end of file diff --git a/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj b/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj index 9a4d4c0..9558aa2 100644 --- a/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj +++ b/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj @@ -6,6 +6,7 @@ +