From 844b103da26c2f150ab019275aee74f9fbb0f6be Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 13 Mar 2024 19:53:50 -0400 Subject: [PATCH 01/28] config driven sampler config with default fallback --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 62 +++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 927285a4ee0..785b0f63976 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Internal; using OpenTelemetry.Resources; @@ -13,6 +14,9 @@ namespace OpenTelemetry.Trace; internal sealed class TracerProviderSdk : TracerProvider { + internal const string TracesSamplerConfigKey = "OTEL_TRACES_SAMPLER"; + internal const string TracesSamplerArgConfigKey = "OTEL_TRACES_SAMPLER_ARG"; + internal readonly IServiceProvider ServiceProvider; internal readonly IDisposable? OwnedServiceProvider; internal int ShutdownCount; @@ -57,7 +61,7 @@ internal TracerProviderSdk( resourceBuilder.ServiceProvider = serviceProvider; this.Resource = resourceBuilder.Build(); - this.sampler = state.Sampler ?? new ParentBasedSampler(new AlwaysOnSampler()); + this.sampler = state.Sampler ?? ResolveSamplerFromSpecificationConfigurationKeys(serviceProvider!.GetRequiredService()); OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Sampler added = \"{this.sampler.GetType()}\"."); this.supportLegacyActivity = state.LegacyActivityOperationNames.Count > 0; @@ -401,6 +405,62 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } + private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfiguration configuration) + { + Sampler? sampler = null; + + if (configuration.TryGetStringValue(TracesSamplerConfigKey, out var tracesSampler)) + { + if (string.Equals(tracesSampler, "always_on", StringComparison.OrdinalIgnoreCase)) + { + sampler = new AlwaysOnSampler(); + } + else if (string.Equals(tracesSampler, "always_off", StringComparison.OrdinalIgnoreCase)) + { + sampler = new AlwaysOffSampler(); + } + else if (string.Equals(tracesSampler, "traceidratio", StringComparison.OrdinalIgnoreCase)) + { + var traceIdRatio = ReadTraceIdRatio(configuration); + sampler = new TraceIdRatioBasedSampler(traceIdRatio); + } + else if (string.Equals(tracesSampler, "parentbased_always_on", StringComparison.OrdinalIgnoreCase)) + { + sampler = new ParentBasedSampler(new AlwaysOnSampler()); + } + else if (string.Equals(tracesSampler, "parentbased_always_off", StringComparison.OrdinalIgnoreCase)) + { + sampler = new ParentBasedSampler(new AlwaysOffSampler()); + } + else if (string.Equals(tracesSampler, "parentbased_traceidratio", StringComparison.OrdinalIgnoreCase)) + { + var traceIdRatio = ReadTraceIdRatio(configuration); + sampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(traceIdRatio)); + } + else + { + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler configuration was found but the value '{tracesSampler}' is invalid and will be ignored."); + } + } + + return sampler ?? new ParentBasedSampler(new AlwaysOnSampler()); + } + + private static double ReadTraceIdRatio(IConfiguration configuration) + { + if (configuration.TryGetStringValue(TracesSamplerArgConfigKey, out var configValue) && + double.TryParse(configValue, out var traceIdRatio)) + { + return traceIdRatio; + } + else + { + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler argument configuration was found but the value '{configValue}' is invalid and will be ignored."); + } + + return 1.0; + } + private static ActivitySamplingResult ComputeActivitySamplingResult( ref ActivityCreationOptions options, Sampler sampler) From 746d139458fd9a2608ca7da5e386c051962a005f Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 13 Mar 2024 19:54:12 -0400 Subject: [PATCH 02/28] basic test cases --- .../Trace/TracerProviderSdkTest.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index b78744b49b5..ded8c03cb93 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 using System.Diagnostics; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Instrumentation; using OpenTelemetry.Resources; using OpenTelemetry.Resources.Tests; @@ -1039,6 +1041,47 @@ public void SdkPopulatesSamplingParamsCorrectlyForLegacyActivityWithInProcParent activity.Stop(); } + [Theory] + [InlineData(null, null, "ParentBased{AlwaysOnSampler}")] + [InlineData("always_on", null, "AlwaysOnSampler")] + [InlineData("always_off", null, "AlwaysOffSampler")] + [InlineData("always_OFF", null, "AlwaysOffSampler")] + [InlineData("traceidratio", "0.5", "TraceIdRatioBasedSampler{0.500000}")] + [InlineData("traceidratio", "not_a_double", "TraceIdRatioBasedSampler{1.000000}")] + [InlineData("parentbased_always_on", null, "ParentBased{AlwaysOnSampler}")] + [InlineData("parentbased_always_off", null, "ParentBased{AlwaysOffSampler}")] + [InlineData("parentbased_traceidratio", "0.111", "ParentBased{TraceIdRatioBasedSampler{0.111000}}")] + [InlineData("ParentBased_TraceIdRatio", "0.000001", "ParentBased{TraceIdRatioBasedSampler{0.000001}}")] + public void TestSamplerSetFromConfiguration(string? configValue, string? argValue, string samplerDescription) + { + var configBuilder = new ConfigurationBuilder(); + + if (!string.IsNullOrEmpty(configValue)) + { + configBuilder.AddInMemoryCollection(new Dictionary + { + [TracerProviderSdk.TracesSamplerConfigKey] = configValue, + }); + } + + if (!string.IsNullOrEmpty(argValue)) + { + configBuilder.AddInMemoryCollection(new Dictionary + { + [TracerProviderSdk.TracesSamplerArgConfigKey] = argValue, + }); + } + + var builder = Sdk.CreateTracerProviderBuilder(); + builder.ConfigureServices(s => s.AddSingleton(configBuilder.Build())); + using var tracerProvider = builder.Build(); + var tracerProviderSdk = tracerProvider as TracerProviderSdk; + + Assert.NotNull(tracerProviderSdk); + Assert.NotNull(tracerProviderSdk.Sampler); + Assert.Equal(samplerDescription, tracerProviderSdk.Sampler.Description); + } + [Fact] public void TracerProvideSdkCreatesAndDiposesInstrumentation() { From e3af439738c357a0f5691b7aee3924693df74502 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 11:27:12 -0400 Subject: [PATCH 03/28] additional log --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 785b0f63976..d0295ea1bd4 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -443,6 +443,11 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu } } + if (sampler != null) + { + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Sampler set to '{sampler.Description}' from configuration."); + } + return sampler ?? new ParentBasedSampler(new AlwaysOnSampler()); } From bbc4435536f4c378b39f9723d4348be7eb54fbdc Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 11:33:45 -0400 Subject: [PATCH 04/28] CHANGELOG entry --- src/OpenTelemetry/CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index b3e5a11e449..4bec998345d 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -47,6 +47,16 @@ [IMetricsListener](https://learn.microsoft.com/dotNet/api/microsoft.extensions.diagnostics.metrics.imetricslistener). ([#5265](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5265)) +* `TracerProvider`s can now have a sampler configured via the + `OTEL_TRACES_SAMPLER` environment variable. The supported values are: + `always_off`, `always_on`, `traceidratio`, `parentbased_always_on`, + `parentbased_always_off`, and `parentbased_traceidratio`. The options + `traceidratio` and `parentbased_traceidratio` may have the sampler probability + configured via the `OTEL_TRACES_SAMPLER_ARG` environment variable. + For details see: [OpenTelemetry Environment Variable + Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration). + ([#3601](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3601)) + * **Experimental (pre-release builds only):** The `Exemplar.FilteredTags` property now returns a `ReadOnlyFilteredTagCollection` instance and the `Exemplar.LongValue` property has been added. The `MetricPoint.GetExemplars` From de6e89ad2a5956e60e614451973a650a9833b5af Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 12:37:47 -0400 Subject: [PATCH 05/28] nullabe not needed --- test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index ded8c03cb93..c5f59047e5e 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -1052,13 +1052,13 @@ public void SdkPopulatesSamplingParamsCorrectlyForLegacyActivityWithInProcParent [InlineData("parentbased_always_off", null, "ParentBased{AlwaysOffSampler}")] [InlineData("parentbased_traceidratio", "0.111", "ParentBased{TraceIdRatioBasedSampler{0.111000}}")] [InlineData("ParentBased_TraceIdRatio", "0.000001", "ParentBased{TraceIdRatioBasedSampler{0.000001}}")] - public void TestSamplerSetFromConfiguration(string? configValue, string? argValue, string samplerDescription) + public void TestSamplerSetFromConfiguration(string configValue, string argValue, string samplerDescription) { var configBuilder = new ConfigurationBuilder(); if (!string.IsNullOrEmpty(configValue)) { - configBuilder.AddInMemoryCollection(new Dictionary + configBuilder.AddInMemoryCollection(new Dictionary { [TracerProviderSdk.TracesSamplerConfigKey] = configValue, }); @@ -1066,7 +1066,7 @@ public void TestSamplerSetFromConfiguration(string? configValue, string? argValu if (!string.IsNullOrEmpty(argValue)) { - configBuilder.AddInMemoryCollection(new Dictionary + configBuilder.AddInMemoryCollection(new Dictionary { [TracerProviderSdk.TracesSamplerArgConfigKey] = argValue, }); From 0e1d3a7bc7ef08357ed621b2accc3e429b576cd0 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 13:41:24 -0400 Subject: [PATCH 06/28] restructure for logging when sampler was previously set --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index d0295ea1bd4..be181245ab1 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -61,7 +61,7 @@ internal TracerProviderSdk( resourceBuilder.ServiceProvider = serviceProvider; this.Resource = resourceBuilder.Build(); - this.sampler = state.Sampler ?? ResolveSamplerFromSpecificationConfigurationKeys(serviceProvider!.GetRequiredService()); + this.sampler = ResolveSamplerFromSpecificationConfigurationKeys(serviceProvider!.GetRequiredService(), state.Sampler); OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Sampler added = \"{this.sampler.GetType()}\"."); this.supportLegacyActivity = state.LegacyActivityOperationNames.Count > 0; @@ -405,12 +405,24 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfiguration configuration) + private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfiguration configuration, Sampler stateSampler) { Sampler? sampler = null; + if (stateSampler != null) + { + sampler = stateSampler; + } + if (configuration.TryGetStringValue(TracesSamplerConfigKey, out var tracesSampler)) { + if (sampler != null) + { + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent( + $"Trace sampler configuration value '{tracesSampler}' has been ignored because a value '{sampler.Description}' was set previously."); + return sampler; + } + if (string.Equals(tracesSampler, "always_on", StringComparison.OrdinalIgnoreCase)) { sampler = new AlwaysOnSampler(); From cc2578fce9b110da9721cdfbb79c08511dab8f52 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 13:41:37 -0400 Subject: [PATCH 07/28] log message consistency --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index be181245ab1..180363a18f9 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -457,7 +457,7 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu if (sampler != null) { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Sampler set to '{sampler.Description}' from configuration."); + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.Description}' from configuration."); } return sampler ?? new ParentBasedSampler(new AlwaysOnSampler()); From 91e2155096ee025a5bab4c83677184734956e13e Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 15:55:20 -0400 Subject: [PATCH 08/28] make nullable explicit --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 180363a18f9..65c5d586e9c 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -405,7 +405,7 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfiguration configuration, Sampler stateSampler) + private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfiguration configuration, Sampler? stateSampler) { Sampler? sampler = null; From 9888beb2094d94908779fbcc1399a2a7011e9e34 Mon Sep 17 00:00:00 2001 From: Matt Hensley <130569+matt-hensley@users.noreply.github.com> Date: Thu, 14 Mar 2024 15:58:59 -0400 Subject: [PATCH 09/28] Update src/OpenTelemetry/Trace/TracerProviderSdk.cs Co-authored-by: Mikel Blanchard --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 65c5d586e9c..60932f51a99 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -453,12 +453,11 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler configuration was found but the value '{tracesSampler}' is invalid and will be ignored."); } - } - - if (sampler != null) - { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.Description}' from configuration."); - } + if (sampler != null) + { + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.Description}' from configuration."); + } + } return sampler ?? new ParentBasedSampler(new AlwaysOnSampler()); } From f9f9ffa2fdbc17ab58c9e59d027352bac72dd760 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 16:05:10 -0400 Subject: [PATCH 10/28] trailing spaces from GitHub editor --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 60932f51a99..8d6f0fb0607 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -457,7 +457,7 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.Description}' from configuration."); } - } + } return sampler ?? new ParentBasedSampler(new AlwaysOnSampler()); } From 36d6c9924fc5217a6b939331e1430b792f886de4 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 16:28:32 -0400 Subject: [PATCH 11/28] misc formatting --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 8d6f0fb0607..606dd99badf 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -453,6 +453,7 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler configuration was found but the value '{tracesSampler}' is invalid and will be ignored."); } + if (sampler != null) { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.Description}' from configuration."); From 236c14a055c7fda43b3d6c4abce4b869116535c4 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 17:41:07 -0400 Subject: [PATCH 12/28] adjust CHANGELOG entry --- src/OpenTelemetry/CHANGELOG.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 4bec998345d..d07cceedcaa 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -47,16 +47,6 @@ [IMetricsListener](https://learn.microsoft.com/dotNet/api/microsoft.extensions.diagnostics.metrics.imetricslistener). ([#5265](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5265)) -* `TracerProvider`s can now have a sampler configured via the - `OTEL_TRACES_SAMPLER` environment variable. The supported values are: - `always_off`, `always_on`, `traceidratio`, `parentbased_always_on`, - `parentbased_always_off`, and `parentbased_traceidratio`. The options - `traceidratio` and `parentbased_traceidratio` may have the sampler probability - configured via the `OTEL_TRACES_SAMPLER_ARG` environment variable. - For details see: [OpenTelemetry Environment Variable - Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration). - ([#3601](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3601)) - * **Experimental (pre-release builds only):** The `Exemplar.FilteredTags` property now returns a `ReadOnlyFilteredTagCollection` instance and the `Exemplar.LongValue` property has been added. The `MetricPoint.GetExemplars` @@ -87,6 +77,16 @@ Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#exemplar). ([#5412](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5412)) +* `TracerProvider`s can now have a sampler configured via the + `OTEL_TRACES_SAMPLER` environment variable. The supported values are: + `always_off`, `always_on`, `traceidratio`, `parentbased_always_on`, + `parentbased_always_off`, and `parentbased_traceidratio`. The options + `traceidratio` and `parentbased_traceidratio` may have the sampler probability + configured via the `OTEL_TRACES_SAMPLER_ARG` environment variable. + For details see: [OpenTelemetry Environment Variable + Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration). + ([#5448](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5448)) + ## 1.7.0 Released 2023-Dec-08 From bf84a97895dd58820d7ed1e71eeccb12d8bda6d4 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Thu, 14 Mar 2024 18:14:41 -0400 Subject: [PATCH 13/28] add test to ensure programmatic usage takes precedence --- .../Trace/TracerProviderSdkTest.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index c5f59047e5e..2b22ffb1242 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -1082,6 +1082,27 @@ public void TestSamplerSetFromConfiguration(string configValue, string argValue, Assert.Equal(samplerDescription, tracerProviderSdk.Sampler.Description); } + [Fact] + public void TestSamplerConfigurationIgnoredWhenSetProgrammatically() + { + var configBuilder = new ConfigurationBuilder(); + configBuilder.AddInMemoryCollection(new Dictionary + { + [TracerProviderSdk.TracesSamplerConfigKey] = "always_off", + }); + + var builder = Sdk.CreateTracerProviderBuilder(); + builder.ConfigureServices(s => s.AddSingleton(configBuilder.Build())); + builder.SetSampler(new AlwaysOnSampler()); + + using var tracerProvider = builder.Build(); + var tracerProviderSdk = tracerProvider as TracerProviderSdk; + + Assert.NotNull(tracerProviderSdk); + Assert.NotNull(tracerProviderSdk.Sampler); + Assert.Equal("AlwaysOnSampler", tracerProviderSdk.Sampler.Description); + } + [Fact] public void TracerProvideSdkCreatesAndDiposesInstrumentation() { From 4f4f7b143f13e0176f93a0f0a097d8ce038c251d Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Fri, 15 Mar 2024 14:56:26 -0400 Subject: [PATCH 14/28] default value for debugging --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 606dd99badf..ca90760aefe 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -472,7 +472,7 @@ private static double ReadTraceIdRatio(IConfiguration configuration) } else { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler argument configuration was found but the value '{configValue}' is invalid and will be ignored."); + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler argument configuration was found but the value '{configValue}' is invalid and will be ignored, default of value of '1.0' will be used."); } return 1.0; From 4ccfd1db3d0f988afd9077fe445fb7f3c636e25a Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Fri, 15 Mar 2024 15:02:14 -0400 Subject: [PATCH 15/28] log type info per feedback --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index ca90760aefe..1d1892eb969 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -419,7 +419,7 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu if (sampler != null) { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent( - $"Trace sampler configuration value '{tracesSampler}' has been ignored because a value '{sampler.Description}' was set previously."); + $"Trace sampler configuration value '{tracesSampler}' has been ignored because a value '{sampler.GetType().FullName}' was set previously."); return sampler; } @@ -456,7 +456,7 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu if (sampler != null) { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.Description}' from configuration."); + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler set to '{sampler.GetType().FullName}' from configuration."); } } From 1b3e472f6b05937a7bb8038318ff95a65ce991fb Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Fri, 15 Mar 2024 15:03:50 -0400 Subject: [PATCH 16/28] rename var --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 1d1892eb969..18e3258c156 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -414,44 +414,44 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu sampler = stateSampler; } - if (configuration.TryGetStringValue(TracesSamplerConfigKey, out var tracesSampler)) + if (configuration.TryGetStringValue(TracesSamplerConfigKey, out var configValue)) { if (sampler != null) { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent( - $"Trace sampler configuration value '{tracesSampler}' has been ignored because a value '{sampler.GetType().FullName}' was set previously."); + $"Trace sampler configuration value '{configValue}' has been ignored because a value '{sampler.GetType().FullName}' was set previously."); return sampler; } - if (string.Equals(tracesSampler, "always_on", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(configValue, "always_on", StringComparison.OrdinalIgnoreCase)) { sampler = new AlwaysOnSampler(); } - else if (string.Equals(tracesSampler, "always_off", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(configValue, "always_off", StringComparison.OrdinalIgnoreCase)) { sampler = new AlwaysOffSampler(); } - else if (string.Equals(tracesSampler, "traceidratio", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(configValue, "traceidratio", StringComparison.OrdinalIgnoreCase)) { var traceIdRatio = ReadTraceIdRatio(configuration); sampler = new TraceIdRatioBasedSampler(traceIdRatio); } - else if (string.Equals(tracesSampler, "parentbased_always_on", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(configValue, "parentbased_always_on", StringComparison.OrdinalIgnoreCase)) { sampler = new ParentBasedSampler(new AlwaysOnSampler()); } - else if (string.Equals(tracesSampler, "parentbased_always_off", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(configValue, "parentbased_always_off", StringComparison.OrdinalIgnoreCase)) { sampler = new ParentBasedSampler(new AlwaysOffSampler()); } - else if (string.Equals(tracesSampler, "parentbased_traceidratio", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(configValue, "parentbased_traceidratio", StringComparison.OrdinalIgnoreCase)) { var traceIdRatio = ReadTraceIdRatio(configuration); sampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(traceIdRatio)); } else { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler configuration was found but the value '{tracesSampler}' is invalid and will be ignored."); + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler configuration was found but the value '{configValue}' is invalid and will be ignored."); } if (sampler != null) From 4ac637e8752598f1180f6d6e0cbd20b7f03bcbac Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Fri, 15 Mar 2024 15:05:30 -0400 Subject: [PATCH 17/28] reference env var --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 18e3258c156..f6e72346574 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -451,7 +451,7 @@ private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfigu } else { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler configuration was found but the value '{configValue}' is invalid and will be ignored."); + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"OTEL_TRACES_SAMPLER configuration was found but the value '{configValue}' is invalid and will be ignored."); } if (sampler != null) From 098eff6dbe81114d1465a78ac3b386dd313e76a2 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Fri, 15 Mar 2024 15:06:12 -0400 Subject: [PATCH 18/28] rename method --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index f6e72346574..4d600667a78 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -61,7 +61,7 @@ internal TracerProviderSdk( resourceBuilder.ServiceProvider = serviceProvider; this.Resource = resourceBuilder.Build(); - this.sampler = ResolveSamplerFromSpecificationConfigurationKeys(serviceProvider!.GetRequiredService(), state.Sampler); + this.sampler = GetSampler(serviceProvider!.GetRequiredService(), state.Sampler); OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Sampler added = \"{this.sampler.GetType()}\"."); this.supportLegacyActivity = state.LegacyActivityOperationNames.Count > 0; @@ -405,7 +405,7 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - private static Sampler ResolveSamplerFromSpecificationConfigurationKeys(IConfiguration configuration, Sampler? stateSampler) + private static Sampler GetSampler(IConfiguration configuration, Sampler? stateSampler) { Sampler? sampler = null; From 779467b081bdd19773ec6362445010eb6bc32ff0 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 16:07:42 -0400 Subject: [PATCH 19/28] if/else -> switch per feedback --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 58 ++++++++++---------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 4d600667a78..be5de71714c 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -423,35 +423,37 @@ private static Sampler GetSampler(IConfiguration configuration, Sampler? stateSa return sampler; } - if (string.Equals(configValue, "always_on", StringComparison.OrdinalIgnoreCase)) + switch (configValue) { - sampler = new AlwaysOnSampler(); - } - else if (string.Equals(configValue, "always_off", StringComparison.OrdinalIgnoreCase)) - { - sampler = new AlwaysOffSampler(); - } - else if (string.Equals(configValue, "traceidratio", StringComparison.OrdinalIgnoreCase)) - { - var traceIdRatio = ReadTraceIdRatio(configuration); - sampler = new TraceIdRatioBasedSampler(traceIdRatio); - } - else if (string.Equals(configValue, "parentbased_always_on", StringComparison.OrdinalIgnoreCase)) - { - sampler = new ParentBasedSampler(new AlwaysOnSampler()); - } - else if (string.Equals(configValue, "parentbased_always_off", StringComparison.OrdinalIgnoreCase)) - { - sampler = new ParentBasedSampler(new AlwaysOffSampler()); - } - else if (string.Equals(configValue, "parentbased_traceidratio", StringComparison.OrdinalIgnoreCase)) - { - var traceIdRatio = ReadTraceIdRatio(configuration); - sampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(traceIdRatio)); - } - else - { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"OTEL_TRACES_SAMPLER configuration was found but the value '{configValue}' is invalid and will be ignored."); + case var _ when string.Equals(configValue, "always_on", StringComparison.OrdinalIgnoreCase): + sampler = new AlwaysOnSampler(); + break; + case var _ when string.Equals(configValue, "always_off", StringComparison.OrdinalIgnoreCase): + sampler = new AlwaysOffSampler(); + break; + case var _ when string.Equals(configValue, "traceidratio", StringComparison.OrdinalIgnoreCase): + { + var traceIdRatio = ReadTraceIdRatio(configuration); + sampler = new TraceIdRatioBasedSampler(traceIdRatio); + break; + } + + case var _ when string.Equals(configValue, "parentbased_always_on", StringComparison.OrdinalIgnoreCase): + sampler = new ParentBasedSampler(new AlwaysOnSampler()); + break; + case var _ when string.Equals(configValue, "parentbased_always_off", StringComparison.OrdinalIgnoreCase): + sampler = new ParentBasedSampler(new AlwaysOffSampler()); + break; + case var _ when string.Equals(configValue, "parentbased_traceidratio", StringComparison.OrdinalIgnoreCase): + { + var traceIdRatio = ReadTraceIdRatio(configuration); + sampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(traceIdRatio)); + break; + } + + default: + OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"OTEL_TRACES_SAMPLER configuration was found but the value '{configValue}' is invalid and will be ignored."); + break; } if (sampler != null) From a253144e526c6a80f2e11ece0bf001cc8c729214 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 16:11:07 -0400 Subject: [PATCH 20/28] document supported env vars --- docs/trace/customizing-the-sdk/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/trace/customizing-the-sdk/README.md b/docs/trace/customizing-the-sdk/README.md index 4d4a0c772e4..7a506cb7e23 100644 --- a/docs/trace/customizing-the-sdk/README.md +++ b/docs/trace/customizing-the-sdk/README.md @@ -363,6 +363,14 @@ var tracerProvider = Sdk.CreateTracerProviderBuilder() .Build(); ``` +It is also possible to configure the sampler by using the following +environmental variables: + +| Environment variable | Description | +| -------------------------- | -------------------------------------------------- | +| `OTEL_TRACES_SAMPLER` | Sampler to be used for traces. See the [General SDK Configuration specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration) for more details. | +| `OTEL_TRACES_SAMPLER_ARG` | String value to be used as the sampler argument. | + Follow [this](../extending-the-sdk/README.md#sampler) document to learn about writing custom samplers. From f12142f172bd6a2b198f33a066075b686ec460b6 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 16:15:27 -0400 Subject: [PATCH 21/28] document supported values. jaeger_remote and xray are not supported --- docs/trace/customizing-the-sdk/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/trace/customizing-the-sdk/README.md b/docs/trace/customizing-the-sdk/README.md index 7a506cb7e23..4ca8ef8f69b 100644 --- a/docs/trace/customizing-the-sdk/README.md +++ b/docs/trace/customizing-the-sdk/README.md @@ -371,6 +371,17 @@ environmental variables: | `OTEL_TRACES_SAMPLER` | Sampler to be used for traces. See the [General SDK Configuration specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration) for more details. | | `OTEL_TRACES_SAMPLER_ARG` | String value to be used as the sampler argument. | +The supported values for `OTEL_TRACES_SAMPLER` are: +* `always_off` +* `always_on` +* `traceidratio` +* `parentbased_always_on`, +* `parentbased_always_off` +* `parentbased_traceidratio` + +The options `traceidratio` and `parentbased_traceidratio` may have the sampler +probability configured via the `OTEL_TRACES_SAMPLER_ARG` environment variable. + Follow [this](../extending-the-sdk/README.md#sampler) document to learn about writing custom samplers. From 96b568f9c6f7a0513c24747882c3059d104ea052 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 16:21:00 -0400 Subject: [PATCH 22/28] add dedicated warning logging for invalid values --- .../Internal/OpenTelemetrySdkEventSource.cs | 12 ++++++++++++ src/OpenTelemetry/Trace/TracerProviderSdk.cs | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs b/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs index 38765609b54..97ddf776ae0 100644 --- a/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs +++ b/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs @@ -346,6 +346,18 @@ public void MetricInstrumentRemoved(string instrumentName, string meterName) this.WriteEvent(53, instrumentName, meterName); } + [Event(54, Message = "OTEL_TRACES_SAMPLER configuration was found but the value '{0}' is invalid and will be ignored.", Level = EventLevel.Warning)] + public void TracesSamplerConfigInvalid(string configValue) + { + this.WriteEvent(54, configValue); + } + + [Event(55, Message = "OTEL_TRACES_SAMPLER_ARG configuration was found but the value '{0}' is invalid and will be ignored, default of value of '1.0' will be used.", Level = EventLevel.Warning)] + public void TracesSamplerArgConfigInvalid(string configValue) + { + this.WriteEvent(54, configValue); + } + #if DEBUG public class OpenTelemetryEventListener : EventListener { diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index be5de71714c..869e738fcde 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -452,7 +452,7 @@ private static Sampler GetSampler(IConfiguration configuration, Sampler? stateSa } default: - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"OTEL_TRACES_SAMPLER configuration was found but the value '{configValue}' is invalid and will be ignored."); + OpenTelemetrySdkEventSource.Log.TracesSamplerConfigInvalid(configValue); break; } @@ -474,7 +474,7 @@ private static double ReadTraceIdRatio(IConfiguration configuration) } else { - OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent($"Trace sampler argument configuration was found but the value '{configValue}' is invalid and will be ignored, default of value of '1.0' will be used."); + OpenTelemetrySdkEventSource.Log.TracesSamplerArgConfigInvalid(configValue ?? string.Empty); } return 1.0; From 8035ca630e35e45cfd27f391b57f09e8301005f5 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 17:30:11 -0400 Subject: [PATCH 23/28] copy/paste error --- src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs b/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs index 97ddf776ae0..6e244cce5ed 100644 --- a/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs +++ b/src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs @@ -355,7 +355,7 @@ public void TracesSamplerConfigInvalid(string configValue) [Event(55, Message = "OTEL_TRACES_SAMPLER_ARG configuration was found but the value '{0}' is invalid and will be ignored, default of value of '1.0' will be used.", Level = EventLevel.Warning)] public void TracesSamplerArgConfigInvalid(string configValue) { - this.WriteEvent(54, configValue); + this.WriteEvent(55, configValue); } #if DEBUG From 282ee7407a9e0a5169a4952a0aaf1edaebc34426 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 17:33:50 -0400 Subject: [PATCH 24/28] language suggestion --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 869e738fcde..959009c5af2 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -419,7 +419,7 @@ private static Sampler GetSampler(IConfiguration configuration, Sampler? stateSa if (sampler != null) { OpenTelemetrySdkEventSource.Log.TracerProviderSdkEvent( - $"Trace sampler configuration value '{configValue}' has been ignored because a value '{sampler.GetType().FullName}' was set previously."); + $"Trace sampler configuration value '{configValue}' has been ignored because a value '{sampler.GetType().FullName}' was set programmatically."); return sampler; } From 6aea8ee281577e31c57de7184c6e0218761fef5b Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 17:33:59 -0400 Subject: [PATCH 25/28] nullable warning --- src/OpenTelemetry/Trace/TracerProviderSdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 959009c5af2..fc3fe02631a 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -452,7 +452,7 @@ private static Sampler GetSampler(IConfiguration configuration, Sampler? stateSa } default: - OpenTelemetrySdkEventSource.Log.TracesSamplerConfigInvalid(configValue); + OpenTelemetrySdkEventSource.Log.TracesSamplerConfigInvalid(configValue ?? string.Empty); break; } From 1ea40e87b6ddeaceb5f3652f0f74bd2c41ff6e8d Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 17:34:15 -0400 Subject: [PATCH 26/28] addtional test --- test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index 2b22ffb1242..6ca312c07ae 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -1051,6 +1051,7 @@ public void SdkPopulatesSamplingParamsCorrectlyForLegacyActivityWithInProcParent [InlineData("parentbased_always_on", null, "ParentBased{AlwaysOnSampler}")] [InlineData("parentbased_always_off", null, "ParentBased{AlwaysOffSampler}")] [InlineData("parentbased_traceidratio", "0.111", "ParentBased{TraceIdRatioBasedSampler{0.111000}}")] + [InlineData("parentbased_traceidratio", "not_a_double", "ParentBased{TraceIdRatioBasedSampler{1.000000}}")] [InlineData("ParentBased_TraceIdRatio", "0.000001", "ParentBased{TraceIdRatioBasedSampler{0.000001}}")] public void TestSamplerSetFromConfiguration(string configValue, string argValue, string samplerDescription) { From 29bc01eb279ef1d59aa4da7e1e28040fb964c104 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 17:43:05 -0400 Subject: [PATCH 27/28] adjust to always have config keys present --- .../Trace/TracerProviderSdkTest.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index 6ca312c07ae..9e6e7320371 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -1057,21 +1057,11 @@ public void TestSamplerSetFromConfiguration(string configValue, string argValue, { var configBuilder = new ConfigurationBuilder(); - if (!string.IsNullOrEmpty(configValue)) - { - configBuilder.AddInMemoryCollection(new Dictionary - { - [TracerProviderSdk.TracesSamplerConfigKey] = configValue, - }); - } - - if (!string.IsNullOrEmpty(argValue)) + configBuilder.AddInMemoryCollection(new Dictionary { - configBuilder.AddInMemoryCollection(new Dictionary - { - [TracerProviderSdk.TracesSamplerArgConfigKey] = argValue, - }); - } + [TracerProviderSdk.TracesSamplerConfigKey] = configValue, + [TracerProviderSdk.TracesSamplerArgConfigKey] = argValue, + }); var builder = Sdk.CreateTracerProviderBuilder(); builder.ConfigureServices(s => s.AddSingleton(configBuilder.Build())); From db9093d02a0ae9c036c9aecde15fa81ddd8b64ea Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 19 Mar 2024 17:55:56 -0400 Subject: [PATCH 28/28] markdown linter error --- docs/trace/customizing-the-sdk/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/trace/customizing-the-sdk/README.md b/docs/trace/customizing-the-sdk/README.md index 4ca8ef8f69b..01db0f6fd6d 100644 --- a/docs/trace/customizing-the-sdk/README.md +++ b/docs/trace/customizing-the-sdk/README.md @@ -372,6 +372,7 @@ environmental variables: | `OTEL_TRACES_SAMPLER_ARG` | String value to be used as the sampler argument. | The supported values for `OTEL_TRACES_SAMPLER` are: + * `always_off` * `always_on` * `traceidratio`