From 86d9726c2e1a4b6025fae5cce59e9ff37e472eb1 Mon Sep 17 00:00:00 2001 From: rngcntr <7890887+rngcntr@users.noreply.github.com> Date: Thu, 24 Aug 2023 14:38:22 +0200 Subject: [PATCH] Let all services report message_processing timings (#941) Fixes #940 --- shared.csproj | 2 +- .../PrometheusDelegatingMessageHandler.cs | 11 +++++----- .../MultiOutputServiceAdapter.cs | 19 +++++----------- .../SingleOutputServiceAdapter.cs | 5 ++--- ...PrometheusDelegatingMessageHandlerTests.cs | 22 +++++++++++++++++++ .../MultiOutputServiceAdapterTests.cs | 17 +------------- 6 files changed, 36 insertions(+), 40 deletions(-) create mode 100644 test/Motor.Extensions.Diagnostics.Metrics_UnitTest/PrometheusDelegatingMessageHandlerTests.cs diff --git a/shared.csproj b/shared.csproj index ba92ca0d..5a6623f0 100644 --- a/shared.csproj +++ b/shared.csproj @@ -1,7 +1,7 @@ - 0.12.0 + 0.13.0 11 enable CS8600;CS8602;CS8625;CS8618;CS8604;CS8601 diff --git a/src/Motor.Extensions.Diagnostics.Metrics/PrometheusDelegatingMessageHandler.cs b/src/Motor.Extensions.Diagnostics.Metrics/PrometheusDelegatingMessageHandler.cs index 2937bdde..ec0889d6 100644 --- a/src/Motor.Extensions.Diagnostics.Metrics/PrometheusDelegatingMessageHandler.cs +++ b/src/Motor.Extensions.Diagnostics.Metrics/PrometheusDelegatingMessageHandler.cs @@ -10,20 +10,19 @@ namespace Motor.Extensions.Diagnostics.Metrics; public class PrometheusDelegatingMessageHandler : DelegatingMessageHandler where TInput : class { - private readonly IMetricFamily _messageProcessingTotal; + private readonly IMetricFamily _messageProcessing; - public PrometheusDelegatingMessageHandler( - IMetricsFactory> metricsFactory) + public PrometheusDelegatingMessageHandler(IMetricsFactory> metricsFactory) { - _messageProcessingTotal = - metricsFactory.CreateCounter("message_processing_total", "Message processing status total", false, "status"); + _messageProcessing = + metricsFactory.CreateSummary("message_processing", "Message processing duration in ms", false, "status"); } public override async Task HandleMessageAsync(MotorCloudEvent dataCloudEvent, CancellationToken token = default) { var processedMessageStatus = ProcessedMessageStatus.CriticalFailure; - using (new AutoIncCounter(() => _messageProcessingTotal.WithLabels(processedMessageStatus.ToString()))) + using (new AutoObserveStopwatch(() => _messageProcessing.WithLabels(processedMessageStatus.ToString()))) { processedMessageStatus = await base.HandleMessageAsync(dataCloudEvent, token); } diff --git a/src/Motor.Extensions.Hosting/MultiOutputServiceAdapter.cs b/src/Motor.Extensions.Hosting/MultiOutputServiceAdapter.cs index 7921ef8a..56c10642 100644 --- a/src/Motor.Extensions.Hosting/MultiOutputServiceAdapter.cs +++ b/src/Motor.Extensions.Hosting/MultiOutputServiceAdapter.cs @@ -2,11 +2,8 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; -using Motor.Extensions.Diagnostics.Metrics; -using Motor.Extensions.Diagnostics.Metrics.Abstractions; using Motor.Extensions.Hosting.Abstractions; using Motor.Extensions.Hosting.CloudEvents; -using Prometheus.Client; namespace Motor.Extensions.Hosting; @@ -16,18 +13,15 @@ public class MultiOutputServiceAdapter : INoOutputService _converter; private readonly ILogger> _logger; - private readonly ISummary? _messageProcessing; private readonly ITypedMessagePublisher _publisher; public MultiOutputServiceAdapter(ILogger> logger, - IMetricsFactory>? metrics, IMultiOutputService converter, ITypedMessagePublisher publisher) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _converter = converter ?? throw new ArgumentNullException(nameof(converter)); _publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - _messageProcessing = metrics?.CreateSummary("message_processing", "Message processing duration in ms"); } public async Task HandleMessageAsync(MotorCloudEvent dataCloudEvent, @@ -35,16 +29,13 @@ public async Task HandleMessageAsync(MotorCloudEvent _messageProcessing)) + await foreach (var message in _converter.ConvertMessageAsync(dataCloudEvent, token) + .ConfigureAwait(false).WithCancellation(token)) { - await foreach (var message in _converter.ConvertMessageAsync(dataCloudEvent, token) - .ConfigureAwait(false).WithCancellation(token)) + if (message?.Data is not null) { - if (message?.Data is not null) - { - await _publisher.PublishMessageAsync(message, token) - .ConfigureAwait(false); - } + await _publisher.PublishMessageAsync(message, token) + .ConfigureAwait(false); } } diff --git a/src/Motor.Extensions.Hosting/SingleOutputServiceAdapter.cs b/src/Motor.Extensions.Hosting/SingleOutputServiceAdapter.cs index b119fbda..085e068c 100644 --- a/src/Motor.Extensions.Hosting/SingleOutputServiceAdapter.cs +++ b/src/Motor.Extensions.Hosting/SingleOutputServiceAdapter.cs @@ -9,9 +9,8 @@ public class SingleOutputServiceAdapter : MultiOutputServiceAda where TOutput : class { public SingleOutputServiceAdapter(ILogger> logger, - IMetricsFactory> metrics, ISingleOutputService service, - ITypedMessagePublisher publisher) : - base(logger, metrics, new SingleToMultiOutputAdapter(service), publisher) + ISingleOutputService service, ITypedMessagePublisher publisher) : + base(logger, new SingleToMultiOutputAdapter(service), publisher) { } } diff --git a/test/Motor.Extensions.Diagnostics.Metrics_UnitTest/PrometheusDelegatingMessageHandlerTests.cs b/test/Motor.Extensions.Diagnostics.Metrics_UnitTest/PrometheusDelegatingMessageHandlerTests.cs new file mode 100644 index 00000000..4ccfdac8 --- /dev/null +++ b/test/Motor.Extensions.Diagnostics.Metrics_UnitTest/PrometheusDelegatingMessageHandlerTests.cs @@ -0,0 +1,22 @@ +using Moq; +using Motor.Extensions.Diagnostics.Metrics; +using Motor.Extensions.Diagnostics.Metrics.Abstractions; +using Xunit; + +namespace Motor.Extensions.Diagnostics.Metrics_UnitTest; + +public class PrometheusDelegatingMessageHandlerTests +{ + [Fact] + public void Ctor_WithMetricsFactory_SummaryIsCreated() + { + var metricsFactoryMock = new Mock>>(); + + var _ = new PrometheusDelegatingMessageHandler(metricsFactoryMock.Object); + + metricsFactoryMock.Verify(x => + x.CreateSummary("message_processing", "Message processing duration in ms", + false, "status") + ); + } +} diff --git a/test/Motor.Extensions.Hosting_UnitTest/MultiOutputServiceAdapterTests.cs b/test/Motor.Extensions.Hosting_UnitTest/MultiOutputServiceAdapterTests.cs index d6fd23a2..67961bd4 100644 --- a/test/Motor.Extensions.Hosting_UnitTest/MultiOutputServiceAdapterTests.cs +++ b/test/Motor.Extensions.Hosting_UnitTest/MultiOutputServiceAdapterTests.cs @@ -22,20 +22,6 @@ public class MultiOutputServiceAdapterTests private static Mock> FakePublisher => new(); - - [Fact] - public void Ctor_WithMetricsFactory_SummaryIsCreated() - { - var metricsFactoryMock = new Mock>>(); - - GetMessageHandler(metrics: metricsFactoryMock.Object); - - metricsFactoryMock.Verify(x => - x.CreateSummary("message_processing", "Message processing duration in ms", - false, null as IReadOnlyList, null, null, null) - ); - } - [Fact] public async Task HandleMessageAsync_WithContextAndInput_HasContext() { @@ -180,7 +166,6 @@ private async IAsyncEnumerable> CreateReturnValues(param private MultiOutputServiceAdapter GetMessageHandler( ILogger>? logger = null, - IMetricsFactory>? metrics = null, IMultiOutputService? service = null, ITypedMessagePublisher? publisher = null) { @@ -188,7 +173,7 @@ private MultiOutputServiceAdapter GetMessageHandler( service ??= FakeService.Object; publisher ??= FakePublisher.Object; - return new MultiOutputServiceAdapter(logger, metrics, service, publisher); + return new MultiOutputServiceAdapter(logger, service, publisher); } private static MotorCloudEvent CreateMotorEvent(string data = "")