From 817891377377048a72b731c5c598dffb0d39eb38 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Sun, 7 Mar 2021 09:09:19 -0800 Subject: [PATCH 1/4] remove options, stick with builder --- docs/trace/exception-handling/Program.cs | 6 +- docs/trace/exception-handling/README.md | 12 ++- .../.publicApi/net452/PublicAPI.Unshipped.txt | 12 +-- .../.publicApi/net46/PublicAPI.Unshipped.txt | 12 +-- .../.publicApi/net461/PublicAPI.Unshipped.txt | 30 +++---- .../netstandard2.0/PublicAPI.Unshipped.txt | 30 +++---- src/OpenTelemetry/Sdk.cs | 14 +-- .../Trace/TracerProviderBuilderExtensions.cs | 17 ++++ .../Trace/TracerProviderBuilderSdk.cs | 58 +++++++++---- .../Trace/TracerProviderOptions.cs | 27 ------ .../TracerProviderBuilderExtensionsTest.cs | 85 +++++++++++++++++++ .../Trace/TracerProviderOptionsTest.cs | 81 ------------------ 12 files changed, 186 insertions(+), 198 deletions(-) delete mode 100644 src/OpenTelemetry/Trace/TracerProviderOptions.cs delete mode 100644 test/OpenTelemetry.Tests/Trace/TracerProviderOptionsTest.cs diff --git a/docs/trace/exception-handling/Program.cs b/docs/trace/exception-handling/Program.cs index 1148dee2e1e..38f8da56a2a 100644 --- a/docs/trace/exception-handling/Program.cs +++ b/docs/trace/exception-handling/Program.cs @@ -26,12 +26,10 @@ public class Program public static void Main() { - using var tracerProvider = Sdk.CreateTracerProviderBuilder(options => - { - options.SetErrorStatusOnException = true; - }) + using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("MyCompany.MyProduct.MyLibrary") .SetSampler(new AlwaysOnSampler()) + .SetErrorStatusOnException() .AddConsoleExporter() .Build(); diff --git a/docs/trace/exception-handling/README.md b/docs/trace/exception-handling/README.md index 15fd107a5c3..e4ea4ec844f 100644 --- a/docs/trace/exception-handling/README.md +++ b/docs/trace/exception-handling/README.md @@ -34,9 +34,9 @@ The following configuration will automatically detect exception and set the activity status to `Error`: ```csharp -Sdk.CreateTracerProviderBuilder(options => { - options.SetErrorStatusOnException = true; -}); +Sdk.CreateTracerProviderBuilder() + .SetErrorStatusOnException() + // ... ``` A complete example can be found [here](./Program.cs). @@ -79,12 +79,10 @@ public class Program { AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; - using var tracerProvider = Sdk.CreateTracerProviderBuilder(options => - { - options.SetErrorStatusOnException = true; - }) + using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("MyCompany.MyProduct.MyLibrary") .SetSampler(new AlwaysOnSampler()) + .SetErrorStatusOnException() .AddConsoleExporter() .Build(); diff --git a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt index 3b7ac888ecd..ca157e9e28a 100644 --- a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt @@ -1,8 +1,4 @@ -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -OpenTelemetry.Trace.TracerProviderOptions -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void -OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void -static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt index 3b7ac888ecd..ca157e9e28a 100644 --- a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt @@ -1,8 +1,4 @@ -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -OpenTelemetry.Trace.TracerProviderOptions -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void -OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void -static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index ed6451db324..0df979de5e0 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -1,17 +1,13 @@ -OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void -OpenTelemetry.Logs.LogRecord.Message.get -> string -OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -OpenTelemetry.Trace.TracerProviderOptions -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void -OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void -static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void +OpenTelemetry.Logs.LogRecord.Message.get -> string +OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index ed6451db324..0df979de5e0 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,17 +1,13 @@ -OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void -OpenTelemetry.Logs.LogRecord.Message.get -> string -OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -OpenTelemetry.Trace.TracerProviderOptions -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool -OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void -OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void -static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void +OpenTelemetry.Logs.LogRecord.Message.get -> string +OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/Sdk.cs b/src/OpenTelemetry/Sdk.cs index ea1899e21fc..d2eb8e65a69 100644 --- a/src/OpenTelemetry/Sdk.cs +++ b/src/OpenTelemetry/Sdk.cs @@ -60,19 +60,7 @@ public static void SetDefaultTextMapPropagator(TextMapPropagator textMapPropagat /// TracerProviderBuilder instance, which should be used to build TracerProvider. public static TracerProviderBuilder CreateTracerProviderBuilder() { - return CreateTracerProviderBuilder(null); - } - - /// - /// Creates TracerProviderBuilder which should be used to build TracerProvider. - /// - /// TracerProvider configuration options. - /// TracerProviderBuilder instance, which should be used to build TracerProvider. - public static TracerProviderBuilder CreateTracerProviderBuilder(Action configure = null) - { - var options = new TracerProviderOptions(); - configure?.Invoke(options); - return new TracerProviderBuilderSdk(options); + return new TracerProviderBuilderSdk(); } } } diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs index 2ef899ffec4..02616f55c3a 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs @@ -22,6 +22,23 @@ namespace OpenTelemetry.Trace { public static class TracerProviderBuilderExtensions { + /// + /// Sets whether the status of + /// should be set to Status.Error when it ended abnormally due to an unhandled exception. + /// + /// TracerProviderBuilder instance. + /// Enabled or not. Default value is true. + /// Returns for chaining. + public static TracerProviderBuilder SetErrorStatusOnException(this TracerProviderBuilder tracerProviderBuilder, bool enabled = true) + { + if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk) + { + tracerProviderBuilderSdk.SetErrorStatusOnException(enabled); + } + + return tracerProviderBuilder; + } + /// /// Sets sampler. /// diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs index 6f058534241..ce5330a9de9 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs @@ -27,29 +27,14 @@ namespace OpenTelemetry.Trace internal class TracerProviderBuilderSdk : TracerProviderBuilder { private readonly List instrumentationFactories = new List(); - - private readonly TracerProviderOptions options; private readonly List> processors = new List>(); private readonly List sources = new List(); private readonly Dictionary legacyActivityOperationNames = new Dictionary(StringComparer.OrdinalIgnoreCase); private ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault(); private Sampler sampler = new ParentBasedSampler(new AlwaysOnSampler()); - internal TracerProviderBuilderSdk(TracerProviderOptions options) + internal TracerProviderBuilderSdk() { - this.options = options ?? new TracerProviderOptions(); - - if (options.SetErrorStatusOnException) - { - try - { - this.AddProcessor(new ExceptionProcessor()); - } - catch (Exception ex) - { - throw new NotSupportedException($"{nameof(options.SetErrorStatusOnException)} is not supported on this platform.", ex); - } - } } /// @@ -103,6 +88,47 @@ public override TracerProviderBuilder AddSource(params string[] names) return this; } + /// + /// Sets whether the status of + /// should be set to Status.Error when it ended abnormally due to an unhandled exception. + /// + /// Enabled or not. + /// Returns for chaining. + internal TracerProviderBuilder SetErrorStatusOnException(bool enabled) + { + ExceptionProcessor existingExceptionProcessor = null; + + if (this.processors.Count > 0) + { + existingExceptionProcessor = this.processors[0] as ExceptionProcessor; + } + + if (enabled) + { + if (existingExceptionProcessor == null) + { + try + { + this.processors.Insert(0, new ExceptionProcessor()); + } + catch (Exception ex) + { + throw new NotSupportedException("SetErrorStatusOnException is not supported on this platform.", ex); + } + } + } + else + { + if (existingExceptionProcessor != null) + { + this.processors.RemoveAt(0); + existingExceptionProcessor.Dispose(); + } + } + + return this; + } + /// /// Sets sampler. /// diff --git a/src/OpenTelemetry/Trace/TracerProviderOptions.cs b/src/OpenTelemetry/Trace/TracerProviderOptions.cs deleted file mode 100644 index 7ec4b852b6c..00000000000 --- a/src/OpenTelemetry/Trace/TracerProviderOptions.cs +++ /dev/null @@ -1,27 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -namespace OpenTelemetry.Trace -{ - public class TracerProviderOptions - { - /// - /// Gets or sets a value indicating whether the status of - /// should be set to Status.Error when it ended abnormally due to an unhandled exception. - /// - public bool SetErrorStatusOnException { get; set; } = false; - } -} diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs index dec6e1e7ce7..8dec54e8170 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs @@ -23,6 +23,8 @@ namespace OpenTelemetry.Trace.Tests { public class TracerProviderBuilderExtensionsTest { + private const string ActivitySourceName = "TracerProviderBuilderExtensionsTest"; + [Fact] public void AddLegacyOperationName_NullBuilder_Noop() { @@ -57,5 +59,88 @@ public void AddLegacyOperationNameAddsActivityListenerForEmptyActivitySource() using var provider = builder.Build(); Assert.True(emptyActivitySource.HasListeners()); } + + [Fact] + public void SetErrorStatusOnExceptionEnabled() + { + using var activitySource = new ActivitySource(ActivitySourceName); + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddSource(ActivitySourceName) + .SetSampler(new AlwaysOnSampler()) + .SetErrorStatusOnException(false) + .SetErrorStatusOnException(false) + .SetErrorStatusOnException(true) + .SetErrorStatusOnException(true) + .SetErrorStatusOnException(false) + .SetErrorStatusOnException() + .Build(); + + Activity activity = null; + + try + { + using (activity = activitySource.StartActivity("Activity")) + { + throw new Exception("Oops!"); + } + } + catch (Exception) + { + } + + Assert.Equal(StatusCode.Error, activity.GetStatus().StatusCode); + } + + [Fact] + public void SetErrorStatusOnExceptionDisabled() + { + using var activitySource = new ActivitySource(ActivitySourceName); + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddSource(ActivitySourceName) + .SetSampler(new AlwaysOnSampler()) + .SetErrorStatusOnException() + .SetErrorStatusOnException(false) + .Build(); + + Activity activity = null; + + try + { + using (activity = activitySource.StartActivity("Activity")) + { + throw new Exception("Oops!"); + } + } + catch (Exception) + { + } + + Assert.Equal(StatusCode.Unset, activity.GetStatus().StatusCode); + } + + [Fact] + public void SetErrorStatusOnExceptionDefault() + { + using var activitySource = new ActivitySource(ActivitySourceName); + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddSource(ActivitySourceName) + .SetSampler(new AlwaysOnSampler()) + .Build(); + + Activity activity = null; + + try + { + using (activity = activitySource.StartActivity("Activity")) + { + throw new Exception("Oops!"); + } + } + catch (Exception) + { + } + + Assert.Equal(StatusCode.Unset, activity.GetStatus().StatusCode); + } } } diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderOptionsTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderOptionsTest.cs deleted file mode 100644 index 7e4b456d9fb..00000000000 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderOptionsTest.cs +++ /dev/null @@ -1,81 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System; -using System.Diagnostics; -using OpenTelemetry.Tests; -using Xunit; - -namespace OpenTelemetry.Trace.Tests -{ - public class TracerProviderOptionsTest - { - private const string ActivitySourceName = "TracerProviderOptionsTest"; - - [Fact] - public void SetErrorStatusOnExceptionEnabled() - { - using var activitySource = new ActivitySource(ActivitySourceName); - using var tracerProvider = Sdk.CreateTracerProviderBuilder(options => - { - options.SetErrorStatusOnException = true; - }) - .AddSource(ActivitySourceName) - .SetSampler(new AlwaysOnSampler()) - .Build(); - - var activity = activitySource.StartActivity("Activity"); - - try - { - using (activity) - { - throw new Exception("Oops!"); - } - } - catch (Exception) - { - } - - Assert.Equal(StatusCode.Error, activity.GetStatus().StatusCode); - } - - [Fact] - public void SetErrorStatusOnExceptionDefault() - { - using var activitySource = new ActivitySource(ActivitySourceName); - using var tracerProvider = Sdk.CreateTracerProviderBuilder(options => { }) - .AddSource(ActivitySourceName) - .SetSampler(new AlwaysOnSampler()) - .Build(); - - var activity = activitySource.StartActivity("Activity"); - - try - { - using (activity) - { - throw new Exception("Oops!"); - } - } - catch (Exception) - { - } - - Assert.Equal(StatusCode.Unset, activity.GetStatus().StatusCode); - } - } -} From fd389c83213253a7c34cb43dfef1c9ae9f7349dc Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Sun, 7 Mar 2021 09:18:19 -0800 Subject: [PATCH 2/4] update changelog --- src/OpenTelemetry/CHANGELOG.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 6751fad1a5a..3873fefdcb5 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -10,7 +10,8 @@ please check the latest changes ## Unreleased * Added `TracerProviderOptions` and `SetErrorStatusOnException`. - ([#1858](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1858)) + ([#1858](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1858) + [#1875](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1875)) * Added `ForceFlush` to `TracerProvider`. ([#1837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837)) * Added a TracerProvierBuilder extension method called @@ -46,9 +47,9 @@ Released 2021-Feb-09 Released 2021-Feb-04 * Default `Resource` will now contain service.name instead of Telemetry SDK. - [#1744](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1744) + ([#1744](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1744)) * Added GetDefaultResource() method to `Provider`. - [#1768](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1768) + ([#1768](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1768)) ## 1.0.0-rc2 @@ -149,14 +150,14 @@ Released 2020-Oct-16 * Changed `ActivityExporter.OnShutdown`, `ActivityExporter.Shutdown`, `ActivityProcessor.OnShutdown` and `ActivityProcessor.Shutdown` to return boolean value - ([#1282](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1282)) - ([#1285](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1285)) + ([#1282](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1282) + [#1285](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1285)) * Renamed `SamplingDecision` options (`NotRecord` to `Drop`, `Record` to `RecordOnly`, and `RecordAndSampled` to `RecordAndSample`) ([#1297](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1297)) * Added `ILogger`/`Microsoft.Extensions.Logging` integration - ([#1308](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1308)) - ([#1315](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1315)) + ([#1308](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1308) + [#1315](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1315)) * Changed exporter and processor to generic types ([#1328](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1328)): * `ActivityExporter` changed to `BaseExporter` From 4b6a007cf82190434b9d39d13c6e2cfe6467916f Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Sun, 7 Mar 2021 09:21:28 -0800 Subject: [PATCH 3/4] sanity --- .../.publicApi/net452/PublicAPI.Unshipped.txt | 8 +++--- .../.publicApi/net46/PublicAPI.Unshipped.txt | 8 +++--- .../.publicApi/net461/PublicAPI.Unshipped.txt | 26 +++++++++---------- .../netstandard2.0/PublicAPI.Unshipped.txt | 26 +++++++++---------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt index ca157e9e28a..33b41421040 100644 --- a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt @@ -1,4 +1,4 @@ -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt index ca157e9e28a..33b41421040 100644 --- a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt @@ -1,4 +1,4 @@ -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index 0df979de5e0..727171e63d6 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -1,13 +1,13 @@ -OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void -OpenTelemetry.Logs.LogRecord.Message.get -> string -OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void +OpenTelemetry.Logs.LogRecord.Message.get -> string +OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 0df979de5e0..727171e63d6 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,13 +1,13 @@ -OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void -OpenTelemetry.Logs.LogRecord.Message.get -> string -OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool -OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void -OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool +OpenTelemetry.Logs.LogRecord.ForEachScope(System.Action callback, TState state) -> void +OpenTelemetry.Logs.LogRecord.Message.get -> string +OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList> +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void +OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool From d06416010f36c759b58447776c38c68b4e5dd874 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Mon, 8 Mar 2021 08:55:28 -0800 Subject: [PATCH 4/4] update changelog --- src/OpenTelemetry/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 3873fefdcb5..9e25ccaa152 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -9,7 +9,8 @@ please check the latest changes ## Unreleased -* Added `TracerProviderOptions` and `SetErrorStatusOnException`. +* Added `TracerProviderBuilder.SetErrorStatusOnException` which automatically + sets the activity status to `Error` when exception happened. ([#1858](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1858) [#1875](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1875)) * Added `ForceFlush` to `TracerProvider`.