diff --git a/docs/trace/building-your-own-exporter/MyExporter.cs b/docs/trace/building-your-own-exporter/MyExporter.cs index 3cec8d15673..45f6a4a2a19 100644 --- a/docs/trace/building-your-own-exporter/MyExporter.cs +++ b/docs/trace/building-your-own-exporter/MyExporter.cs @@ -31,7 +31,7 @@ public override Task ExportAsync( // telemetry should do so inside SuppressInstrumentation // scope. This suppresses telemetry from // exporter's own code to avoid live-loop situation. - using var scope = Sdk.SuppressInstrumentation.Begin(); + using var scope = SuppressInstrumentationScope.Begin(); foreach (var activity in batch) { diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index b9cfbb392ea..d511455ed29 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -12,7 +12,8 @@ match the spec ([#1013](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1013)) * Added `SuppressInstrumentationScope` API - ([#988](https://github.com/open-telemetry/opentelemetry-dotnet/pull/988)) + ([#988](https://github.com/open-telemetry/opentelemetry-dotnet/pull/988) + [#1067](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1067)) * Changed `BroadcastActivityProcessor` to `FanOutActivityProcessor` ([#1015](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1015)) * Changed `TracerProviderBuilder` and `TracerProviderSdk` design to simply the diff --git a/src/OpenTelemetry/Sdk.cs b/src/OpenTelemetry/Sdk.cs index 3a1dc5e0998..b1f3728381c 100644 --- a/src/OpenTelemetry/Sdk.cs +++ b/src/OpenTelemetry/Sdk.cs @@ -29,10 +29,13 @@ namespace OpenTelemetry /// public static class Sdk { - public static readonly SuppressInstrumentationScope SuppressInstrumentation = new SuppressInstrumentationScope(false); - private static readonly TimeSpan DefaultPushInterval = TimeSpan.FromSeconds(60); + /// + /// Gets a value indicating whether instrumentation is suppressed (disabled). + /// + public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed; + /// /// Creates MeterProvider with the configuration provided. /// Configuration involves MetricProcessor, Exporter and push internval. diff --git a/src/OpenTelemetry/SuppressInstrumentationScope.cs b/src/OpenTelemetry/SuppressInstrumentationScope.cs index 1eae39b4295..aadc5442efb 100644 --- a/src/OpenTelemetry/SuppressInstrumentationScope.cs +++ b/src/OpenTelemetry/SuppressInstrumentationScope.cs @@ -32,7 +32,7 @@ internal SuppressInstrumentationScope(bool value = true) Slot.Set(value); } - public static implicit operator bool(SuppressInstrumentationScope unused) => Slot.Get(); + internal static bool IsSuppressed => Slot.Get(); /// /// Begins a new scope in which instrumentation is suppressed (disabled). @@ -43,20 +43,20 @@ internal SuppressInstrumentationScope(bool value = true) /// This is typically used to prevent infinite loops created by /// collection of internal operations, such as exporting traces over HTTP. /// - /// public override async Task<ExportResult> ExportAsync( - /// IEnumerable<Activity> batch, - /// CancellationToken cancellationToken) - /// { - /// using (Sdk.SuppressInstrumentation.Begin()) - /// { - /// // Instrumentation is suppressed (i.e., Sdk.SuppressInstrumentation == true) - /// } + /// public override async Task<ExportResult> ExportAsync( + /// IEnumerable<Activity> batch, + /// CancellationToken cancellationToken) + /// { + /// using (SuppressInstrumentationScope.Begin()) + /// { + /// // Instrumentation is suppressed (i.e., Sdk.SuppressInstrumentation == true) + /// } /// - /// // Instrumentation is not suppressed (i.e., Sdk.SuppressInstrumentation == false) - /// } + /// // Instrumentation is not suppressed (i.e., Sdk.SuppressInstrumentation == false) + /// } /// /// - public IDisposable Begin(bool value = true) + public static IDisposable Begin(bool value = true) { return new SuppressInstrumentationScope(value); } diff --git a/test/OpenTelemetry.Tests/SuppressInstrumentationTest.cs b/test/OpenTelemetry.Tests/SuppressInstrumentationTest.cs index 6cf6c2849f8..2cc10d3a71a 100644 --- a/test/OpenTelemetry.Tests/SuppressInstrumentationTest.cs +++ b/test/OpenTelemetry.Tests/SuppressInstrumentationTest.cs @@ -23,11 +23,13 @@ public class SuppressInstrumentationTest [Fact] public static void UsingSuppressInstrumentation() { - using (var scope = Sdk.SuppressInstrumentation.Begin()) + Assert.False(Sdk.SuppressInstrumentation); + + using (var scope = SuppressInstrumentationScope.Begin()) { Assert.True(Sdk.SuppressInstrumentation); - using (var innerScope = Sdk.SuppressInstrumentation.Begin()) + using (var innerScope = SuppressInstrumentationScope.Begin()) { innerScope.Dispose();