diff --git a/src/Instrumentation/InstrumentationBuilderExtensions.cs b/src/Instrumentation/InstrumentationBuilderExtensions.cs index d070ed4..7dc50e5 100644 --- a/src/Instrumentation/InstrumentationBuilderExtensions.cs +++ b/src/Instrumentation/InstrumentationBuilderExtensions.cs @@ -13,6 +13,7 @@ public static class InstrumentationBuilderExtensions public static TracerProviderBuilder AddInMemoryMessagingInstrumentation(this TracerProviderBuilder builder) { builder.AddSource(InMemoryMessagingTraceInstrumentation.InstrumentationName); + InMemoryMessagingTraceInstrumentation.IsEnabled = true; return builder; } diff --git a/src/Instrumentation/Trace/InMemoryMessagingTraceInstrumentation.cs b/src/Instrumentation/Trace/InMemoryMessagingTraceInstrumentation.cs index 1e9c048..004ca47 100644 --- a/src/Instrumentation/Trace/InMemoryMessagingTraceInstrumentation.cs +++ b/src/Instrumentation/Trace/InMemoryMessagingTraceInstrumentation.cs @@ -13,9 +13,9 @@ internal struct InMemoryMessagingTraceInstrumentation internal const string InstrumentationName = "InMemoryMessaging"; /// - /// The key to add/read the id of activity (parent trace and span) to/from the publishing/received events. + /// Determines whether the instrumentation is enabled or not. /// - public const string TraceParentIdKey = "TraceParentId"; + public static bool IsEnabled { get; internal set; } /// /// The activity source to create a new activity @@ -27,10 +27,12 @@ internal struct InMemoryMessagingTraceInstrumentation /// /// Name of new activity /// Type of new activity. The default is - /// The id of activity (parent trace and span) to assign. Example: "{version}-{trace-id}-{parent-span-id}-{trace-flags}" /// Newly created an open telemetry activity - internal static Activity StartActivity(string name, ActivityKind kind = ActivityKind.Internal, string traceParentId = null) + internal static Activity StartActivity(string name, ActivityKind kind = ActivityKind.Producer) { + if (!IsEnabled) return null; + + var traceParentId = Activity.Current?.Id; ActivityContext.TryParse(traceParentId, null, out ActivityContext parentContext); var activity = ActivitySource.StartActivity(name, kind, parentContext); diff --git a/src/Managers/MessageManager.cs b/src/Managers/MessageManager.cs index c7e615b..41a76d3 100644 --- a/src/Managers/MessageManager.cs +++ b/src/Managers/MessageManager.cs @@ -50,8 +50,7 @@ public async Task PublishAsync(TMessage message) where TMessage : clas try { - var traceParentId = Activity.Current?.Id; - using var activity = InMemoryMessagingTraceInstrumentation.StartActivity($"DomainEvent: Executing handlers of the '{messageName}' memory message.", ActivityKind.Producer, traceParentId); + using var activity = InMemoryMessagingTraceInstrumentation.StartActivity($"DomainEvent: Executing handler(s) of the '{messageName}' memory message."); OnExecutingReceivedMessage(message);