From 41f89f52f3c1f40b98c7ef963449e8fbc6c4ece5 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 11:55:06 +0200 Subject: [PATCH 01/13] static analyzer fixes --- .../Core/DiagnosticSourceListener.cs | 8 ++------ .../Core/DiagnosticSourceSubscriber.cs | 3 +-- .../EventListeners/CountersEventListener.cs | 4 ++-- .../Healthchecks/PrometheusHealthcheckPublisher.cs | 1 - 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs b/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs index 3589f82..e550f3e 100644 --- a/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs +++ b/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Text; - +W namespace Prometheus.Contrib.Core { internal class DiagnosticSourceListener : IObserver> @@ -24,9 +23,6 @@ public void OnError(Exception error) public void OnNext(KeyValuePair value) { - //if (Activity.Current == null) - // return; - try { if (value.Key.EndsWith("Start")) @@ -38,7 +34,7 @@ public void OnNext(KeyValuePair value) else handler.OnCustom(value.Key, Activity.Current, value.Value); } - catch (Exception ex) + catch { } } diff --git a/src/prometheus-net.Contrib/Core/DiagnosticSourceSubscriber.cs b/src/prometheus-net.Contrib/Core/DiagnosticSourceSubscriber.cs index 3d30b85..b1de0d5 100644 --- a/src/prometheus-net.Contrib/Core/DiagnosticSourceSubscriber.cs +++ b/src/prometheus-net.Contrib/Core/DiagnosticSourceSubscriber.cs @@ -9,10 +9,9 @@ public class DiagnosticSourceSubscriber : IDisposable, IObserver handlerFactory; private readonly Func diagnosticSourceFilter; - //private readonly Func isEnabledFilter; private long disposed; private IDisposable allSourcesSubscription; - private List listenerSubscriptions; + private readonly List listenerSubscriptions; public DiagnosticSourceSubscriber( Func handlerFactory, diff --git a/src/prometheus-net.Contrib/EventListeners/CountersEventListener.cs b/src/prometheus-net.Contrib/EventListeners/CountersEventListener.cs index 9b1b7cc..7ee1b7a 100644 --- a/src/prometheus-net.Contrib/EventListeners/CountersEventListener.cs +++ b/src/prometheus-net.Contrib/EventListeners/CountersEventListener.cs @@ -7,7 +7,7 @@ namespace Prometheus.Contrib.EventListeners { public class CountersEventListener : EventListener { - private Dictionary counterAdapters = new Dictionary() + private readonly Dictionary counterAdapters = new Dictionary { ["System.Runtime"] = new PrometheusRuntimeCounterAdapter(), ["Microsoft.AspNetCore.Hosting"] = new PrometheusAspNetCoreCounterAdapter(), @@ -16,7 +16,7 @@ public class CountersEventListener : EventListener ["Grpc.Net.Client"] = new PrometheusGrpcServerCounterAdapter() }; - private IDictionary eventArguments = new Dictionary + private readonly IDictionary eventArguments = new Dictionary { ["EventCounterIntervalSec"] = "10" }; diff --git a/src/prometheus-net.Contrib/Healthchecks/PrometheusHealthcheckPublisher.cs b/src/prometheus-net.Contrib/Healthchecks/PrometheusHealthcheckPublisher.cs index 42ca216..176608a 100644 --- a/src/prometheus-net.Contrib/Healthchecks/PrometheusHealthcheckPublisher.cs +++ b/src/prometheus-net.Contrib/Healthchecks/PrometheusHealthcheckPublisher.cs @@ -2,7 +2,6 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Diagnostics.HealthChecks; -using Prometheus; namespace Prometheus.Contrib.Healthchecks { From 1497378eb0f988f15db648eb50feeb8b96c2ecc2 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 12:02:57 +0200 Subject: [PATCH 02/13] update dependencies --- samples/WebApp/Controllers/TestController.cs | 2 +- .../{Consumers => MassTransit}/TestCommand.cs | 2 +- .../{Consumers => MassTransit}/TestConsumer.cs | 15 ++++----------- samples/WebApp/Startup.cs | 16 ++++++---------- samples/WebApp/WebApp.csproj | 14 ++++++++------ samples/WebApp/docker-compose.yml | 16 ++++++++-------- .../Core/DiagnosticSourceListener.cs | 2 +- .../prometheus-net.EntityFramework.csproj | 2 +- 8 files changed, 30 insertions(+), 39 deletions(-) rename samples/WebApp/{Consumers => MassTransit}/TestCommand.cs (77%) rename samples/WebApp/{Consumers => MassTransit}/TestConsumer.cs (73%) diff --git a/samples/WebApp/Controllers/TestController.cs b/samples/WebApp/Controllers/TestController.cs index 04d4ce6..7ad3f9a 100644 --- a/samples/WebApp/Controllers/TestController.cs +++ b/samples/WebApp/Controllers/TestController.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using MassTransit; using Microsoft.AspNetCore.Mvc; -using WebApp.Consumers; +using WebApp.MassTransit; namespace WebApp.Controllers { diff --git a/samples/WebApp/Consumers/TestCommand.cs b/samples/WebApp/MassTransit/TestCommand.cs similarity index 77% rename from samples/WebApp/Consumers/TestCommand.cs rename to samples/WebApp/MassTransit/TestCommand.cs index 7f651a6..8bde987 100644 --- a/samples/WebApp/Consumers/TestCommand.cs +++ b/samples/WebApp/MassTransit/TestCommand.cs @@ -1,6 +1,6 @@ using System; -namespace WebApp.Consumers +namespace WebApp.MassTransit { public class TestCommand { diff --git a/samples/WebApp/Consumers/TestConsumer.cs b/samples/WebApp/MassTransit/TestConsumer.cs similarity index 73% rename from samples/WebApp/Consumers/TestConsumer.cs rename to samples/WebApp/MassTransit/TestConsumer.cs index c35fdfa..ab3da5b 100644 --- a/samples/WebApp/Consumers/TestConsumer.cs +++ b/samples/WebApp/MassTransit/TestConsumer.cs @@ -1,14 +1,10 @@ -using MassTransit; -using StackExchange.Redis; -using System; +using System.Threading.Tasks; +using MassTransit; using Microsoft.Data.SqlClient; -using System.Threading.Tasks; -using WebApp; -using WebApp.Data; using Microsoft.EntityFrameworkCore; -using System.Transactions; +using WebApp.Data; -namespace WebApp.Consumers +namespace WebApp.MassTransit { public class TestConsumer : IConsumer { @@ -23,9 +19,6 @@ public TestConsumer(SqlConnection connection, TestContext testContext) public async Task Consume(ConsumeContext context) { - //var database = Startup.connection.GetDatabase(); - //database.StringGet("test1"); - await testContext.TestEntities.ToListAsync(); var command = connection.CreateCommand(); diff --git a/samples/WebApp/Startup.cs b/samples/WebApp/Startup.cs index b33df1e..b70af9d 100644 --- a/samples/WebApp/Startup.cs +++ b/samples/WebApp/Startup.cs @@ -4,24 +4,19 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using MassTransit.AspNetCoreIntegration; -using StackExchange.Redis; using Microsoft.Data.SqlClient; using Prometheus; using MassTransit; -using WebApp.Consumers; -using MassTransit.ActiveMqTransport; -using MassTransit.ActiveMqTransport.Configurators; using System; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Diagnostics.HealthChecks; using WebApp.Data; -using Microsoft.AspNetCore.Routing; +using WebApp.MassTransit; namespace WebApp { public class Startup { - public static ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(""); - public Startup(IConfiguration configuration) { Configuration = configuration; @@ -39,9 +34,10 @@ public void ConfigureServices(IServiceCollection services) }); services.AddMassTransit( - provider => Bus.Factory.CreateUsingActiveMq(factoryConfigurator => + provider => Bus.Factory.CreateUsingRabbitMq(factoryConfigurator => { - var host = factoryConfigurator.Host(new ConfigurationHostSettings(new Uri("activemq://localhost:61616"))); + factoryConfigurator.Host(new Uri("amqp://localhost:5672/")); + factoryConfigurator.ReceiveEndpoint("test_events", receiveEndpointConfigurator => { receiveEndpointConfigurator.Consumer(provider); @@ -50,7 +46,7 @@ public void ConfigureServices(IServiceCollection services) config => { config.AddConsumer(); - }); + }, options => options.FailureStatus = HealthStatus.Unhealthy); services.AddPrometheusAspNetCoreMetrics(); diff --git a/samples/WebApp/WebApp.csproj b/samples/WebApp/WebApp.csproj index 0002cb3..aff6c1e 100644 --- a/samples/WebApp/WebApp.csproj +++ b/samples/WebApp/WebApp.csproj @@ -1,18 +1,20 @@  - netcoreapp3.0 + netcoreapp3.1 - - + + - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - diff --git a/samples/WebApp/docker-compose.yml b/samples/WebApp/docker-compose.yml index addf81b..7f0a76a 100644 --- a/samples/WebApp/docker-compose.yml +++ b/samples/WebApp/docker-compose.yml @@ -15,13 +15,13 @@ services: ports: - 6379:6379 - activemq: - image: rmohr/activemq:latest + rabbit: + image: "rabbitmq:3.8.2-management" environment: - - "ACTIVEMQ_ADMIN_LOGIN=admin" - - "ACTIVEMQ_ADMIN_PASSWORD=admin" - - "ACTIVEMQ_LOGGER_LOGLEVEL=TRACE" + RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG" + RABBITMQ_DEFAULT_USER: "admin" + RABBITMQ_DEFAULT_PASS: "admin" + RABBITMQ_DEFAULT_VHOST: "/" ports: - - 8161:8161 - - 61616:61616 - - 61613:61613 \ No newline at end of file + - "15672:15672" + - "5672:5672" \ No newline at end of file diff --git a/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs b/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs index e550f3e..d63e518 100644 --- a/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs +++ b/src/prometheus-net.Contrib/Core/DiagnosticSourceListener.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; -W + namespace Prometheus.Contrib.Core { internal class DiagnosticSourceListener : IObserver> diff --git a/src/prometheus-net.EntityFramework/prometheus-net.EntityFramework.csproj b/src/prometheus-net.EntityFramework/prometheus-net.EntityFramework.csproj index 593af58..32813e1 100644 --- a/src/prometheus-net.EntityFramework/prometheus-net.EntityFramework.csproj +++ b/src/prometheus-net.EntityFramework/prometheus-net.EntityFramework.csproj @@ -7,7 +7,7 @@ - + From c14313ee4af09e954d58bc827cf201df80e2f8ad Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 12:25:20 +0200 Subject: [PATCH 03/13] Updated masstransit handler --- samples/WebApp/Startup.cs | 4 +- samples/WebApp/docker-compose.yml | 6 --- .../Diagnostics/MassTransitListenerHandler.cs | 48 +++++++++---------- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/samples/WebApp/Startup.cs b/samples/WebApp/Startup.cs index b70af9d..22a8938 100644 --- a/samples/WebApp/Startup.cs +++ b/samples/WebApp/Startup.cs @@ -36,7 +36,7 @@ public void ConfigureServices(IServiceCollection services) services.AddMassTransit( provider => Bus.Factory.CreateUsingRabbitMq(factoryConfigurator => { - factoryConfigurator.Host(new Uri("amqp://localhost:5672/")); + factoryConfigurator.Host(new Uri("amqp://admin:admin@localhost:5672/")); factoryConfigurator.ReceiveEndpoint("test_events", receiveEndpointConfigurator => { @@ -48,7 +48,9 @@ public void ConfigureServices(IServiceCollection services) config.AddConsumer(); }, options => options.FailureStatus = HealthStatus.Unhealthy); + services.AddPrometheusAspNetCoreMetrics(); + services.AddPrometheusMassTransitMetrics(); services.AddSingleton(provider => { diff --git a/samples/WebApp/docker-compose.yml b/samples/WebApp/docker-compose.yml index 7f0a76a..bfcd0f5 100644 --- a/samples/WebApp/docker-compose.yml +++ b/samples/WebApp/docker-compose.yml @@ -9,12 +9,6 @@ services: ports: - 11433:1433 - redis: - image: redis:5 - command: ["redis-server", "--appendonly", "yes"] - ports: - - 6379:6379 - rabbit: image: "rabbitmq:3.8.2-management" environment: diff --git a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs index 51ae466..05d1f91 100644 --- a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs +++ b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs @@ -8,41 +8,33 @@ public class MassTransitListenerHandler : DiagnosticListenerHandler { private static class PrometheusCounters { - private static string[] DefaultLabelNames = new[] { "message" }; - private static string[] ErrorLabelNames = new[] { "exception" }; - public static readonly Histogram SendMessageCount = Metrics.CreateHistogram( - "masstransit_total_sent_messages", + "masstransit_messages_sent_total", "Total sent messages.", - new HistogramConfiguration { LabelNames = DefaultLabelNames }); + new HistogramConfiguration { LabelNames = new[] { "exchange" } }); public static readonly Counter SendMessageErrors = Metrics.CreateCounter( - "masstransit_total_sent_messages_errors", + "masstransit_messages_sent_errors_total", "Total sent messages errors", - new CounterConfiguration { LabelNames = ErrorLabelNames }); + new CounterConfiguration { LabelNames = new[] { "exception" } }); public static readonly Histogram ConsumeMessageCount = Metrics.CreateHistogram( - "masstransit_total_consumed_messages", + "masstransit_messages_consumed_total", "The time to consume a message, in seconds.", - new HistogramConfiguration { LabelNames = DefaultLabelNames }); - - public static readonly Histogram ConsumeCriticalDuration = Metrics.CreateHistogram( - "masstransit_critical_time_seconds", - "The time between when message is sent and when it is consumed, in seconds.", - new HistogramConfiguration { LabelNames = DefaultLabelNames }); + new HistogramConfiguration { LabelNames = new[] { "consumer", "message" } }); public static readonly Counter ConsumeMessageError = Metrics.CreateCounter( - "masstransit_total_consumed_messages_errors", + "masstransit_messages_consumed_errors_total", "The number of message processing failures.", - new CounterConfiguration { LabelNames = ErrorLabelNames }); + new CounterConfiguration { LabelNames = new[] { "exception" } }); } + private readonly PropertyFetcher exchangeFetcher = new PropertyFetcher("Exchange"); private readonly PropertyFetcher messageTypeFetcher = new PropertyFetcher("MessageType"); private readonly PropertyFetcher consumerTypeFetcher = new PropertyFetcher("ConsumerType"); - private readonly PropertyFetcher exchangeFetcher = new PropertyFetcher("Exchange"); - private AsyncLocal messageTypeContext = new AsyncLocal(); - private AsyncLocal exchangeContext = new AsyncLocal(); + private readonly AsyncLocal exchangeContext = new AsyncLocal(); + private readonly AsyncLocal<(string, string)> messageTypeContext = new AsyncLocal<(string, string)>(); public MassTransitListenerHandler(string sourceName) : base(sourceName) { @@ -54,8 +46,8 @@ public override void OnStartActivity(Activity activity, object payload) { case "Transport.Send": { - var messageType = exchangeFetcher.Fetch(payload); - exchangeContext.Value = messageType.ToString(); + var exchangeType = exchangeFetcher.Fetch(payload); + exchangeContext.Value = exchangeType.ToString(); } break; case "Transport.Receive": @@ -63,7 +55,10 @@ public override void OnStartActivity(Activity activity, object payload) case "Consumer.Consume": { var messageType = messageTypeFetcher.Fetch(payload); - messageTypeContext.Value = messageType.ToString(); + var consumerType = consumerTypeFetcher.Fetch(payload); + + messageTypeContext.Value = (messageType.ToString(), consumerType.ToString()); + } break; } @@ -75,18 +70,19 @@ public override void OnStopActivity(Activity activity, object payload) { case "Transport.Send": { + var exchangeType = exchangeContext.Value; PrometheusCounters.SendMessageCount - .WithLabels(exchangeContext.Value) + .WithLabels(exchangeType) .Observe(activity.Duration.TotalSeconds); } break; case "Consumer.Consume": { + var (messageType, consumerType) = messageTypeContext.Value; + PrometheusCounters.ConsumeMessageCount - .WithLabels(messageTypeContext.Value) + .WithLabels(consumerType, messageType) .Observe(activity.Duration.TotalSeconds); - - // TODO: calculate critical time } break; } From 9a8bb1cfeaac633f1810140bea93b814621029ef Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 12:45:32 +0200 Subject: [PATCH 04/13] update metrics --- samples/WebApp/WebApp.csproj | 4 +- .../Diagnostics/MassTransitListenerHandler.cs | 85 ++++++++++--------- .../prometheus-net.MassTransit.csproj | 2 +- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/samples/WebApp/WebApp.csproj b/samples/WebApp/WebApp.csproj index aff6c1e..6eee1a0 100644 --- a/samples/WebApp/WebApp.csproj +++ b/samples/WebApp/WebApp.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs index 05d1f91..5128e06 100644 --- a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs +++ b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs @@ -1,5 +1,6 @@ using System.Diagnostics; -using System.Threading; +using System.Linq; +using MassTransit.Logging; using Prometheus.Contrib.Core; namespace Prometheus.MassTransit.Diagnostics @@ -10,18 +11,27 @@ private static class PrometheusCounters { public static readonly Histogram SendMessageCount = Metrics.CreateHistogram( "masstransit_messages_sent_total", - "Total sent messages.", - new HistogramConfiguration { LabelNames = new[] { "exchange" } }); + "Total sent messages."); public static readonly Counter SendMessageErrors = Metrics.CreateCounter( "masstransit_messages_sent_errors_total", "Total sent messages errors", new CounterConfiguration { LabelNames = new[] { "exception" } }); + public static readonly Histogram ReceiveMessageCount = Metrics.CreateHistogram( + "masstransit_messages_received_total", + "The time to receive a message, in seconds.", + new HistogramConfiguration { LabelNames = new[] { "message" } }); + public static readonly Histogram ConsumeMessageCount = Metrics.CreateHistogram( "masstransit_messages_consumed_total", "The time to consume a message, in seconds.", - new HistogramConfiguration { LabelNames = new[] { "consumer", "message" } }); + new HistogramConfiguration { LabelNames = new[] { "consumer" } }); + + public static readonly Counter ReceiveMessageError = Metrics.CreateCounter( + "masstransit_messages_received_errors_total", + "The number of message processing failures.", + new CounterConfiguration { LabelNames = new[] { "exception" } }); public static readonly Counter ConsumeMessageError = Metrics.CreateCounter( "masstransit_messages_consumed_errors_total", @@ -29,60 +39,54 @@ private static class PrometheusCounters new CounterConfiguration { LabelNames = new[] { "exception" } }); } - private readonly PropertyFetcher exchangeFetcher = new PropertyFetcher("Exchange"); - private readonly PropertyFetcher messageTypeFetcher = new PropertyFetcher("MessageType"); - private readonly PropertyFetcher consumerTypeFetcher = new PropertyFetcher("ConsumerType"); - - private readonly AsyncLocal exchangeContext = new AsyncLocal(); - private readonly AsyncLocal<(string, string)> messageTypeContext = new AsyncLocal<(string, string)>(); - public MassTransitListenerHandler(string sourceName) : base(sourceName) { } public override void OnStartActivity(Activity activity, object payload) + { + } + + public override void OnStopActivity(Activity activity, object payload) { switch (activity.OperationName) { - case "Transport.Send": + case OperationName.Transport.Send: { - var exchangeType = exchangeFetcher.Fetch(payload); - exchangeContext.Value = exchangeType.ToString(); + PrometheusCounters.SendMessageCount.Observe(activity.Duration.TotalSeconds); } break; - case "Transport.Receive": - break; - case "Consumer.Consume": + case OperationName.Transport.Receive: { - var messageType = messageTypeFetcher.Fetch(payload); - var consumerType = consumerTypeFetcher.Fetch(payload); - - messageTypeContext.Value = (messageType.ToString(), consumerType.ToString()); + var messageType = activity.Tags + .Where(c => c.Key == DiagnosticHeaders.MessageTypes) + .Select(c => c.Value) + .FirstOrDefault(); + PrometheusCounters.ReceiveMessageCount + .WithLabels(messageType) + .Observe(activity.Duration.TotalSeconds); } break; - } - } - - public override void OnStopActivity(Activity activity, object payload) - { - switch (activity.OperationName) - { - case "Transport.Send": + case OperationName.Consumer.Consume: { - var exchangeType = exchangeContext.Value; - PrometheusCounters.SendMessageCount - .WithLabels(exchangeType) + var consumerType = activity.Tags + .Where(c => c.Key == DiagnosticHeaders.ConsumerType) + .Select(c => c.Value) + .FirstOrDefault(); + + PrometheusCounters.ConsumeMessageCount + .WithLabels(consumerType) .Observe(activity.Duration.TotalSeconds); } break; - case "Consumer.Consume": + case OperationName.Consumer.Handle: { - var (messageType, consumerType) = messageTypeContext.Value; + //var (messageType, consumerType) = messageTypeContext.Value; - PrometheusCounters.ConsumeMessageCount - .WithLabels(consumerType, messageType) - .Observe(activity.Duration.TotalSeconds); + //PrometheusCounters.ConsumeMessageCount + // .WithLabels(consumerType, messageType) + // .Observe(activity.Duration.TotalSeconds); } break; } @@ -92,10 +96,13 @@ public override void OnException(Activity activity, object payload) { switch (activity.OperationName) { - case "Transport.Send": + case OperationName.Transport.Send: PrometheusCounters.SendMessageErrors.Inc(); break; - case "Consumer.Consume": + case OperationName.Transport.Receive: + PrometheusCounters.ReceiveMessageError.Inc(); + break; + case OperationName.Consumer.Consume: PrometheusCounters.ConsumeMessageError.Inc(); break; } diff --git a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj index a72a064..6b9a21c 100644 --- a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj +++ b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj @@ -7,7 +7,7 @@ - + From 4a67780f496731d1a6cca0e1e7bd97aa23591d71 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 13:07:00 +0200 Subject: [PATCH 05/13] fix consumer handle --- samples/WebApp/MassTransit/TestConsumer.cs | 3 ++- .../Diagnostics/MassTransitListenerHandler.cs | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/samples/WebApp/MassTransit/TestConsumer.cs b/samples/WebApp/MassTransit/TestConsumer.cs index ab3da5b..f5e68a2 100644 --- a/samples/WebApp/MassTransit/TestConsumer.cs +++ b/samples/WebApp/MassTransit/TestConsumer.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using MassTransit; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; diff --git a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs index 5128e06..19c5aef 100644 --- a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs +++ b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs @@ -82,11 +82,8 @@ public override void OnStopActivity(Activity activity, object payload) break; case OperationName.Consumer.Handle: { - //var (messageType, consumerType) = messageTypeContext.Value; - - //PrometheusCounters.ConsumeMessageCount - // .WithLabels(consumerType, messageType) - // .Observe(activity.Duration.TotalSeconds); + PrometheusCounters.ConsumeMessageCount + .Observe(activity.Duration.TotalSeconds); } break; } From 9d6fcf600da6bed9864f40248b3f3bbe4f838c56 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 13:17:52 +0200 Subject: [PATCH 06/13] added observers --- .../DiagnosticServiceCollectionExtensions.cs | 9 ++++ .../Diagnostics/MassTransitListenerHandler.cs | 10 ++-- .../Observers/PrometheusConsumeObserver.cs | 52 +++++++++++++++++++ .../Observers/PrometheusPublishObserver.cs | 44 ++++++++++++++++ .../Observers/PrometheusSendObserver.cs | 43 +++++++++++++++ .../prometheus-net.MassTransit.csproj | 6 ++- 6 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 src/prometheus-net.MassTransit/Observers/PrometheusConsumeObserver.cs create mode 100644 src/prometheus-net.MassTransit/Observers/PrometheusPublishObserver.cs create mode 100644 src/prometheus-net.MassTransit/Observers/PrometheusSendObserver.cs diff --git a/src/prometheus-net.MassTransit/DiagnosticServiceCollectionExtensions.cs b/src/prometheus-net.MassTransit/DiagnosticServiceCollectionExtensions.cs index 36ff07b..d10672e 100644 --- a/src/prometheus-net.MassTransit/DiagnosticServiceCollectionExtensions.cs +++ b/src/prometheus-net.MassTransit/DiagnosticServiceCollectionExtensions.cs @@ -1,5 +1,7 @@ +using MassTransit; using Prometheus.Contrib.Core; using Prometheus.MassTransit.Diagnostics; +using Prometheus.MassTransit.Observers; namespace Microsoft.Extensions.DependencyInjection { @@ -14,5 +16,12 @@ public static void AddPrometheusMassTransitMetrics(this IServiceCollection servi services.AddSingleton(aspNetCoreListenerHandler); } + + public static void ConnectPrometheusObservers(this IBusControl busControl) + { + busControl.ConnectPublishObserver(new PrometheusPublishObserver()); + busControl.ConnectReceiveObserver(new PrometheusConsumeObserver()); + busControl.ConnectSendObserver(new PrometheusSendObserver()); + } } } diff --git a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs index 19c5aef..8d8dd9b 100644 --- a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs +++ b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs @@ -23,16 +23,16 @@ private static class PrometheusCounters "The time to receive a message, in seconds.", new HistogramConfiguration { LabelNames = new[] { "message" } }); - public static readonly Histogram ConsumeMessageCount = Metrics.CreateHistogram( - "masstransit_messages_consumed_total", - "The time to consume a message, in seconds.", - new HistogramConfiguration { LabelNames = new[] { "consumer" } }); - public static readonly Counter ReceiveMessageError = Metrics.CreateCounter( "masstransit_messages_received_errors_total", "The number of message processing failures.", new CounterConfiguration { LabelNames = new[] { "exception" } }); + public static readonly Histogram ConsumeMessageCount = Metrics.CreateHistogram( + "masstransit_messages_consumed_total", + "The time to consume a message, in seconds.", + new HistogramConfiguration { LabelNames = new[] { "consumer" } }); + public static readonly Counter ConsumeMessageError = Metrics.CreateCounter( "masstransit_messages_consumed_errors_total", "The number of message processing failures.", diff --git a/src/prometheus-net.MassTransit/Observers/PrometheusConsumeObserver.cs b/src/prometheus-net.MassTransit/Observers/PrometheusConsumeObserver.cs new file mode 100644 index 0000000..19213bd --- /dev/null +++ b/src/prometheus-net.MassTransit/Observers/PrometheusConsumeObserver.cs @@ -0,0 +1,52 @@ +using System; +using System.Threading.Tasks; +using MassTransit; + +namespace Prometheus.MassTransit.Observers +{ + public class PrometheusConsumeObserver : IReceiveObserver + { + private static readonly Histogram ConsumeTimer = Metrics.CreateHistogram( + "masstransit_messages_consumed_total", + "The time to consume a message, in seconds.", + new HistogramConfiguration { LabelNames = new[] { "message" } }); + + private static readonly Histogram CriticalTimer = Metrics.CreateHistogram( + "masstransit_critical_time_seconds", + "The time between when message is sent and when it is consumed, in seconds.", + new HistogramConfiguration { LabelNames = new[] { "message" } }); + + private static readonly Counter ErrorCounter = Metrics.CreateCounter( + "masstransit_messages_consumed_errors_total", + "The number of message processing failures.", + new CounterConfiguration { LabelNames = new[] { "exception" } }); + + public Task PreReceive(ReceiveContext context) => Task.CompletedTask; + public Task PostReceive(ReceiveContext context) => Task.CompletedTask; + public Task ReceiveFault(ReceiveContext context, Exception exception) => Task.CompletedTask; + + public Task PostConsume(ConsumeContext context, TimeSpan duration, string consumerType) where T : class + { + var messageType = typeof(T).ToString(); + + ConsumeTimer.WithLabels(messageType).Observe(duration.TotalSeconds); + + if (context.SentTime != null) + { + CriticalTimer.WithLabels(messageType) + .Observe((DateTime.UtcNow - context.SentTime.Value).TotalSeconds); + } + + return Task.CompletedTask; + } + + public Task ConsumeFault(ConsumeContext context, TimeSpan duration, string consumerType, Exception exception) where T : class + { + var messageType = typeof(T).ToString(); + + ErrorCounter.WithLabels(messageType).Inc(); + + return Task.CompletedTask; + } + } +} diff --git a/src/prometheus-net.MassTransit/Observers/PrometheusPublishObserver.cs b/src/prometheus-net.MassTransit/Observers/PrometheusPublishObserver.cs new file mode 100644 index 0000000..60ecfb2 --- /dev/null +++ b/src/prometheus-net.MassTransit/Observers/PrometheusPublishObserver.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading.Tasks; +using MassTransit; + +namespace Prometheus.MassTransit.Observers +{ + public class PrometheusPublishObserver : IPublishObserver + { + private static readonly Counter SentMessagesCount = Metrics.CreateCounter( + "masstransit_messages_sent_total", + "Total published messages.", + new CounterConfiguration { LabelNames = new[] { "operation", "message" } }); + + private static readonly Counter SentMessagesErrors = Metrics.CreateCounter( + "masstransit_messages_sent_errors_total", + "Total published messages errors", + new CounterConfiguration { LabelNames = new[] { "operation", "exception" } }); + + public Task PrePublish(PublishContext context) + where T : class + { + return Task.CompletedTask; + } + + public Task PostPublish(PublishContext context) + where T : class + { + SentMessagesCount + .WithLabels("Publish", context.Message.GetType().ToString()) + .Inc(); + return Task.CompletedTask; + } + + public Task PublishFault(PublishContext context, Exception exception) + where T : class + { + SentMessagesErrors + .WithLabels("Publish", exception.GetType().Name) + .Inc(); + + return Task.CompletedTask; + } + } +} diff --git a/src/prometheus-net.MassTransit/Observers/PrometheusSendObserver.cs b/src/prometheus-net.MassTransit/Observers/PrometheusSendObserver.cs new file mode 100644 index 0000000..d764c61 --- /dev/null +++ b/src/prometheus-net.MassTransit/Observers/PrometheusSendObserver.cs @@ -0,0 +1,43 @@ +using System; +using System.Threading.Tasks; +using MassTransit; + +namespace Prometheus.MassTransit.Observers +{ + public class PrometheusSendObserver : ISendObserver + { + private static readonly Counter SentMessagesCount = Metrics.CreateCounter( + "masstransit_messages_sent_total", + "Total published messages.", + new CounterConfiguration { LabelNames = new[] { "operation", "message" } }); + + private static readonly Counter SentMessagesErrors = Metrics.CreateCounter( + "masstransit_messages_sent_errors_total", + "Total published messages errors", + new CounterConfiguration { LabelNames = new[] { "operation", "exception" } }); + + public Task PreSend(SendContext context) + where T : class + { + return Task.CompletedTask; + } + + public Task PostSend(SendContext context) + where T : class + { + SentMessagesCount + .WithLabels("Send", context.Message.GetType().FullName) + .Inc(); + return Task.CompletedTask; + } + + public Task SendFault(SendContext context, Exception exception) + where T : class + { + SentMessagesErrors + .WithLabels("Send", exception.GetType().Name) + .Inc(); + return Task.CompletedTask; + } + } +} diff --git a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj index 6b9a21c..9b9f258 100644 --- a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj +++ b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.0 @@ -14,4 +14,8 @@ + + + + From f444fa38b0018b436fb2a5560a08c4f8eeae2cd8 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 13:30:26 +0200 Subject: [PATCH 07/13] fix --- .github/workflows/publish-nuget-packages.yaml | 2 +- .github/workflows/run-tests.yaml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish-nuget-packages.yaml b/.github/workflows/publish-nuget-packages.yaml index 3aa5a45..5dc1f34 100644 --- a/.github/workflows/publish-nuget-packages.yaml +++ b/.github/workflows/publish-nuget-packages.yaml @@ -11,6 +11,6 @@ jobs: - uses: actions/checkout@v1 - uses: actions/setup-dotnet@v1 with: - dotnet-version: '3.0.100' + dotnet-version: '3.1.101' - run: dotnet pack prometheus-net.Contrib.sln --include-symbols -c "Release" --output "build/" - run: dotnet nuget push "build/*.symbols.nupkg" -k ${{ secrets.NUGET_API_KEY }} -s "https://api.nuget.org/v3/index.json" -n true diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 96a37ec..2bf2e22 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -14,7 +14,6 @@ jobs: - uses: actions/checkout@v1 - uses: actions/setup-dotnet@v1 with: - dotnet-version: 3.0.100 + dotnet-version: '3.1.101' - name: dotnet test run: dotnet test -c "Release" - From 29d80f6885212a6a3c2ac62b12799561351f564f Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 13:32:29 +0200 Subject: [PATCH 08/13] clean csproj --- .../prometheus-net.MassTransit.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj index 9b9f258..e2f56a9 100644 --- a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj +++ b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj @@ -14,8 +14,4 @@ - - - - From 38cd200f8e9c16ce10971894a155bfcf46a28b2e Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 14:18:08 +0200 Subject: [PATCH 09/13] state machine saga metrics --- prometheus-net.Contrib.sln | 2 + samples/WebApp/Controllers/TestController.cs | 18 ++++++ .../WebApp/MassTransit/OrderStateMachine.cs | 52 ++++++++++++++++ samples/WebApp/Startup.cs | 9 +++ .../Diagnostics/MassTransitListenerHandler.cs | 62 +++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 samples/WebApp/MassTransit/OrderStateMachine.cs diff --git a/prometheus-net.Contrib.sln b/prometheus-net.Contrib.sln index 40e9786..9690d31 100644 --- a/prometheus-net.Contrib.sln +++ b/prometheus-net.Contrib.sln @@ -23,7 +23,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "prometheus-net.Esquio", "sr EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AC951721-6E15-4E0A-A15A-E268AA6DDAEA}" ProjectSection(SolutionItems) = preProject + .github\workflows\publish-nuget-packages.yaml = .github\workflows\publish-nuget-packages.yaml README.md = README.md + .github\workflows\run-tests.yaml = .github\workflows\run-tests.yaml EndProjectSection EndProject Global diff --git a/samples/WebApp/Controllers/TestController.cs b/samples/WebApp/Controllers/TestController.cs index 7ad3f9a..97e82e5 100644 --- a/samples/WebApp/Controllers/TestController.cs +++ b/samples/WebApp/Controllers/TestController.cs @@ -31,5 +31,23 @@ await endpoint.Publish(new TestCommand return Ok(); } + + [HttpGet("saga")] + public async Task TestSaga() + { + var orderId = Guid.NewGuid(); + + await endpoint.Publish(new + { + OrderId = orderId + }); + + await endpoint.Publish(new + { + OrderId = orderId + }); + + return Ok(); + } } } diff --git a/samples/WebApp/MassTransit/OrderStateMachine.cs b/samples/WebApp/MassTransit/OrderStateMachine.cs new file mode 100644 index 0000000..a391120 --- /dev/null +++ b/samples/WebApp/MassTransit/OrderStateMachine.cs @@ -0,0 +1,52 @@ +using System; +using Automatonymous; + +namespace WebApp.MassTransit +{ + public class OrderStateMachine : MassTransitStateMachine + { + public OrderStateMachine() + { + InstanceState(x => x.CurrentState); + + Event(() => SubmitOrder, x => x.CorrelateById(context => context.Message.OrderId)); + Event(() => OrderAccepted, x => x.CorrelateById(context => context.Message.OrderId)); + + Initially( + When(SubmitOrder) + .TransitionTo(Submitted)); + + During(Submitted, + When(OrderAccepted) + .Then(context => + { + + }) + .TransitionTo(Accepted) + .Finalize()); + } + + public Event SubmitOrder { get; private set; } + public Event OrderAccepted { get; private set; } + + + public State Submitted { get; private set; } + public State Accepted { get; private set; } + } + + public class OrderState : SagaStateMachineInstance + { + public Guid CorrelationId { get; set; } + public string CurrentState { get; set; } + } + + public interface SubmitOrder + { + Guid OrderId { get; } + } + + public interface OrderAccepted + { + Guid OrderId { get; } + } +} diff --git a/samples/WebApp/Startup.cs b/samples/WebApp/Startup.cs index 22a8938..42d8545 100644 --- a/samples/WebApp/Startup.cs +++ b/samples/WebApp/Startup.cs @@ -8,6 +8,7 @@ using Prometheus; using MassTransit; using System; +using MassTransit.Saga; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Diagnostics.HealthChecks; using WebApp.Data; @@ -33,6 +34,8 @@ public void ConfigureServices(IServiceCollection services) options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")); }); + services.AddSingleton>(provider => new InMemorySagaRepository()); + services.AddMassTransit( provider => Bus.Factory.CreateUsingRabbitMq(factoryConfigurator => { @@ -42,10 +45,16 @@ public void ConfigureServices(IServiceCollection services) { receiveEndpointConfigurator.Consumer(provider); }); + + factoryConfigurator.ReceiveEndpoint("test_saga", receiveEndpointConfigurator => + { + receiveEndpointConfigurator.StateMachineSaga(provider); + }); }), config => { config.AddConsumer(); + config.AddSagaStateMachine(); }, options => options.FailureStatus = HealthStatus.Unhealthy); diff --git a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs index 8d8dd9b..fad739a 100644 --- a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs +++ b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs @@ -37,6 +37,20 @@ private static class PrometheusCounters "masstransit_messages_consumed_errors_total", "The number of message processing failures.", new CounterConfiguration { LabelNames = new[] { "exception" } }); + + public static readonly Counter SagaRaisedEvents = Metrics.CreateCounter( + "masstransit_saga_raised_events_total", + "The time to receive a message, in seconds.", + new CounterConfiguration { LabelNames = new[] { "saga", "from", "to" } }); + + public static readonly Counter SagaSentEvents = Metrics.CreateCounter( + "masstransit_saga_sent_events_total", + "The time to receive a message, in seconds.", + new CounterConfiguration { LabelNames = new[] { "saga" } }); + + public static readonly Counter SagaSentQuery = Metrics.CreateCounter( + "masstransit_saga_sent_queries_total", + "The time to receive a message, in seconds."); } public MassTransitListenerHandler(string sourceName) : base(sourceName) @@ -86,6 +100,54 @@ public override void OnStopActivity(Activity activity, object payload) .Observe(activity.Duration.TotalSeconds); } break; + + case OperationName.Saga.Send: + { + var sagaType = activity.Tags.Where(c => c.Key == DiagnosticHeaders.SagaType).Select(c => c.Value).FirstOrDefault(); + + PrometheusCounters.SagaSentEvents + .WithLabels(sagaType) + .Inc(); + } + break; + case OperationName.Saga.SendQuery: + { + PrometheusCounters.SagaSentQuery.Inc(); + } + break; + case OperationName.Saga.RaiseEvent: + { + var sagaType = activity.Tags.Where(c => c.Key == DiagnosticHeaders.SagaType).Select(c => c.Value).FirstOrDefault(); + var beginState = activity.Tags.Where(c => c.Key == DiagnosticHeaders.BeginState).Select(c => c.Value).FirstOrDefault(); + var endState = activity.Tags.Where(c => c.Key == DiagnosticHeaders.EndState).Select(c => c.Value).FirstOrDefault(); + + PrometheusCounters.SagaRaisedEvents + .WithLabels(sagaType, beginState, endState) + .Inc(); + } + break; + + case OperationName.Saga.Initiate: + { + } + break; + case OperationName.Saga.Orchestrate: + { + } + break; + case OperationName.Saga.Observe: + { + } + break; + + case OperationName.Courier.Execute: + { + } + break; + case OperationName.Courier.Compensate: + { + } + break; } } From 5beefbdbb4103674525c05963383234a9b250485 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sat, 18 Jan 2020 14:43:28 +0200 Subject: [PATCH 10/13] activity example --- samples/WebApp/Controllers/TestController.cs | 26 +++++++--- samples/WebApp/MassTransit/TestCourier.cs | 48 +++++++++++++++++++ samples/WebApp/Startup.cs | 17 ++++++- .../Diagnostics/MassTransitListenerHandler.cs | 20 ++++++++ 4 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 samples/WebApp/MassTransit/TestCourier.cs diff --git a/samples/WebApp/Controllers/TestController.cs b/samples/WebApp/Controllers/TestController.cs index 97e82e5..b8a9b2c 100644 --- a/samples/WebApp/Controllers/TestController.cs +++ b/samples/WebApp/Controllers/TestController.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading.Tasks; using MassTransit; +using MassTransit.Courier; using Microsoft.AspNetCore.Mvc; using WebApp.MassTransit; @@ -11,17 +12,17 @@ namespace WebApp.Controllers [Route("[controller]")] public class TestController : ControllerBase { - private readonly IPublishEndpoint endpoint; + private readonly IBus bus; - public TestController(IPublishEndpoint endpoint) + public TestController(IBus bus) { - this.endpoint = endpoint; + this.bus = bus; } [HttpGet("send")] public async Task TestSend() { - await endpoint.Publish(new TestCommand + await bus.Publish(new TestCommand { Id = Guid.NewGuid() }); @@ -37,17 +38,30 @@ public async Task TestSaga() { var orderId = Guid.NewGuid(); - await endpoint.Publish(new + await bus.Publish(new { OrderId = orderId }); - await endpoint.Publish(new + await bus.Publish(new { OrderId = orderId }); return Ok(); } + + [HttpGet("courier")] + public async Task TestCourier() + { + var builder = new RoutingSlipBuilder(NewId.NextGuid()); + builder.AddActivity("DownloadImage", new Uri("queue:test_courier_execute")); + builder.AddActivity("FilterImage", new Uri("queue:test_courier_execute")); + var routingSlip = builder.Build(); + + await bus.Execute(routingSlip); + + return Ok(); + } } } diff --git a/samples/WebApp/MassTransit/TestCourier.cs b/samples/WebApp/MassTransit/TestCourier.cs new file mode 100644 index 0000000..1d874c7 --- /dev/null +++ b/samples/WebApp/MassTransit/TestCourier.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using MassTransit.Courier; + +namespace WebApp.MassTransit +{ + public class DownloadImageActivity : IActivity + { + public async Task Execute(ExecuteContext context) + { + return default; + } + + public async Task Compensate(CompensateContext context) + { + return default; + } + } + + public class FilterImageActivity : IActivity + { + public async Task Execute(ExecuteContext context) + { + return default; + } + + public async Task Compensate(CompensateContext context) + { + return default; + } + } + + public class FilterImageLog + { + } + + public class FilterImageArguments + { + } + + public class DownloadImageLog + { + } + + public class DownloadImageArguments + { + } +} diff --git a/samples/WebApp/Startup.cs b/samples/WebApp/Startup.cs index 42d8545..be6ece5 100644 --- a/samples/WebApp/Startup.cs +++ b/samples/WebApp/Startup.cs @@ -8,6 +8,7 @@ using Prometheus; using MassTransit; using System; +using GreenPipes; using MassTransit.Saga; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Diagnostics.HealthChecks; @@ -50,13 +51,27 @@ public void ConfigureServices(IServiceCollection services) { receiveEndpointConfigurator.StateMachineSaga(provider); }); + + factoryConfigurator.ReceiveEndpoint("test_courier_execute", receiveEndpointConfigurator => + { + var compnsateUri = new Uri("queue:test_courier_compensate"); + receiveEndpointConfigurator.ExecuteActivityHost(compnsateUri, provider); + receiveEndpointConfigurator.ExecuteActivityHost(compnsateUri, provider); + }); + + factoryConfigurator.ReceiveEndpoint("test_courier_compensate", receiveEndpointConfigurator => + { + receiveEndpointConfigurator.CompensateActivityHost(provider); + }); }), config => { config.AddConsumer(); config.AddSagaStateMachine(); - }, options => options.FailureStatus = HealthStatus.Unhealthy); + config.AddActivity(); + config.AddActivity(); + }, options => options.FailureStatus = HealthStatus.Unhealthy); services.AddPrometheusAspNetCoreMetrics(); services.AddPrometheusMassTransitMetrics(); diff --git a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs index fad739a..40b935a 100644 --- a/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs +++ b/src/prometheus-net.MassTransit/Diagnostics/MassTransitListenerHandler.cs @@ -51,6 +51,16 @@ private static class PrometheusCounters public static readonly Counter SagaSentQuery = Metrics.CreateCounter( "masstransit_saga_sent_queries_total", "The time to receive a message, in seconds."); + + public static readonly Counter CourierExecute = Metrics.CreateCounter( + "masstransit_courier_executed_total", + "The time to receive a message, in seconds.", + new CounterConfiguration { LabelNames = new[] { "activity" } }); + + public static readonly Counter CourierCompensate = Metrics.CreateCounter( + "masstransit_courier_compensate_total", + "The time to receive a message, in seconds.", + new CounterConfiguration { LabelNames = new[] { "activity" } }); } public MassTransitListenerHandler(string sourceName) : base(sourceName) @@ -142,10 +152,20 @@ public override void OnStopActivity(Activity activity, object payload) case OperationName.Courier.Execute: { + var activityType = activity.Tags.Where(c => c.Key == DiagnosticHeaders.ActivityType).Select(c => c.Value).FirstOrDefault(); + + PrometheusCounters.CourierExecute + .WithLabels(activityType) + .Inc(); } break; case OperationName.Courier.Compensate: { + var activityType = activity.Tags.Where(c => c.Key == DiagnosticHeaders.ActivityType).Select(c => c.Value).FirstOrDefault(); + + PrometheusCounters.CourierCompensate + .WithLabels(activityType) + .Inc(); } break; } From 5e1ceb6dd02605da9c3fcc2d6de233e889db7b68 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sun, 2 Feb 2020 12:24:56 +0200 Subject: [PATCH 11/13] update masstransit version --- samples/WebApp/WebApp.csproj | 4 ++-- .../prometheus-net.MassTransit.csproj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/WebApp/WebApp.csproj b/samples/WebApp/WebApp.csproj index 6eee1a0..d80ff9c 100644 --- a/samples/WebApp/WebApp.csproj +++ b/samples/WebApp/WebApp.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj index e2f56a9..2cb9258 100644 --- a/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj +++ b/src/prometheus-net.MassTransit/prometheus-net.MassTransit.csproj @@ -7,7 +7,7 @@ - + From bca0fba3d97478dcd34d6ac5e5852f75a694dae1 Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sun, 2 Feb 2020 12:25:43 +0200 Subject: [PATCH 12/13] fix version --- Directory.Build.props | 2 +- prometheus-net.Contrib.sln | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 9ebc670..19ee783 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,6 +7,6 @@ alexvaluyskiy alexvaluyskiy true - 0.6.36 + 0.7.0 diff --git a/prometheus-net.Contrib.sln b/prometheus-net.Contrib.sln index 9690d31..65657b6 100644 --- a/prometheus-net.Contrib.sln +++ b/prometheus-net.Contrib.sln @@ -23,6 +23,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "prometheus-net.Esquio", "sr EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AC951721-6E15-4E0A-A15A-E268AA6DDAEA}" ProjectSection(SolutionItems) = preProject + Directory.Build.props = Directory.Build.props .github\workflows\publish-nuget-packages.yaml = .github\workflows\publish-nuget-packages.yaml README.md = README.md .github\workflows\run-tests.yaml = .github\workflows\run-tests.yaml From 5de23bb75ef58111a9139f38283eca1f8982876b Mon Sep 17 00:00:00 2001 From: Oleksii Valuiskyi Date: Sun, 2 Feb 2020 12:28:11 +0200 Subject: [PATCH 13/13] fix nuget description --- Directory.Build.props | 2 ++ src/prometheus-net.Contrib/prometheus-net.Contrib.csproj | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 19ee783..150e7af 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -8,5 +8,7 @@ alexvaluyskiy true 0.7.0 + prometheus metrics + Exposes .NET core diagnostic listeners and counters diff --git a/src/prometheus-net.Contrib/prometheus-net.Contrib.csproj b/src/prometheus-net.Contrib/prometheus-net.Contrib.csproj index ccbce75..1a16b4c 100644 --- a/src/prometheus-net.Contrib/prometheus-net.Contrib.csproj +++ b/src/prometheus-net.Contrib/prometheus-net.Contrib.csproj @@ -5,7 +5,6 @@ Prometheus.Contrib prometheus-net.Contrib prometheus-net.Contrib - prometheus metrics