From 9eac6b06b80aae1258e11a798d5b06a34e1e3855 Mon Sep 17 00:00:00 2001 From: "jan.jansen" Date: Wed, 13 Jan 2021 15:30:24 +0100 Subject: [PATCH] Make tracing sources configureable Signed-off-by: jan.jansen --- shared.csproj | 2 +- .../OpenTelemetryOptions.cs | 4 ++++ .../TracingDelegatingMessageHandler.cs | 6 +++--- .../TracingHostBuilderExtensions.cs | 2 +- .../GenericHostingTests.cs | 7 ++++++- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/shared.csproj b/shared.csproj index 0557f246..456f52bb 100644 --- a/shared.csproj +++ b/shared.csproj @@ -1,7 +1,7 @@ - 0.5.2 + 0.5.3 9 enable CS8600;CS8602;CS8625;CS8618;CS8604;CS8601 diff --git a/src/Motor.Extensions.Diagnostics.Tracing/OpenTelemetryOptions.cs b/src/Motor.Extensions.Diagnostics.Tracing/OpenTelemetryOptions.cs index 9ba6e5a5..a9090ba5 100644 --- a/src/Motor.Extensions.Diagnostics.Tracing/OpenTelemetryOptions.cs +++ b/src/Motor.Extensions.Diagnostics.Tracing/OpenTelemetryOptions.cs @@ -1,7 +1,11 @@ +using System.Collections.Generic; + namespace Motor.Extensions.Diagnostics.Tracing { public record OpenTelemetryOptions { + public static readonly string DefaultActivitySourceName = "MotorNet.OpenTelemetry"; public double SamplingProbability { get; set; } = 0.0001; + public List Sources { get; set; } = new() {DefaultActivitySourceName}; } } diff --git a/src/Motor.Extensions.Diagnostics.Tracing/TracingDelegatingMessageHandler.cs b/src/Motor.Extensions.Diagnostics.Tracing/TracingDelegatingMessageHandler.cs index 1d2a317b..1b9446df 100644 --- a/src/Motor.Extensions.Diagnostics.Tracing/TracingDelegatingMessageHandler.cs +++ b/src/Motor.Extensions.Diagnostics.Tracing/TracingDelegatingMessageHandler.cs @@ -14,8 +14,8 @@ public class TracingDelegatingMessageHandler : DelegatingMessageHandler< { private readonly ILogger> _logger; - private static readonly ActivitySource _activitySource = - new(typeof(TracingDelegatingMessageHandler<>).FullName!); + private static readonly ActivitySource ActivitySource = + new(OpenTelemetryOptions.DefaultActivitySourceName); public TracingDelegatingMessageHandler(ILogger> logger) { @@ -33,7 +33,7 @@ public override async Task HandleMessageAsync(MotorCloud { parentContext = extension.GetActivityContext(); } - using var activity = _activitySource.StartActivity(nameof(HandleMessageAsync), ActivityKind.Server, parentContext); + using var activity = ActivitySource.StartActivity(nameof(HandleMessageAsync), ActivityKind.Server, parentContext); if (activity == null) return await base.HandleMessageAsync(dataCloudEvent, token); using (activity.Start()) diff --git a/src/Motor.Extensions.Diagnostics.Tracing/TracingHostBuilderExtensions.cs b/src/Motor.Extensions.Diagnostics.Tracing/TracingHostBuilderExtensions.cs index 51ca03d0..5f378c39 100644 --- a/src/Motor.Extensions.Diagnostics.Tracing/TracingHostBuilderExtensions.cs +++ b/src/Motor.Extensions.Diagnostics.Tracing/TracingHostBuilderExtensions.cs @@ -43,7 +43,7 @@ public static IMotorHostBuilder ConfigureJaegerTracing(this IMotorHostBuilder ho builder .AddAspNetCoreInstrumentation() - .AddSource(typeof(TracingDelegatingMessageHandler<>).FullName!) + .AddSource(openTelemetryOptions.Sources.ToArray()) .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(applicationNameService.GetFullName(), serviceVersion: applicationNameService.GetVersion())) .SetMotorSampler(openTelemetryOptions) .AddExporter(logger, jaegerOptions.Value, applicationNameService, hostContext); diff --git a/test/Motor.Extensions.Hosting_IntegrationTest/GenericHostingTests.cs b/test/Motor.Extensions.Hosting_IntegrationTest/GenericHostingTests.cs index e3774f1e..a53d619f 100644 --- a/test/Motor.Extensions.Hosting_IntegrationTest/GenericHostingTests.cs +++ b/test/Motor.Extensions.Hosting_IntegrationTest/GenericHostingTests.cs @@ -50,7 +50,7 @@ public GenericHostingTests(RabbitMQFixture fixture, ITestOutputHelper outputHelp _outputHelper = outputHelper; _listener = new ActivityListener { - ShouldListenTo = source => source.Name == typeof(TracingDelegatingMessageHandler<>).FullName, + ShouldListenTo = source => source.Name == OpenTelemetryOptions.DefaultActivitySourceName, Sample = (ref ActivityCreationOptions options) => ActivitySamplingResult.AllDataAndRecorded, ActivityStarted = _ => { _outputHelper.WriteLine("test"); }, @@ -301,6 +301,7 @@ protected class ReverseStringConverter : ISingleOutputService { private readonly ILogger _logger; private readonly IMetricFamily _summary; + private static ActivitySource ActivitySource = new(OpenTelemetryOptions.DefaultActivitySourceName); public ReverseStringConverter(ILogger logger, IMetricsFactory metricsFactory) @@ -314,6 +315,10 @@ public Task> ConvertMessageAsync(MotorCloudEvent { _logger.LogInformation("log your request"); var tmpChar = dataCloudEvent.TypedData.ToCharArray(); + if (!ActivitySource.HasListeners()) + { + throw new ArgumentException(); + } var reversed = tmpChar.Reverse().ToArray(); _summary.WithLabels("collect_your_metrics").Observe(1.0); return Task.FromResult(dataCloudEvent.CreateNew(new string(reversed)));