From 073ce6cedceb4e9cd1aa946afe7c5e388011c166 Mon Sep 17 00:00:00 2001 From: Austin Tan Date: Mon, 3 May 2021 14:03:58 -0700 Subject: [PATCH] Move AddLegacySource to API (#2019) --- .../.publicApi/net452/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + src/OpenTelemetry.Api/CHANGELOG.md | 3 ++ .../Trace/TracerProviderBuilder.cs | 11 ++++++ .../.publicApi/net452/PublicAPI.Unshipped.txt | 2 +- .../.publicApi/net46/PublicAPI.Unshipped.txt | 2 +- .../.publicApi/net461/PublicAPI.Unshipped.txt | 2 +- .../netstandard2.0/PublicAPI.Unshipped.txt | 2 +- src/OpenTelemetry/CHANGELOG.md | 4 +++ .../Trace/TracerProviderBuilderBase.cs | 33 +++++++---------- .../Trace/TracerProviderBuilderExtensions.cs | 19 ---------- .../TracerProviderBuilderExtensionsTest.cs | 35 ------------------- .../Trace/TracerProviderSdkTest.cs | 22 ++++++++++++ 13 files changed, 59 insertions(+), 78 deletions(-) diff --git a/src/OpenTelemetry.Api/.publicApi/net452/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Api/.publicApi/net452/PublicAPI.Unshipped.txt index 877fcaee8d0..eb5e650a70e 100644 --- a/src/OpenTelemetry.Api/.publicApi/net452/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Api/.publicApi/net452/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ +abstract OpenTelemetry.Trace.TracerProviderBuilder.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void \ No newline at end of file diff --git a/src/OpenTelemetry.Api/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Api/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 877fcaee8d0..eb5e650a70e 100644 --- a/src/OpenTelemetry.Api/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Api/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ +abstract OpenTelemetry.Trace.TracerProviderBuilder.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void \ No newline at end of file diff --git a/src/OpenTelemetry.Api/CHANGELOG.md b/src/OpenTelemetry.Api/CHANGELOG.md index f364587626f..24a9ad9c7bb 100644 --- a/src/OpenTelemetry.Api/CHANGELOG.md +++ b/src/OpenTelemetry.Api/CHANGELOG.md @@ -9,6 +9,9 @@ please check the latest changes ## Unreleased +* Adds `AddLegacySource()` to `TracerProviderBuilder` + ([#2019](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2019)) + ## 1.1.0-beta2 Released 2021-Apr-23 diff --git a/src/OpenTelemetry.Api/Trace/TracerProviderBuilder.cs b/src/OpenTelemetry.Api/Trace/TracerProviderBuilder.cs index dc8e0d57efb..e902a38722e 100644 --- a/src/OpenTelemetry.Api/Trace/TracerProviderBuilder.cs +++ b/src/OpenTelemetry.Api/Trace/TracerProviderBuilder.cs @@ -14,6 +14,7 @@ // limitations under the License. // using System; +using System.Diagnostics; namespace OpenTelemetry.Trace { @@ -45,5 +46,15 @@ public abstract TracerProviderBuilder AddInstrumentation( /// Activity source names. /// Returns for chaining. public abstract TracerProviderBuilder AddSource(params string[] names); + + /// + /// Adds a listener for objects created with the given operation name to the . + /// + /// + /// This is provided to capture legacy objects created without using the API. + /// + /// Operation name of the objects to capture. + /// Returns for chaining. + public abstract TracerProviderBuilder AddLegacySource(string operationName); } } diff --git a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt index 67d8b08882c..434be6c75d6 100644 --- a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt @@ -6,7 +6,7 @@ OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrume OpenTelemetry.Trace.TracerProviderBuilderBase.Build() -> OpenTelemetry.Trace.TracerProvider OpenTelemetry.Trace.TracerProviderBuilderBase.TracerProviderBuilderBase() -> void override OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(System.Func instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder +override OpenTelemetry.Trace.TracerProviderBuilderBase.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder override OpenTelemetry.Trace.TracerProviderBuilderBase.AddSource(params string[] names) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(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 67d8b08882c..434be6c75d6 100644 --- a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt @@ -6,7 +6,7 @@ OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrume OpenTelemetry.Trace.TracerProviderBuilderBase.Build() -> OpenTelemetry.Trace.TracerProvider OpenTelemetry.Trace.TracerProviderBuilderBase.TracerProviderBuilderBase() -> void override OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(System.Func instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder +override OpenTelemetry.Trace.TracerProviderBuilderBase.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder override OpenTelemetry.Trace.TracerProviderBuilderBase.AddSource(params string[] names) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(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 aae8523dcf5..848249793be 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -17,8 +17,8 @@ OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrume OpenTelemetry.Trace.TracerProviderBuilderBase.Build() -> OpenTelemetry.Trace.TracerProvider OpenTelemetry.Trace.TracerProviderBuilderBase.TracerProviderBuilderBase() -> void override OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(System.Func instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder +override OpenTelemetry.Trace.TracerProviderBuilderBase.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder override OpenTelemetry.Trace.TracerProviderBuilderBase.AddSource(params string[] names) -> OpenTelemetry.Trace.TracerProviderBuilder override OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(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 aae8523dcf5..848249793be 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -17,8 +17,8 @@ OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(string instrume OpenTelemetry.Trace.TracerProviderBuilderBase.Build() -> OpenTelemetry.Trace.TracerProvider OpenTelemetry.Trace.TracerProviderBuilderBase.TracerProviderBuilderBase() -> void override OpenTelemetry.Trace.TracerProviderBuilderBase.AddInstrumentation(System.Func instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder +override OpenTelemetry.Trace.TracerProviderBuilderBase.AddLegacySource(string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder override OpenTelemetry.Trace.TracerProviderBuilderBase.AddSource(params string[] names) -> OpenTelemetry.Trace.TracerProviderBuilder override OpenTelemetry.Logs.OpenTelemetryLoggerProvider.Dispose(bool disposing) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(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/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index e24138fd7bd..dd2b7d9ec4a 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -9,6 +9,10 @@ please check the latest changes ## Unreleased +* `AddLegacySource()` moved out of `TracerProviderBuilderExtensions` and into + public API + ([#2019](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2019)) + ## 1.1.0-beta2 Released 2021-Apr-23 diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs index f0e6fc3b50f..0b752dd15cd 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs @@ -79,6 +79,19 @@ public override TracerProviderBuilder AddSource(params string[] names) return this; } + /// + public override TracerProviderBuilder AddLegacySource(string operationName) + { + if (string.IsNullOrWhiteSpace(operationName)) + { + throw new ArgumentException($"{nameof(operationName)} contains null or whitespace string."); + } + + this.legacyActivityOperationNames[operationName] = true; + + return this; + } + /// /// Sets whether the status of /// should be set to Status.Error when it ended abnormally due to an unhandled exception. @@ -160,26 +173,6 @@ internal TracerProviderBuilder AddProcessor(BaseProcessor processor) return this; } - /// - /// Adds a listener for objects created with the given operation name to the . - /// - /// - /// This is provided to capture legacy objects created without using the API. - /// - /// Operation name of the objects to capture. - /// Returns for chaining. - internal TracerProviderBuilder AddLegacySource(string operationName) - { - if (string.IsNullOrWhiteSpace(operationName)) - { - throw new ArgumentException($"{nameof(operationName)} contains null or whitespace string."); - } - - this.legacyActivityOperationNames[operationName] = true; - - return this; - } - /// /// Adds instrumentation to the provider. /// diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs index 9db834cff2e..f6b261e12cf 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs @@ -91,25 +91,6 @@ public static TracerProviderBuilder AddProcessor(this TracerProviderBuilder trac return tracerProviderBuilder; } - /// - /// Adds a listener for objects created with the given operation name to the . - /// - /// - /// This is provided to capture legacy objects created without using the API. - /// - /// instance. - /// Operation name of the objects to capture. - /// Returns for chaining. - public static TracerProviderBuilder AddLegacySource(this TracerProviderBuilder tracerProviderBuilder, string operationName) - { - if (tracerProviderBuilder is TracerProviderBuilderBase tracerProviderBuilderBase) - { - tracerProviderBuilderBase.AddLegacySource(operationName); - } - - return tracerProviderBuilder; - } - /// /// Run the given actions to initialize the . /// diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs index dc528bf20ee..067dbcc2937 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs @@ -25,41 +25,6 @@ public class TracerProviderBuilderExtensionsTest { private const string ActivitySourceName = "TracerProviderBuilderExtensionsTest"; - [Fact] - public void AddLegacyOperationName_NullBuilder_Noop() - { - TracerProviderBuilder builder = null; - - // No exception is thrown on executing this line - builder.AddLegacySource("TestOperationName"); - using var provider = builder.Build(); - - var emptyActivitySource = new ActivitySource(string.Empty); - Assert.False(emptyActivitySource.HasListeners()); // Check if AddLegacyOperationName was noop after TracerProviderBuilder.Build - } - - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData(" ")] - public void AddLegacyOperationName_BadArgs(string operationName) - { - var builder = Sdk.CreateTracerProviderBuilder(); - Assert.Throws(() => builder.AddLegacySource(operationName)); - } - - [Fact] - public void AddLegacyOperationNameAddsActivityListenerForEmptyActivitySource() - { - var emptyActivitySource = new ActivitySource(string.Empty); - var builder = Sdk.CreateTracerProviderBuilder(); - builder.AddLegacySource("TestOperationName"); - - Assert.False(emptyActivitySource.HasListeners()); - using var provider = builder.Build(); - Assert.True(emptyActivitySource.HasListeners()); - } - [Fact] public void SetErrorStatusOnExceptionEnabled() { diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index 89f6f0603cc..a1560738739 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -896,6 +896,28 @@ public void TracerProviderSdkBuildsWithDefaultResource() Assert.Contains("unknown_service", (string)resource.Attributes.FirstOrDefault().Value); } + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void AddLegacyOperationName_BadArgs(string operationName) + { + var builder = Sdk.CreateTracerProviderBuilder(); + Assert.Throws(() => builder.AddLegacySource(operationName)); + } + + [Fact] + public void AddLegacyOperationNameAddsActivityListenerForEmptyActivitySource() + { + var emptyActivitySource = new ActivitySource(string.Empty); + var builder = Sdk.CreateTracerProviderBuilder(); + builder.AddLegacySource("TestOperationName"); + + Assert.False(emptyActivitySource.HasListeners()); + using var provider = builder.Build(); + Assert.True(emptyActivitySource.HasListeners()); + } + [Fact] public void TracerProviderSdkBuildsWithSDKResource() {