From 3b39975e25bd9369c2e2d1ed1df4da8544d8e532 Mon Sep 17 00:00:00 2001 From: Bart Koelman <104792814+bart-vmware@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:44:05 +0200 Subject: [PATCH 1/2] Update OpenTelemetry packages - EnableGrpcAspNetCoreSupport was replaced by environment variable OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION, see https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs - Replace usage of IDeferredTracerProviderBuilder, see https://github.com/open-telemetry/opentelemetry-dotnet/issues/4228 --- shared-package.props | 2 +- shared-test.props | 2 +- .../src/Abstractions/ITracingOptions.cs | 5 - src/Common/src/Common/TracingOptions.cs | 3 - .../Steeltoe.Management.Tracing.csproj | 6 +- .../TracingBaseServiceCollectionExtensions.cs | 115 +++++++++--------- .../TracingCoreServiceCollectionExtensions.cs | 7 +- .../src/Wavefront/Exporters/TagExtensions.cs | 4 +- versions.props | 7 +- 9 files changed, 71 insertions(+), 80 deletions(-) diff --git a/shared-package.props b/shared-package.props index e7955d65e0..ac5d92cbdc 100644 --- a/shared-package.props +++ b/shared-package.props @@ -1,6 +1,6 @@ - $(NoWarn);CS1591;NU5104 + $(NoWarn);CS1591 true diff --git a/shared-test.props b/shared-test.props index d7f9d60d94..1fdc957ac6 100644 --- a/shared-test.props +++ b/shared-test.props @@ -1,6 +1,6 @@ - $(NoWarn);S2094 + $(NoWarn);S2094;NU5104 diff --git a/src/Common/src/Abstractions/ITracingOptions.cs b/src/Common/src/Abstractions/ITracingOptions.cs index f3f78cdbfe..9dc52aae3b 100644 --- a/src/Common/src/Abstractions/ITracingOptions.cs +++ b/src/Common/src/Abstractions/ITracingOptions.cs @@ -66,11 +66,6 @@ public interface ITracingOptions /// bool SingleB3Header { get; } - /// - /// Gets a value indicating whether GRPC requests should participate in tracing. - /// - bool EnableGrpcAspNetCoreSupport { get; } - /// /// Gets a value representing the endpoint used for exporting traces. /// diff --git a/src/Common/src/Common/TracingOptions.cs b/src/Common/src/Common/TracingOptions.cs index 08a97a9a1e..f977aceb65 100644 --- a/src/Common/src/Common/TracingOptions.cs +++ b/src/Common/src/Common/TracingOptions.cs @@ -43,9 +43,6 @@ public class TracingOptions : ITracingOptions /// public bool SingleB3Header { get; set; } = true; - /// - public bool EnableGrpcAspNetCoreSupport { get; set; } = true; - /// public Uri ExporterEndpoint { get; set; } diff --git a/src/Management/src/Tracing/Steeltoe.Management.Tracing.csproj b/src/Management/src/Tracing/Steeltoe.Management.Tracing.csproj index 41372a6f74..795808854a 100644 --- a/src/Management/src/Tracing/Steeltoe.Management.Tracing.csproj +++ b/src/Management/src/Tracing/Steeltoe.Management.Tracing.csproj @@ -1,4 +1,4 @@ - + net8.0;net6.0 Enables request tracing in distributed systems. @@ -16,8 +16,8 @@ - - + + diff --git a/src/Management/src/Tracing/TracingBaseServiceCollectionExtensions.cs b/src/Management/src/Tracing/TracingBaseServiceCollectionExtensions.cs index c00c05e9c7..668ca9e3b8 100644 --- a/src/Management/src/Tracing/TracingBaseServiceCollectionExtensions.cs +++ b/src/Management/src/Tracing/TracingBaseServiceCollectionExtensions.cs @@ -8,9 +8,9 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using OpenTelemetry; using OpenTelemetry.Context.Propagation; using OpenTelemetry.Exporter; +using OpenTelemetry.Instrumentation.Http; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using Steeltoe.Common; @@ -19,6 +19,7 @@ using Steeltoe.Management.Endpoint; using Steeltoe.Management.Wavefront.Exporters; using B3Propagator = OpenTelemetry.Extensions.Propagators.B3Propagator; +using Sdk = OpenTelemetry.Sdk; namespace Steeltoe.Management.Tracing; @@ -60,6 +61,8 @@ public static IServiceCollection AddDistributedTracing(this IServiceCollection s services.TryAddSingleton(serviceProvider => new TracingOptions(serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService())); + services.ConfigureOptionsWithChangeTokenSource(); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); bool exportToZipkin = ReflectionHelpers.IsAssemblyLoaded("OpenTelemetry.Exporter.Zipkin"); @@ -81,69 +84,72 @@ public static IServiceCollection AddDistributedTracing(this IServiceCollection s ConfigureOpenTelemetryProtocolOptions(services); } - services.AddOpenTelemetry().WithTracing(builder => + services.AddOpenTelemetry().WithTracing(tracerProviderBuilder => { - (builder as IDeferredTracerProviderBuilder)?.Configure((serviceProvider, deferredBuilder) => + tracerProviderBuilder.AddHttpClientInstrumentation(); + + if (exportToZipkin) { - string appName = serviceProvider.GetRequiredService() - .GetApplicationNameInContext(SteeltoeComponent.Management, $"{TracingOptions.ConfigurationPrefix}:name"); + AddZipkinExporter(tracerProviderBuilder); + } - var tracingOptions = serviceProvider.GetRequiredService(); + if (exportToJaeger) + { + AddJaegerExporter(tracerProviderBuilder); + } - ILogger logger = serviceProvider.GetRequiredService() - .CreateLogger($"{typeof(TracingBaseServiceCollectionExtensions).Namespace}.Setup"); + if (exportToOpenTelemetryProtocol) + { + AddOpenTelemetryProtocolExporter(tracerProviderBuilder); + } - logger.LogTrace("Found Zipkin exporter: {exportToZipkin}. Found Jaeger exporter: {exportToJaeger}. Found OTLP exporter: {exportToOtlp}.", - exportToZipkin, exportToJaeger, exportToOpenTelemetryProtocol); + action?.Invoke(tracerProviderBuilder); + }); - deferredBuilder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(appName)); + services.AddOptions().Configure((options, serviceProvider) => + { + var tracingOptions = serviceProvider.GetRequiredService(); - deferredBuilder.AddHttpClientInstrumentation(options => - { - var pathMatcher = new Regex(tracingOptions.EgressIgnorePattern); - options.Filter += requestMessage => !pathMatcher.IsMatch(requestMessage.RequestUri?.PathAndQuery ?? string.Empty); - }); + var pathMatcher = new Regex(tracingOptions.EgressIgnorePattern); + options.FilterHttpRequestMessage += requestMessage => !pathMatcher.IsMatch(requestMessage.RequestUri?.PathAndQuery ?? string.Empty); + }); - if (tracingOptions.PropagationType.Equals("B3", StringComparison.OrdinalIgnoreCase)) - { - var propagators = new List - { - new B3Propagator(tracingOptions.SingleB3Header), - new BaggagePropagator() - }; + services.ConfigureOpenTelemetryTracerProvider((serviceProvider, tracerProviderBuilder) => + { + string appName = serviceProvider.GetRequiredService() + .GetApplicationNameInContext(SteeltoeComponent.Management, $"{TracingOptions.ConfigurationPrefix}:name"); - Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(propagators)); - } + var tracingOptions = serviceProvider.GetRequiredService(); - if (tracingOptions.NeverSample) - { - deferredBuilder.SetSampler(new AlwaysOffSampler()); - } - else if (tracingOptions.AlwaysSample) - { - deferredBuilder.SetSampler(new AlwaysOnSampler()); - } - }); + ILogger logger = serviceProvider.GetRequiredService() + .CreateLogger($"{typeof(TracingBaseServiceCollectionExtensions).Namespace}.Setup"); - if (exportToZipkin) + logger.LogTrace("Found Zipkin exporter: {exportToZipkin}. Found Jaeger exporter: {exportToJaeger}. Found OTLP exporter: {exportToOtlp}.", + exportToZipkin, exportToJaeger, exportToOpenTelemetryProtocol); + + tracerProviderBuilder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(appName)); + + if (tracingOptions.PropagationType.Equals("B3", StringComparison.OrdinalIgnoreCase)) { - AddZipkinExporter(builder); + var propagators = new List + { + new B3Propagator(tracingOptions.SingleB3Header), + new BaggagePropagator() + }; + + Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(propagators)); } - if (exportToJaeger) + if (tracingOptions.NeverSample) { - AddJaegerExporter(builder); + tracerProviderBuilder.SetSampler(new AlwaysOffSampler()); } - - if (exportToOpenTelemetryProtocol) + else if (tracingOptions.AlwaysSample) { - AddOpenTelemetryProtocolExporter(builder); + tracerProviderBuilder.SetSampler(new AlwaysOnSampler()); } - services.ConfigureOptionsWithChangeTokenSource(); - AddWavefrontExporter(builder); - - action?.Invoke(builder); + AddWavefrontExporter(tracerProviderBuilder, serviceProvider); }); return services; @@ -203,20 +209,15 @@ private static void AddOpenTelemetryProtocolExporter(TracerProviderBuilder build builder.AddOtlpExporter(); } - private static void AddWavefrontExporter(TracerProviderBuilder builder) + private static void AddWavefrontExporter(TracerProviderBuilder tracerProviderBuilder, IServiceProvider serviceProvider) { - var deferredTracerProviderBuilder = (IDeferredTracerProviderBuilder)builder; + var wavefrontOptions = serviceProvider.GetRequiredService>(); - deferredTracerProviderBuilder.Configure((serviceProvider, innerBuilder) => + // Only add if wavefront is configured + if (!string.IsNullOrEmpty(wavefrontOptions.Value.Uri)) { - var wavefrontOptions = serviceProvider.GetRequiredService>(); - - // Only add if wavefront is configured - if (!string.IsNullOrEmpty(wavefrontOptions.Value.Uri)) - { - var logger = serviceProvider.GetRequiredService>(); - innerBuilder.AddWavefrontTraceExporter(wavefrontOptions.Value, logger); - } - }); + var logger = serviceProvider.GetRequiredService>(); + tracerProviderBuilder.AddWavefrontTraceExporter(wavefrontOptions.Value, logger); + } } } diff --git a/src/Management/src/Tracing/TracingCoreServiceCollectionExtensions.cs b/src/Management/src/Tracing/TracingCoreServiceCollectionExtensions.cs index 2bcb41a44c..b015ab676f 100644 --- a/src/Management/src/Tracing/TracingCoreServiceCollectionExtensions.cs +++ b/src/Management/src/Tracing/TracingCoreServiceCollectionExtensions.cs @@ -44,11 +44,10 @@ public static IServiceCollection AddDistributedTracingAspNetCore(this IServiceCo action += builder => builder.AddAspNetCoreInstrumentation(); - services.AddOptions().PostConfigure((options, traceOpts) => + services.AddOptions().PostConfigure((instrumentationOptions, tracingOptions) => { - var pathMatcher = new Regex(traceOpts.IngressIgnorePattern); - options.EnableGrpcAspNetCoreSupport = traceOpts.EnableGrpcAspNetCoreSupport; - options.Filter += context => !pathMatcher.IsMatch(context.Request.Path); + var pathMatcher = new Regex(tracingOptions.IngressIgnorePattern); + instrumentationOptions.Filter += context => !pathMatcher.IsMatch(context.Request.Path); }); return services.AddDistributedTracing(action); diff --git a/src/Management/src/Wavefront/Exporters/TagExtensions.cs b/src/Management/src/Wavefront/Exporters/TagExtensions.cs index bfbb00baaf..8852bf1f21 100644 --- a/src/Management/src/Wavefront/Exporters/TagExtensions.cs +++ b/src/Management/src/Wavefront/Exporters/TagExtensions.cs @@ -12,9 +12,9 @@ internal static class TagExtensions { var tags = new Dictionary(); - foreach (KeyValuePair tag in tagCollection) + foreach (KeyValuePair tag in tagCollection) { - if (string.IsNullOrEmpty(tag.Key) || string.IsNullOrEmpty(tag.Value.ToString())) + if (string.IsNullOrEmpty(tag.Key) || tag.Value == null || string.IsNullOrEmpty(tag.Value.ToString())) { continue; } diff --git a/versions.props b/versions.props index 41d3b7e8dd..a4e5130b88 100644 --- a/versions.props +++ b/versions.props @@ -68,10 +68,9 @@ --> 8.0.* - 1.5.* - 1.4.0-rc.3 - 1.0.0-rc9.2 - 1.6.* + 1.6.*-* + 1.8.*-* + 1.8.* 7.0.* 7.0.* 4.1.* From 22c0a881cb9eaed451057dc4837b3f713c52086e Mon Sep 17 00:00:00 2001 From: Bart Koelman <104792814+bart-vmware@users.noreply.github.com> Date: Wed, 17 Apr 2024 12:49:24 +0200 Subject: [PATCH 2/2] Update src/Management/src/Wavefront/Exporters/TagExtensions.cs Co-authored-by: Tim Hess --- src/Management/src/Wavefront/Exporters/TagExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Management/src/Wavefront/Exporters/TagExtensions.cs b/src/Management/src/Wavefront/Exporters/TagExtensions.cs index 8852bf1f21..edf8b15f62 100644 --- a/src/Management/src/Wavefront/Exporters/TagExtensions.cs +++ b/src/Management/src/Wavefront/Exporters/TagExtensions.cs @@ -14,7 +14,7 @@ internal static class TagExtensions foreach (KeyValuePair tag in tagCollection) { - if (string.IsNullOrEmpty(tag.Key) || tag.Value == null || string.IsNullOrEmpty(tag.Value.ToString())) + if (string.IsNullOrEmpty(tag.Key) || string.IsNullOrEmpty(tag.Value?.ToString())) { continue; }