From 5f672e472ecc266188591635ed5b9c3e929fe14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 5 Apr 2024 11:56:53 +0200 Subject: [PATCH] OTEL_TRACES_SAMPLER and OTEL_TRACES_SAMPLER_ARG are handled by SDK --- .../Configurations/ConfigurationKeys.cs | 10 ---- .../EnvironmentConfigurationTracerHelper.cs | 18 -------- .../Configurations/TracerSamplerHelper.cs | 46 ------------------- .../Configurations/TracerSettings.cs | 13 ------ .../Configurations/SettingsTests.cs | 19 -------- .../TracerSamplerHelperTests.cs | 39 ---------------- 6 files changed, 145 deletions(-) delete mode 100644 src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSamplerHelper.cs delete mode 100644 test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/TracerSamplerHelperTests.cs diff --git a/src/OpenTelemetry.AutoInstrumentation/Configurations/ConfigurationKeys.cs b/src/OpenTelemetry.AutoInstrumentation/Configurations/ConfigurationKeys.cs index 79d7319bb3..784dc9d3ae 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Configurations/ConfigurationKeys.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Configurations/ConfigurationKeys.cs @@ -103,16 +103,6 @@ public static class Traces /// public const string AdditionalLegacySources = "OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_LEGACY_SOURCES"; - /// - /// Configuration key for sampler to be used for traces. - /// - public const string TracesSampler = "OTEL_TRACES_SAMPLER"; - - /// - /// Configuration key for string value to be used as the sampler argument. - /// - public const string TracesSamplerArguments = "OTEL_TRACES_SAMPLER_ARG"; - /// /// Configuration keys for instrumentation options. /// diff --git a/src/OpenTelemetry.AutoInstrumentation/Configurations/EnvironmentConfigurationTracerHelper.cs b/src/OpenTelemetry.AutoInstrumentation/Configurations/EnvironmentConfigurationTracerHelper.cs index 9db75043fd..46ff79c145 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Configurations/EnvironmentConfigurationTracerHelper.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Configurations/EnvironmentConfigurationTracerHelper.cs @@ -58,7 +58,6 @@ public static TracerProviderBuilder UseEnvironmentVariables( } builder - .SetSampler(settings) // Exporters can cause dependency loads. // Should be called later if dependency listeners are already setup. .SetExporter(settings, pluginManager) @@ -89,23 +88,6 @@ private static TracerProviderBuilder AddWcfIfNeeded( return tracerProviderBuilder; } - private static TracerProviderBuilder SetSampler(this TracerProviderBuilder builder, TracerSettings settings) - { - if (settings.TracesSampler == null) - { - return builder; - } - - var sampler = TracerSamplerHelper.GetSampler(settings.TracesSampler, settings.TracesSamplerArguments); - - if (sampler == null) - { - return builder; - } - - return builder.SetSampler(sampler); - } - private static TracerProviderBuilder SetExporter(this TracerProviderBuilder builder, TracerSettings settings, PluginManager pluginManager) { if (settings.ConsoleExporterEnabled) diff --git a/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSamplerHelper.cs b/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSamplerHelper.cs deleted file mode 100644 index 79dd2d1ba9..0000000000 --- a/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSamplerHelper.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using System.Globalization; -using OpenTelemetry.Trace; - -namespace OpenTelemetry.AutoInstrumentation.Configurations; - -internal static class TracerSamplerHelper -{ - public static Sampler? GetSampler(string tracerSampler, string? tracerSamplerArguments) - { - switch (tracerSampler) - { - case "always_on": - return new AlwaysOnSampler(); - case "always_off": - return new AlwaysOffSampler(); - case "traceidratio": - return CreateTraceIdRatioBasedSampler(tracerSamplerArguments); - case "parentbased_always_on": - return new ParentBasedSampler(new AlwaysOnSampler()); - case "parentbased_always_off": - return new ParentBasedSampler(new AlwaysOffSampler()); - case "parentbased_traceidratio": - return new ParentBasedSampler(CreateTraceIdRatioBasedSampler(tracerSamplerArguments)); - } - - return null; - } - - private static TraceIdRatioBasedSampler CreateTraceIdRatioBasedSampler(string? arguments) - { - const double defaultRatio = 1.0; - - var ratio = defaultRatio; - - if (double.TryParse(arguments, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedRatio) - && parsedRatio >= 0.0 && parsedRatio <= 1.0) - { - ratio = parsedRatio; - } - - return new TraceIdRatioBasedSampler(ratio); - } -} diff --git a/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSettings.cs b/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSettings.cs index 6a03d99004..37bcfbbcdc 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSettings.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Configurations/TracerSettings.cs @@ -52,16 +52,6 @@ internal class TracerSettings : Settings /// public InstrumentationOptions InstrumentationOptions { get; private set; } = new(new Configuration(failFast: false)); - /// - /// Gets sampler to be used for traces. - /// - public string? TracesSampler { get; private set; } - - /// - /// Gets a value to be used as the sampler argument. - /// - public string? TracesSamplerArguments { get; private set; } - protected override void OnLoad(Configuration configuration) { TracesExporter = ParseTracesExporter(configuration); @@ -97,9 +87,6 @@ protected override void OnLoad(Configuration configuration) OpenTracingEnabled = configuration.GetBool(ConfigurationKeys.Traces.OpenTracingEnabled) ?? false; InstrumentationOptions = new InstrumentationOptions(configuration); - - TracesSampler = configuration.GetString(ConfigurationKeys.Traces.TracesSampler); - TracesSamplerArguments = configuration.GetString(ConfigurationKeys.Traces.TracesSamplerArguments); } private static TracesExporter ParseTracesExporter(Configuration configuration) diff --git a/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/SettingsTests.cs b/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/SettingsTests.cs index 3a445d049a..609edad787 100644 --- a/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/SettingsTests.cs +++ b/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/SettingsTests.cs @@ -53,8 +53,6 @@ internal void TracerSettings_DefaultValues() settings.EnabledInstrumentations.Should().NotBeEmpty(); settings.ActivitySources.Should().BeEquivalentTo(new List { "OpenTelemetry.AutoInstrumentation.*" }); settings.AdditionalLegacySources.Should().BeEmpty(); - settings.TracesSampler.Should().BeNull(); - settings.TracesSamplerArguments.Should().BeNull(); // Instrumentation options tests settings.InstrumentationOptions.GraphQLSetDocument.Should().BeFalse(); @@ -252,21 +250,6 @@ internal void TracerSettings_Instrumentations_SupportedValues(string tracerInstr settings.EnabledInstrumentations.Should().BeEquivalentTo(new List { expectedTracerInstrumentation }); } - [Fact] - internal void TracerSettings_TracerSampler() - { - const string expectedTracesSampler = nameof(expectedTracesSampler); - const string expectedTracesSamplerArguments = nameof(expectedTracesSamplerArguments); - - Environment.SetEnvironmentVariable(ConfigurationKeys.Traces.TracesSampler, expectedTracesSampler); - Environment.SetEnvironmentVariable(ConfigurationKeys.Traces.TracesSamplerArguments, expectedTracesSamplerArguments); - - var settings = Settings.FromDefaultSources(false); - - settings.TracesSampler.Should().Be(expectedTracesSampler); - settings.TracesSamplerArguments.Should().Be(expectedTracesSamplerArguments); - } - [Theory] #if NETFRAMEWORK [InlineData("ASPNET", MetricInstrumentation.AspNet)] @@ -384,8 +367,6 @@ private static void ClearEnvVars() } Environment.SetEnvironmentVariable(ConfigurationKeys.Traces.Exporter, null); - Environment.SetEnvironmentVariable(ConfigurationKeys.Traces.TracesSampler, null); - Environment.SetEnvironmentVariable(ConfigurationKeys.Traces.TracesSamplerArguments, null); Environment.SetEnvironmentVariable(ConfigurationKeys.Traces.InstrumentationOptions.GraphQLSetDocument, null); Environment.SetEnvironmentVariable(ConfigurationKeys.ExporterOtlpProtocol, null); Environment.SetEnvironmentVariable(ConfigurationKeys.FlushOnUnhandledException, null); diff --git a/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/TracerSamplerHelperTests.cs b/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/TracerSamplerHelperTests.cs deleted file mode 100644 index 3c7f73b1d1..0000000000 --- a/test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/TracerSamplerHelperTests.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using FluentAssertions; -using OpenTelemetry.AutoInstrumentation.Configurations; -using Xunit; - -namespace OpenTelemetry.AutoInstrumentation.Tests.Configurations; - -public class TracerSamplerHelperTests -{ - [Theory] - [InlineData("always_on", null, "AlwaysOnSampler")] - [InlineData("always_off", null, "AlwaysOffSampler")] - [InlineData("traceidratio", null, "TraceIdRatioBasedSampler{1.000000}")] - [InlineData("traceidratio", "2", "TraceIdRatioBasedSampler{1.000000}")] - [InlineData("traceidratio", "-1", "TraceIdRatioBasedSampler{1.000000}")] - [InlineData("traceidratio", "non-a-number", "TraceIdRatioBasedSampler{1.000000}")] - [InlineData("traceidratio", "0.25", "TraceIdRatioBasedSampler{0.250000}")] - [InlineData("parentbased_always_on", null, "ParentBased{AlwaysOnSampler}")] - [InlineData("parentbased_always_off", null, "ParentBased{AlwaysOffSampler}")] - [InlineData("parentbased_traceidratio", null, "ParentBased{TraceIdRatioBasedSampler{1.000000}}")] - [InlineData("parentbased_traceidratio", "0.25", "ParentBased{TraceIdRatioBasedSampler{0.250000}}")] - public void GetSamplerSupportedValues(string tracesSampler, string? tracerSamplerArguments, string expectedDescription) - { - var sampler = TracerSamplerHelper.GetSampler(tracesSampler, tracerSamplerArguments); - - sampler.Should().NotBeNull(); - sampler!.Description.Should().Be(expectedDescription); - } - - [Fact] - public void GetSamplerNonSupportedValues() - { - var sampler = TracerSamplerHelper.GetSampler("non-supported-value", null); - - sampler.Should().BeNull(); - } -}