From a5871df386bb4335d53fba6a9ef0fb6291edb2a5 Mon Sep 17 00:00:00 2001 From: Phong Nguyen Date: Tue, 21 Jan 2025 10:09:32 +0700 Subject: [PATCH] Message Bus: Distributed Tracing --- .../Logging/ActivityExtensions.cs} | 43 +++++++------------ .../Logging/ILogger.cs | 5 --- .../RabbitMQ/RabbitMQReceiver.cs | 1 + .../GlobalExceptionHandler.cs | 2 +- .../GlobalExceptionHandlerMiddleware.cs | 2 +- .../Logging/ActivityExtensions.cs} | 43 +++++++------------ .../Logging/ILogger.cs | 5 --- .../RabbitMQ/RabbitMQReceiver.cs | 1 + .../GlobalExceptionHandler.cs | 2 +- .../GlobalExceptionHandlerMiddleware.cs | 2 +- 10 files changed, 38 insertions(+), 68 deletions(-) rename src/Microservices/Common/{ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs => ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs} (54%) delete mode 100644 src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs rename src/ModularMonolith/{ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs => ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs} (54%) delete mode 100644 src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs b/src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs similarity index 54% rename from src/Microservices/Common/ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs rename to src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs index cc2decab4..9065e2c3b 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs +++ b/src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs @@ -1,32 +1,8 @@ -/* - * https://github.com/serilog/serilog-aspnetcore/issues/207 - * - */ +using System.Diagnostics; -using Serilog.Core; -using Serilog.Events; -using System.Diagnostics; +namespace ClassifiedAds.CrossCuttingConcerns.Logging; -namespace ClassifiedAds.Infrastructure.Logging; - -public class ActivityEnricher : ILogEventEnricher -{ - public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) - { - var activity = Activity.Current; - - if (activity == null) - { - return; - } - - logEvent.AddPropertyIfAbsent(new LogEventProperty("SpanId", new ScalarValue(activity.GetSpanId()))); - logEvent.AddPropertyIfAbsent(new LogEventProperty("TraceId", new ScalarValue(activity.GetTraceId()))); - logEvent.AddPropertyIfAbsent(new LogEventProperty("ParentId", new ScalarValue(activity.GetParentId()))); - } -} - -internal static class ActivityExtensions +public static class ActivityExtensions { public static string GetSpanId(this Activity activity) { @@ -57,4 +33,17 @@ public static string GetParentId(this Activity activity) _ => null, } ?? string.Empty; } + + public static Activity StartNew(string name, string parentId = null) + { + var activity = new Activity(name); + + if (!string.IsNullOrEmpty(parentId)) + { + activity.SetParentId(parentId); + } + + activity.Start(); + return activity; + } } diff --git a/src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs b/src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs deleted file mode 100644 index a79da2a03..000000000 --- a/src/Microservices/Common/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace ClassifiedAds.CrossCuttingConcerns.Logging; - -public interface ILogger -{ -} diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs b/src/Microservices/Common/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs index 7e0ae797c..d22d7fc95 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs @@ -86,6 +86,7 @@ public async Task ReceiveAsync(Func action, CancellationToken await _channel.QueueDeclareAsync(_options.QueueName, true, false, false, arguments, cancellationToken: cancellationToken); await _channel.QueueBindAsync(_options.QueueName, _options.ExchangeName, _options.RoutingKey, null, cancellationToken: cancellationToken); + await _channel.QueueBindAsync(_options.QueueName, "amq.direct", $"direct_route_to_queue_{_options.QueueName}", null, cancellationToken: cancellationToken); } await _channel.BasicQosAsync(prefetchSize: 0, prefetchCount: 1, global: false, cancellationToken: cancellationToken); diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs index 3ae8a2984..0b1f544b2 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs @@ -1,5 +1,5 @@ using ClassifiedAds.CrossCuttingConcerns.Exceptions; -using ClassifiedAds.Infrastructure.Logging; +using ClassifiedAds.CrossCuttingConcerns.Logging; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs index 20fc6a7d3..e5f90d907 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs @@ -1,5 +1,5 @@ using ClassifiedAds.CrossCuttingConcerns.Exceptions; -using ClassifiedAds.Infrastructure.Logging; +using ClassifiedAds.CrossCuttingConcerns.Logging; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs b/src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs similarity index 54% rename from src/ModularMonolith/ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs rename to src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs index cc2decab4..9065e2c3b 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/Logging/ActivityEnricher.cs +++ b/src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ActivityExtensions.cs @@ -1,32 +1,8 @@ -/* - * https://github.com/serilog/serilog-aspnetcore/issues/207 - * - */ +using System.Diagnostics; -using Serilog.Core; -using Serilog.Events; -using System.Diagnostics; +namespace ClassifiedAds.CrossCuttingConcerns.Logging; -namespace ClassifiedAds.Infrastructure.Logging; - -public class ActivityEnricher : ILogEventEnricher -{ - public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) - { - var activity = Activity.Current; - - if (activity == null) - { - return; - } - - logEvent.AddPropertyIfAbsent(new LogEventProperty("SpanId", new ScalarValue(activity.GetSpanId()))); - logEvent.AddPropertyIfAbsent(new LogEventProperty("TraceId", new ScalarValue(activity.GetTraceId()))); - logEvent.AddPropertyIfAbsent(new LogEventProperty("ParentId", new ScalarValue(activity.GetParentId()))); - } -} - -internal static class ActivityExtensions +public static class ActivityExtensions { public static string GetSpanId(this Activity activity) { @@ -57,4 +33,17 @@ public static string GetParentId(this Activity activity) _ => null, } ?? string.Empty; } + + public static Activity StartNew(string name, string parentId = null) + { + var activity = new Activity(name); + + if (!string.IsNullOrEmpty(parentId)) + { + activity.SetParentId(parentId); + } + + activity.Start(); + return activity; + } } diff --git a/src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs b/src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs deleted file mode 100644 index a79da2a03..000000000 --- a/src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Logging/ILogger.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace ClassifiedAds.CrossCuttingConcerns.Logging; - -public interface ILogger -{ -} diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs b/src/ModularMonolith/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs index 7e0ae797c..d22d7fc95 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/MessageBrokers/RabbitMQ/RabbitMQReceiver.cs @@ -86,6 +86,7 @@ public async Task ReceiveAsync(Func action, CancellationToken await _channel.QueueDeclareAsync(_options.QueueName, true, false, false, arguments, cancellationToken: cancellationToken); await _channel.QueueBindAsync(_options.QueueName, _options.ExchangeName, _options.RoutingKey, null, cancellationToken: cancellationToken); + await _channel.QueueBindAsync(_options.QueueName, "amq.direct", $"direct_route_to_queue_{_options.QueueName}", null, cancellationToken: cancellationToken); } await _channel.BasicQosAsync(prefetchSize: 0, prefetchCount: 1, global: false, cancellationToken: cancellationToken); diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs index 3ae8a2984..0b1f544b2 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs @@ -1,5 +1,5 @@ using ClassifiedAds.CrossCuttingConcerns.Exceptions; -using ClassifiedAds.Infrastructure.Logging; +using ClassifiedAds.CrossCuttingConcerns.Logging; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs index 20fc6a7d3..e5f90d907 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/GlobalExceptionHandlerMiddleware.cs @@ -1,5 +1,5 @@ using ClassifiedAds.CrossCuttingConcerns.Exceptions; -using ClassifiedAds.Infrastructure.Logging; +using ClassifiedAds.CrossCuttingConcerns.Logging; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging;