From 44bc244ae6a7e9eb3a7851385b24847699260ecd Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 11 Nov 2021 11:24:01 -0800 Subject: [PATCH 01/12] draft --- .../ConsoleActivityExporter.cs | 10 +++ .../Trace/ActivityStatusProcessor.cs | 72 +++++++++++++++++++ .../Trace/TracerProviderBuilderBase.cs | 17 +++++ .../Trace/TracerProviderBuilderExtensions.cs | 15 ++++ 4 files changed, 114 insertions(+) create mode 100644 src/OpenTelemetry/Trace/ActivityStatusProcessor.cs diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index e52497ba6a5..a47ba7c0c00 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -83,6 +83,16 @@ public override ExportResult Export(in Batch batch) } } + if (activity.Status != ActivityStatusCode.Unset) + { + this.WriteLine("Activity Status:"); + this.WriteLine($"Status: {activity.Status}"); + if (activity.StatusDescription != null) + { + this.WriteLine($"Status Description: {activity.StatusDescription}"); + } + } + this.WriteLine(string.Empty); } diff --git a/src/OpenTelemetry/Trace/ActivityStatusProcessor.cs b/src/OpenTelemetry/Trace/ActivityStatusProcessor.cs new file mode 100644 index 00000000000..25def879ad8 --- /dev/null +++ b/src/OpenTelemetry/Trace/ActivityStatusProcessor.cs @@ -0,0 +1,72 @@ +// +// 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; + +namespace OpenTelemetry.Trace +{ + internal sealed class ActivityStatusProcessor : BaseProcessor + { + public const string UnsetStatusCodeTagValue = "UNSET"; + public const string OkStatusCodeTagValue = "OK"; + public const string ErrorStatusCodeTagValue = "ERROR"; + + private ActivityStatusCode statusCode = ActivityStatusCode.Unset; + private string activityStatusDescription; + + /// + public override void OnEnd(Activity activity) + { + if (this.statusCode == ActivityStatusCode.Unset) + { + foreach (var tag in activity.TagObjects) + { + if (tag.Key == SpanAttributeConstants.StatusCodeKey) + { + this.statusCode = this.GetActivityStatusCode((string)tag.Value); + } + + if (tag.Key == SpanAttributeConstants.StatusDescriptionKey) + { + this.activityStatusDescription = (string)tag.Value; + } + } + + if (this.statusCode != ActivityStatusCode.Unset) + { + activity.SetStatus(this.statusCode, this.activityStatusDescription); + } + } + } + + private ActivityStatusCode GetActivityStatusCode(string statusCodeTagValue) + { + return statusCodeTagValue switch + { + /* + * Note: Order here does matter for perf. Unset is + * first because assumption is most spans will be + * Unset, then Error. Ok is not set by the SDK. + */ + string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Unset, + string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Error, + string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Ok, + _ => ActivityStatusCode.Unset, + }; + } + } +} diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs index ef88b44f353..f91db491b47 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs @@ -33,6 +33,7 @@ public abstract class TracerProviderBuilderBase : TracerProviderBuilder private readonly Dictionary legacyActivityOperationNames = new Dictionary(StringComparer.OrdinalIgnoreCase); private ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault(); private Sampler sampler = new ParentBasedSampler(new AlwaysOnSampler()); + private bool activityStatusSwitch = true; protected TracerProviderBuilderBase() { @@ -163,6 +164,17 @@ internal TracerProviderBuilder AddProcessor(BaseProcessor processor) return this; } + /// + /// Disables status switch for activity. + /// + /// Returns for chaining. + internal TracerProviderBuilder DisableActivityStatusSwitch() + { + this.activityStatusSwitch = false; + + return this; + } + /// /// Adds instrumentation to the provider. /// @@ -187,6 +199,11 @@ protected TracerProviderBuilder AddInstrumentation( /// . protected TracerProvider Build() { + if (this.activityStatusSwitch) + { + this.processors.Add(new ActivityStatusProcessor()); + } + return new TracerProviderSdk( this.resourceBuilder.Build(), this.sources, diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs index 2c1bef0ef5e..5773ee3d3cf 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs @@ -91,6 +91,21 @@ public static TracerProviderBuilder AddProcessor(this TracerProviderBuilder trac return tracerProviderBuilder; } + /// + /// Disable status switch for Activity. + /// + /// TracerProviderBuilder instance. + /// Returns for chaining. + public static TracerProviderBuilder DisableActivityStatusSwitch(this TracerProviderBuilder tracerProviderBuilder) + { + if (tracerProviderBuilder is TracerProviderBuilderBase tracerProviderBuilderBase) + { + tracerProviderBuilderBase.DisableActivityStatusSwitch(); + } + + return tracerProviderBuilder; + } + /// /// Run the given actions to initialize the . /// From 2fac5d0ee4b272fc45424ebf6c336540ff43c429 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Fri, 12 Nov 2021 11:42:08 -0800 Subject: [PATCH 02/12] update status in batch and simple export processor --- .../BackwardCompatibilitySwitches.cs | 26 +++++++ .../BackwardCompatibilityUtils.cs | 68 +++++++++++++++++++ .../BatchActivityExportProcessor.cs | 5 ++ .../SimpleActivityExportProcessor.cs | 5 ++ .../Trace/TracerProviderBuilderBase.cs | 11 ++- 5 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 src/OpenTelemetry/BackwardCompatibilitySwitches.cs create mode 100644 src/OpenTelemetry/BackwardCompatibilityUtils.cs diff --git a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs new file mode 100644 index 00000000000..586345e6c80 --- /dev/null +++ b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs @@ -0,0 +1,26 @@ +// +// 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 +{ + internal static class BackwardCompatibilitySwitches + { + /// + /// Gets or sets a value indicating whether activity status switch is enabled or not. + /// + internal static bool ActivityStatusSwitch { get; set; } = true; + } +} diff --git a/src/OpenTelemetry/BackwardCompatibilityUtils.cs b/src/OpenTelemetry/BackwardCompatibilityUtils.cs new file mode 100644 index 00000000000..d0a9095abe5 --- /dev/null +++ b/src/OpenTelemetry/BackwardCompatibilityUtils.cs @@ -0,0 +1,68 @@ +// +// 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.Trace; + +namespace OpenTelemetry +{ + internal static class BackwardCompatibilityUtils + { + internal const string UnsetStatusCodeTagValue = "UNSET"; + internal const string OkStatusCodeTagValue = "OK"; + internal const string ErrorStatusCodeTagValue = "ERROR"; + + internal static void SetActivityStatusUsingTags(Activity activity) + { + ActivityStatusCode statusCode = ActivityStatusCode.Unset; + string statusDescription = null; + foreach (var tag in activity.TagObjects) + { + if (tag.Key == SpanAttributeConstants.StatusCodeKey) + { + statusCode = GetActivityStatusCode((string)tag.Value); + } + + if (tag.Key == SpanAttributeConstants.StatusDescriptionKey) + { + statusDescription = (string)tag.Value; + } + } + + if (statusCode != ActivityStatusCode.Unset) + { + activity.SetStatus(statusCode, statusDescription); + } + } + + private static ActivityStatusCode GetActivityStatusCode(string statusCodeTagValue) + { + return statusCodeTagValue switch + { + /* + * Note: Order here does matter for perf. Unset is + * first because assumption is most spans will be + * Unset, then Error. Ok is not set by the SDK. + */ + string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Unset, + string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Error, + string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Ok, + _ => ActivityStatusCode.Unset, + }; + } + } +} diff --git a/src/OpenTelemetry/BatchActivityExportProcessor.cs b/src/OpenTelemetry/BatchActivityExportProcessor.cs index be62c8d015c..c541d968d78 100644 --- a/src/OpenTelemetry/BatchActivityExportProcessor.cs +++ b/src/OpenTelemetry/BatchActivityExportProcessor.cs @@ -43,6 +43,11 @@ public override void OnEnd(Activity data) return; } + if (BackwardCompatibilitySwitches.ActivityStatusSwitch && data.Status == ActivityStatusCode.Unset) + { + BackwardCompatibilityUtils.SetActivityStatusUsingTags(data); + } + this.OnExport(data); } } diff --git a/src/OpenTelemetry/SimpleActivityExportProcessor.cs b/src/OpenTelemetry/SimpleActivityExportProcessor.cs index ac9a4a20c7f..23894266740 100644 --- a/src/OpenTelemetry/SimpleActivityExportProcessor.cs +++ b/src/OpenTelemetry/SimpleActivityExportProcessor.cs @@ -33,6 +33,11 @@ public override void OnEnd(Activity data) return; } + if (BackwardCompatibilitySwitches.ActivityStatusSwitch && data.Status == ActivityStatusCode.Unset) + { + BackwardCompatibilityUtils.SetActivityStatusUsingTags(data); + } + this.OnExport(data); } } diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs index f91db491b47..58295f8ced2 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs @@ -33,7 +33,6 @@ public abstract class TracerProviderBuilderBase : TracerProviderBuilder private readonly Dictionary legacyActivityOperationNames = new Dictionary(StringComparer.OrdinalIgnoreCase); private ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault(); private Sampler sampler = new ParentBasedSampler(new AlwaysOnSampler()); - private bool activityStatusSwitch = true; protected TracerProviderBuilderBase() { @@ -170,7 +169,10 @@ internal TracerProviderBuilder AddProcessor(BaseProcessor processor) /// Returns for chaining. internal TracerProviderBuilder DisableActivityStatusSwitch() { - this.activityStatusSwitch = false; + if (BackwardCompatibilitySwitches.ActivityStatusSwitch) + { + BackwardCompatibilitySwitches.ActivityStatusSwitch = false; + } return this; } @@ -199,11 +201,6 @@ protected TracerProviderBuilder AddInstrumentation( /// . protected TracerProvider Build() { - if (this.activityStatusSwitch) - { - this.processors.Add(new ActivityStatusProcessor()); - } - return new TracerProviderSdk( this.resourceBuilder.Build(), this.sources, From 772ee16b9651f0a0e6439524890e8beea9c0e16c Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Fri, 12 Nov 2021 11:46:27 -0800 Subject: [PATCH 03/12] remove processor --- .../Trace/ActivityStatusProcessor.cs | 72 ------------------- 1 file changed, 72 deletions(-) delete mode 100644 src/OpenTelemetry/Trace/ActivityStatusProcessor.cs diff --git a/src/OpenTelemetry/Trace/ActivityStatusProcessor.cs b/src/OpenTelemetry/Trace/ActivityStatusProcessor.cs deleted file mode 100644 index 25def879ad8..00000000000 --- a/src/OpenTelemetry/Trace/ActivityStatusProcessor.cs +++ /dev/null @@ -1,72 +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; - -namespace OpenTelemetry.Trace -{ - internal sealed class ActivityStatusProcessor : BaseProcessor - { - public const string UnsetStatusCodeTagValue = "UNSET"; - public const string OkStatusCodeTagValue = "OK"; - public const string ErrorStatusCodeTagValue = "ERROR"; - - private ActivityStatusCode statusCode = ActivityStatusCode.Unset; - private string activityStatusDescription; - - /// - public override void OnEnd(Activity activity) - { - if (this.statusCode == ActivityStatusCode.Unset) - { - foreach (var tag in activity.TagObjects) - { - if (tag.Key == SpanAttributeConstants.StatusCodeKey) - { - this.statusCode = this.GetActivityStatusCode((string)tag.Value); - } - - if (tag.Key == SpanAttributeConstants.StatusDescriptionKey) - { - this.activityStatusDescription = (string)tag.Value; - } - } - - if (this.statusCode != ActivityStatusCode.Unset) - { - activity.SetStatus(this.statusCode, this.activityStatusDescription); - } - } - } - - private ActivityStatusCode GetActivityStatusCode(string statusCodeTagValue) - { - return statusCodeTagValue switch - { - /* - * Note: Order here does matter for perf. Unset is - * first because assumption is most spans will be - * Unset, then Error. Ok is not set by the SDK. - */ - string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Unset, - string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Error, - string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Ok, - _ => ActivityStatusCode.Unset, - }; - } - } -} From 4c35ae622ff19f335b63e213a31df354fb556247 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Mon, 15 Nov 2021 11:50:41 -0800 Subject: [PATCH 04/12] resolve PR comments --- .../Internal/StatusHelper.cs | 18 +++++ .../BackwardCompatibilitySwitches.cs | 8 ++- .../BackwardCompatibilityUtils.cs | 67 ++++++++++--------- .../Trace/TracerProviderBuilderBase.cs | 14 ---- .../Trace/TracerProviderBuilderExtensions.cs | 15 ----- 5 files changed, 58 insertions(+), 64 deletions(-) diff --git a/src/OpenTelemetry.Api/Internal/StatusHelper.cs b/src/OpenTelemetry.Api/Internal/StatusHelper.cs index 4ca8fa2b2a6..371e93f5007 100644 --- a/src/OpenTelemetry.Api/Internal/StatusHelper.cs +++ b/src/OpenTelemetry.Api/Internal/StatusHelper.cs @@ -15,6 +15,7 @@ // using System; +using System.Diagnostics; using System.Runtime.CompilerServices; using OpenTelemetry.Trace; @@ -59,5 +60,22 @@ string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.O _ => (StatusCode?)null, }; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ActivityStatusCode GetActivityStatusCodeForTagValue(string statusCodeTagValue) + { + return statusCodeTagValue switch + { + /* + * Note: Order here does matter for perf. Unset is + * first because assumption is most spans will be + * Unset, then Error. Ok is not set by the SDK. + */ + string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Unset, + string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Error, + string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Ok, + _ => ActivityStatusCode.Unset, + }; + } } } diff --git a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs index 586345e6c80..4e978d6e49c 100644 --- a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs +++ b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs @@ -16,11 +16,13 @@ namespace OpenTelemetry { - internal static class BackwardCompatibilitySwitches + public static class BackwardCompatibilitySwitches { /// - /// Gets or sets a value indicating whether activity status switch is enabled or not. + /// Gets or sets a value indicating whether activity status switch is enabled + /// If true then activity Status and StatusDescription properties will be set + /// using tags otel.status_code and otel.status_description respecitvely. /// - internal static bool ActivityStatusSwitch { get; set; } = true; + public static bool ActivityStatusSwitch { get; set; } = true; } } diff --git a/src/OpenTelemetry/BackwardCompatibilityUtils.cs b/src/OpenTelemetry/BackwardCompatibilityUtils.cs index d0a9095abe5..1d15ca70161 100644 --- a/src/OpenTelemetry/BackwardCompatibilityUtils.cs +++ b/src/OpenTelemetry/BackwardCompatibilityUtils.cs @@ -14,55 +14,58 @@ // limitations under the License. // -using System; +using System.Collections.Generic; using System.Diagnostics; +using OpenTelemetry.Internal; using OpenTelemetry.Trace; namespace OpenTelemetry { internal static class BackwardCompatibilityUtils { - internal const string UnsetStatusCodeTagValue = "UNSET"; - internal const string OkStatusCodeTagValue = "OK"; - internal const string ErrorStatusCodeTagValue = "ERROR"; - internal static void SetActivityStatusUsingTags(Activity activity) { - ActivityStatusCode statusCode = ActivityStatusCode.Unset; - string statusDescription = null; - foreach (var tag in activity.TagObjects) - { - if (tag.Key == SpanAttributeConstants.StatusCodeKey) - { - statusCode = GetActivityStatusCode((string)tag.Value); - } + var tagState = default(TagEnumerationState); - if (tag.Key == SpanAttributeConstants.StatusDescriptionKey) - { - statusDescription = (string)tag.Value; - } - } + activity.EnumerateTags(ref tagState); - if (statusCode != ActivityStatusCode.Unset) + if (tagState.StatusCode != ActivityStatusCode.Unset) { - activity.SetStatus(statusCode, statusDescription); + activity.SetStatus(tagState.StatusCode, tagState.StatusDescription); } } - private static ActivityStatusCode GetActivityStatusCode(string statusCodeTagValue) + internal struct TagEnumerationState : IActivityEnumerator> { - return statusCodeTagValue switch + public ActivityStatusCode StatusCode { get; set; } + + public string StatusDescription { get; set; } + + public bool ForEach(KeyValuePair activityTag) { - /* - * Note: Order here does matter for perf. Unset is - * first because assumption is most spans will be - * Unset, then Error. Ok is not set by the SDK. - */ - string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Unset, - string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Error, - string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => ActivityStatusCode.Ok, - _ => ActivityStatusCode.Unset, - }; + if (activityTag.Value == null) + { + return true; + } + + string key = activityTag.Key; + + if (activityTag.Value is string strVal) + { + if (key == SpanAttributeConstants.StatusCodeKey) + { + this.StatusCode = StatusHelper.GetActivityStatusCodeForTagValue(strVal); + return true; + } + else if (key == SpanAttributeConstants.StatusDescriptionKey) + { + this.StatusDescription = strVal; + return true; + } + } + + return true; + } } } } diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs index 58295f8ced2..ef88b44f353 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs @@ -163,20 +163,6 @@ internal TracerProviderBuilder AddProcessor(BaseProcessor processor) return this; } - /// - /// Disables status switch for activity. - /// - /// Returns for chaining. - internal TracerProviderBuilder DisableActivityStatusSwitch() - { - if (BackwardCompatibilitySwitches.ActivityStatusSwitch) - { - BackwardCompatibilitySwitches.ActivityStatusSwitch = false; - } - - return this; - } - /// /// Adds instrumentation to the provider. /// diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs index 5773ee3d3cf..2c1bef0ef5e 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs @@ -91,21 +91,6 @@ public static TracerProviderBuilder AddProcessor(this TracerProviderBuilder trac return tracerProviderBuilder; } - /// - /// Disable status switch for Activity. - /// - /// TracerProviderBuilder instance. - /// Returns for chaining. - public static TracerProviderBuilder DisableActivityStatusSwitch(this TracerProviderBuilder tracerProviderBuilder) - { - if (tracerProviderBuilder is TracerProviderBuilderBase tracerProviderBuilderBase) - { - tracerProviderBuilderBase.DisableActivityStatusSwitch(); - } - - return tracerProviderBuilder; - } - /// /// Run the given actions to initialize the . /// From 36e10a424543ac21cadfbb24c1c8db7631b22e1b Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Mon, 15 Nov 2021 11:55:12 -0800 Subject: [PATCH 05/12] fix spell check --- src/OpenTelemetry/BackwardCompatibilitySwitches.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs index 4e978d6e49c..18c39dee236 100644 --- a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs +++ b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs @@ -21,7 +21,7 @@ public static class BackwardCompatibilitySwitches /// /// Gets or sets a value indicating whether activity status switch is enabled /// If true then activity Status and StatusDescription properties will be set - /// using tags otel.status_code and otel.status_description respecitvely. + /// using tags otel.status_code and otel.status_description respectively. /// public static bool ActivityStatusSwitch { get; set; } = true; } From 5f42a407900ed30e88853cd3307092fe235faf8f Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Mon, 15 Nov 2021 11:59:57 -0800 Subject: [PATCH 06/12] add public api --- src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt | 3 +++ .../.publicApi/netstandard2.0/PublicAPI.Unshipped.txt | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index 00f0cae0998..126790341c7 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -1,4 +1,5 @@ abstract OpenTelemetry.Metrics.MetricReader.ProcessMetrics(in OpenTelemetry.Batch metrics, int timeoutMilliseconds) -> bool +OpenTelemetry.BackwardCompatibilitySwitches OpenTelemetry.BaseExporter.ForceFlush(int timeoutMilliseconds = -1) -> bool OpenTelemetry.Batch.Batch(T[] items, int count) -> void OpenTelemetry.Batch.Count.get -> long @@ -120,6 +121,8 @@ override OpenTelemetry.Metrics.MeterProviderBuilderBase.AddMeter(params string[] override OpenTelemetry.Metrics.PeriodicExportingMetricReader.Dispose(bool disposing) -> void override OpenTelemetry.Metrics.PeriodicExportingMetricReader.OnShutdown(int timeoutMilliseconds) -> bool readonly OpenTelemetry.Metrics.BaseExportingMetricReader.exporter -> OpenTelemetry.BaseExporter +static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.get -> bool +static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.set -> void static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddReader(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, OpenTelemetry.Metrics.MetricReader reader) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, OpenTelemetry.Metrics.MetricStreamConfiguration metricStreamConfiguration) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, string name) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 87c9dc171ea..4b462197865 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,4 +1,5 @@ abstract OpenTelemetry.Metrics.MetricReader.ProcessMetrics(in OpenTelemetry.Batch metrics, int timeoutMilliseconds) -> bool +OpenTelemetry.BackwardCompatibilitySwitches OpenTelemetry.BaseExporter.ForceFlush(int timeoutMilliseconds = -1) -> bool OpenTelemetry.Batch.Batch(T[] items, int count) -> void OpenTelemetry.Batch.Count.get -> long @@ -120,6 +121,8 @@ override OpenTelemetry.Metrics.MeterProviderBuilderBase.AddMeter(params string[] override OpenTelemetry.Metrics.PeriodicExportingMetricReader.Dispose(bool disposing) -> void override OpenTelemetry.Metrics.PeriodicExportingMetricReader.OnShutdown(int timeoutMilliseconds) -> bool readonly OpenTelemetry.Metrics.BaseExportingMetricReader.exporter -> OpenTelemetry.BaseExporter +static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.get -> bool +static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.set -> void static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddReader(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, OpenTelemetry.Metrics.MetricReader reader) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, OpenTelemetry.Metrics.MetricStreamConfiguration metricStreamConfiguration) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, string name) -> OpenTelemetry.Metrics.MeterProviderBuilder From 996197fdbf7a0bbf28561beb5ee73edd2b3b4984 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Mon, 15 Nov 2021 12:14:58 -0800 Subject: [PATCH 07/12] helper class rename --- ...rdCompatibilityUtils.cs => BackwardCompatibilityHelper.cs} | 4 ++-- src/OpenTelemetry/BatchActivityExportProcessor.cs | 2 +- src/OpenTelemetry/SimpleActivityExportProcessor.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/OpenTelemetry/{BackwardCompatibilityUtils.cs => BackwardCompatibilityHelper.cs} (94%) diff --git a/src/OpenTelemetry/BackwardCompatibilityUtils.cs b/src/OpenTelemetry/BackwardCompatibilityHelper.cs similarity index 94% rename from src/OpenTelemetry/BackwardCompatibilityUtils.cs rename to src/OpenTelemetry/BackwardCompatibilityHelper.cs index 1d15ca70161..bc47e414a16 100644 --- a/src/OpenTelemetry/BackwardCompatibilityUtils.cs +++ b/src/OpenTelemetry/BackwardCompatibilityHelper.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,7 @@ namespace OpenTelemetry { - internal static class BackwardCompatibilityUtils + internal static class BackwardCompatibilityHelper { internal static void SetActivityStatusUsingTags(Activity activity) { diff --git a/src/OpenTelemetry/BatchActivityExportProcessor.cs b/src/OpenTelemetry/BatchActivityExportProcessor.cs index c541d968d78..92293d12a96 100644 --- a/src/OpenTelemetry/BatchActivityExportProcessor.cs +++ b/src/OpenTelemetry/BatchActivityExportProcessor.cs @@ -45,7 +45,7 @@ public override void OnEnd(Activity data) if (BackwardCompatibilitySwitches.ActivityStatusSwitch && data.Status == ActivityStatusCode.Unset) { - BackwardCompatibilityUtils.SetActivityStatusUsingTags(data); + BackwardCompatibilityHelper.SetActivityStatusUsingTags(data); } this.OnExport(data); diff --git a/src/OpenTelemetry/SimpleActivityExportProcessor.cs b/src/OpenTelemetry/SimpleActivityExportProcessor.cs index 23894266740..f2f7333dec8 100644 --- a/src/OpenTelemetry/SimpleActivityExportProcessor.cs +++ b/src/OpenTelemetry/SimpleActivityExportProcessor.cs @@ -35,7 +35,7 @@ public override void OnEnd(Activity data) if (BackwardCompatibilitySwitches.ActivityStatusSwitch && data.Status == ActivityStatusCode.Unset) { - BackwardCompatibilityUtils.SetActivityStatusUsingTags(data); + BackwardCompatibilityHelper.SetActivityStatusUsingTags(data); } this.OnExport(data); From 8cd6b025b7e7ca14666329009dc8f49a7615be39 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 16 Nov 2021 16:33:51 -0800 Subject: [PATCH 08/12] move status update to Baseexportprocessor --- .../.publicApi/net461/PublicAPI.Unshipped.txt | 6 +-- .../netstandard2.0/PublicAPI.Unshipped.txt | 4 +- .../BackwardCompatibilityHelper.cs | 20 +++++++- .../BackwardCompatibilitySwitches.cs | 12 +++-- src/OpenTelemetry/BaseExportProcessor.cs | 11 +++++ .../BatchActivityExportProcessor.cs | 7 +-- .../SimpleActivityExportProcessor.cs | 7 +-- .../Trace/BatchExportActivityProcessorTest.cs | 48 +++++++++++++++++++ .../SimpleExportActivityProcessorTest.cs | 47 ++++++++++++++++++ 9 files changed, 140 insertions(+), 22 deletions(-) diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index 126790341c7..83957f9f450 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -121,8 +121,8 @@ override OpenTelemetry.Metrics.MeterProviderBuilderBase.AddMeter(params string[] override OpenTelemetry.Metrics.PeriodicExportingMetricReader.Dispose(bool disposing) -> void override OpenTelemetry.Metrics.PeriodicExportingMetricReader.OnShutdown(int timeoutMilliseconds) -> bool readonly OpenTelemetry.Metrics.BaseExportingMetricReader.exporter -> OpenTelemetry.BaseExporter -static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.get -> bool -static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.set -> void +static OpenTelemetry.BackwardCompatibilitySwitches.StatusTagMigrationEnabled.get -> bool +static OpenTelemetry.BackwardCompatibilitySwitches.StatusTagMigrationEnabled.set -> void static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddReader(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, OpenTelemetry.Metrics.MetricReader reader) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, OpenTelemetry.Metrics.MetricStreamConfiguration metricStreamConfiguration) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, string name) -> OpenTelemetry.Metrics.MeterProviderBuilder @@ -145,4 +145,4 @@ virtual OpenTelemetry.Metrics.MetricReader.Dispose(bool disposing) -> void virtual OpenTelemetry.Metrics.MetricReader.OnCollect(int timeoutMilliseconds) -> bool virtual OpenTelemetry.Metrics.MetricReader.OnShutdown(int timeoutMilliseconds) -> bool virtual OpenTelemetry.Metrics.MetricStreamConfiguration.Aggregation.get -> OpenTelemetry.Metrics.Aggregation -virtual OpenTelemetry.Metrics.MetricStreamConfiguration.Aggregation.set -> void \ No newline at end of file +virtual OpenTelemetry.Metrics.MetricStreamConfiguration.Aggregation.set -> void diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 4b462197865..83957f9f450 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -121,8 +121,8 @@ override OpenTelemetry.Metrics.MeterProviderBuilderBase.AddMeter(params string[] override OpenTelemetry.Metrics.PeriodicExportingMetricReader.Dispose(bool disposing) -> void override OpenTelemetry.Metrics.PeriodicExportingMetricReader.OnShutdown(int timeoutMilliseconds) -> bool readonly OpenTelemetry.Metrics.BaseExportingMetricReader.exporter -> OpenTelemetry.BaseExporter -static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.get -> bool -static OpenTelemetry.BackwardCompatibilitySwitches.ActivityStatusSwitch.set -> void +static OpenTelemetry.BackwardCompatibilitySwitches.StatusTagMigrationEnabled.get -> bool +static OpenTelemetry.BackwardCompatibilitySwitches.StatusTagMigrationEnabled.set -> void static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddReader(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, OpenTelemetry.Metrics.MetricReader reader) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, OpenTelemetry.Metrics.MetricStreamConfiguration metricStreamConfiguration) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddView(this OpenTelemetry.Metrics.MeterProviderBuilder meterProviderBuilder, string instrumentName, string name) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry/BackwardCompatibilityHelper.cs b/src/OpenTelemetry/BackwardCompatibilityHelper.cs index bc47e414a16..0b4f8ffcbd9 100644 --- a/src/OpenTelemetry/BackwardCompatibilityHelper.cs +++ b/src/OpenTelemetry/BackwardCompatibilityHelper.cs @@ -29,7 +29,11 @@ internal static void SetActivityStatusUsingTags(Activity activity) activity.EnumerateTags(ref tagState); - if (tagState.StatusCode != ActivityStatusCode.Unset) + if (tagState.StatusCode == ActivityStatusCode.Ok) + { + activity.SetStatus(tagState.StatusCode); + } + else if (tagState.StatusCode == ActivityStatusCode.Error) { activity.SetStatus(tagState.StatusCode, tagState.StatusDescription); } @@ -55,11 +59,25 @@ public bool ForEach(KeyValuePair activityTag) if (key == SpanAttributeConstants.StatusCodeKey) { this.StatusCode = StatusHelper.GetActivityStatusCodeForTagValue(strVal); + if (this.StatusCode != ActivityStatusCode.Error) + { + // Description is only valid for Error + // No need to look further for description. + return false; + } + return true; } else if (key == SpanAttributeConstants.StatusDescriptionKey) { this.StatusDescription = strVal; + if (this.StatusCode != ActivityStatusCode.Unset) + { + // We now have both Status and StatusDescription + // No need to look further for other tags. + return false; + } + return true; } } diff --git a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs index 18c39dee236..0c81ba76890 100644 --- a/src/OpenTelemetry/BackwardCompatibilitySwitches.cs +++ b/src/OpenTelemetry/BackwardCompatibilitySwitches.cs @@ -14,15 +14,19 @@ // limitations under the License. // +using System.Diagnostics; + namespace OpenTelemetry { public static class BackwardCompatibilitySwitches { /// - /// Gets or sets a value indicating whether activity status switch is enabled - /// If true then activity Status and StatusDescription properties will be set - /// using tags otel.status_code and otel.status_description respectively. + /// Gets or sets a value indicating whether or not activity status migration is enabled. Default value: true. /// - public static bool ActivityStatusSwitch { get; set; } = true; + /// + /// If true then and properties (added in .NET 6) will be set + /// from `otel.status_code` and `otel.status_description` tag values respectively prior to export. + /// + public static bool StatusTagMigrationEnabled { get; set; } = true; } } diff --git a/src/OpenTelemetry/BaseExportProcessor.cs b/src/OpenTelemetry/BaseExportProcessor.cs index df07767da40..551304d65ce 100644 --- a/src/OpenTelemetry/BaseExportProcessor.cs +++ b/src/OpenTelemetry/BaseExportProcessor.cs @@ -15,6 +15,7 @@ // using System; +using System.Diagnostics; using OpenTelemetry.Internal; namespace OpenTelemetry @@ -47,6 +48,16 @@ public sealed override void OnStart(T data) public override void OnEnd(T data) { + var activity = data as Activity; + + if (activity != null) + { + if (BackwardCompatibilitySwitches.StatusTagMigrationEnabled && activity.Status == ActivityStatusCode.Unset) + { + BackwardCompatibilityHelper.SetActivityStatusUsingTags(activity); + } + } + this.OnExport(data); } diff --git a/src/OpenTelemetry/BatchActivityExportProcessor.cs b/src/OpenTelemetry/BatchActivityExportProcessor.cs index 92293d12a96..55ddc4b1f42 100644 --- a/src/OpenTelemetry/BatchActivityExportProcessor.cs +++ b/src/OpenTelemetry/BatchActivityExportProcessor.cs @@ -43,12 +43,7 @@ public override void OnEnd(Activity data) return; } - if (BackwardCompatibilitySwitches.ActivityStatusSwitch && data.Status == ActivityStatusCode.Unset) - { - BackwardCompatibilityHelper.SetActivityStatusUsingTags(data); - } - - this.OnExport(data); + base.OnEnd(data); } } } diff --git a/src/OpenTelemetry/SimpleActivityExportProcessor.cs b/src/OpenTelemetry/SimpleActivityExportProcessor.cs index f2f7333dec8..012cd2b12e8 100644 --- a/src/OpenTelemetry/SimpleActivityExportProcessor.cs +++ b/src/OpenTelemetry/SimpleActivityExportProcessor.cs @@ -33,12 +33,7 @@ public override void OnEnd(Activity data) return; } - if (BackwardCompatibilitySwitches.ActivityStatusSwitch && data.Status == ActivityStatusCode.Unset) - { - BackwardCompatibilityHelper.SetActivityStatusUsingTags(data); - } - - this.OnExport(data); + base.OnEnd(data); } } } diff --git a/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs b/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs index 38cacff2a15..832559817ea 100644 --- a/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs +++ b/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs @@ -19,12 +19,15 @@ using System.Diagnostics; using System.Threading; using OpenTelemetry.Exporter; +using OpenTelemetry.Internal; using Xunit; namespace OpenTelemetry.Trace.Tests { public class BatchExportActivityProcessorTest { + private const string ActivitySourceName = "BatchActivityExportProcessorTest"; + [Fact] public void CheckNullExporter() { @@ -196,5 +199,50 @@ public void CheckExportDrainsBatchOnFailure() Assert.Equal(3, processor.ProcessedCount); // Verify batch was drained even though nothing was exported. } + + [Theory] + [InlineData("OK", null, true)] + [InlineData("ERROR", "Error Description", true)] + [InlineData("OK", null, false)] + [InlineData("ERROR", "Error Description", false)] + public void ActivityStatusIsSetIfStatusMigrationIsEnabled(string statusCode, string statusDescription, bool isStatusMigrationEnabled) + { + var sampler = new AlwaysOnSampler(); + var exportedItems = new List(); + var processor = new BatchActivityExportProcessor(new InMemoryExporter(exportedItems)); + using var activitySource = new ActivitySource(ActivitySourceName); + + // Set status migration - it is enabled by default. + BackwardCompatibilitySwitches.StatusTagMigrationEnabled = isStatusMigrationEnabled; + using var sdk = Sdk.CreateTracerProviderBuilder() + .AddSource(ActivitySourceName) + .SetSampler(sampler) + .AddProcessor(processor) + .Build(); + + using (var activity = activitySource.StartActivity("Activity")) + { + activity?.SetTag("otel.status_code", statusCode); + if (statusDescription != null) + { + activity?.SetTag("otel.status_description", statusDescription); + } + } + + processor.ForceFlush(); + ActivityStatusCode expectedStatusForTagValue = StatusHelper.GetActivityStatusCodeForTagValue(statusCode); + + if (isStatusMigrationEnabled) + { + Assert.Equal(expectedStatusForTagValue, exportedItems[0].Status); + Assert.Equal(statusDescription, exportedItems[0].StatusDescription); + } + else + { + Assert.Equal(ActivityStatusCode.Unset, exportedItems[0].Status); + Assert.NotEqual(expectedStatusForTagValue, exportedItems[0].Status); + Assert.Null(exportedItems[0].StatusDescription); + } + } } } diff --git a/test/OpenTelemetry.Tests/Trace/SimpleExportActivityProcessorTest.cs b/test/OpenTelemetry.Tests/Trace/SimpleExportActivityProcessorTest.cs index d30ff6ed051..0029bc848bb 100644 --- a/test/OpenTelemetry.Tests/Trace/SimpleExportActivityProcessorTest.cs +++ b/test/OpenTelemetry.Tests/Trace/SimpleExportActivityProcessorTest.cs @@ -19,12 +19,15 @@ using System.Diagnostics; using System.Threading; using OpenTelemetry.Exporter; +using OpenTelemetry.Internal; using Xunit; namespace OpenTelemetry.Trace.Tests { public class SimpleExportActivityProcessorTest { + private const string ActivitySourceName = "SimpleActivityExportProcessorTest"; + [Fact] public void CheckNullExporter() { @@ -113,5 +116,49 @@ public void CheckExportForRecordingButNotSampledActivity() processor.OnEnd(activity); Assert.Empty(exportedItems); } + + [Theory] + [InlineData("OK", null, true)] + [InlineData("ERROR", "Error Description", true)] + [InlineData("OK", null, false)] + [InlineData("ERROR", "Error Description", false)] + public void ActivityStatusIsSetIfStatusMigrationIsEnabled(string statusCode, string statusDescription, bool isStatusMigrationEnabled) + { + var sampler = new AlwaysOnSampler(); + var exportedItems = new List(); + var processor = new SimpleActivityExportProcessor(new InMemoryExporter(exportedItems)); + using var activitySource = new ActivitySource(ActivitySourceName); + + // Set status migration - it is true by default. + BackwardCompatibilitySwitches.StatusTagMigrationEnabled = isStatusMigrationEnabled; + using var sdk = Sdk.CreateTracerProviderBuilder() + .AddSource(ActivitySourceName) + .SetSampler(sampler) + .AddProcessor(processor) + .Build(); + + using (var activity = activitySource.StartActivity("Activity")) + { + activity?.SetTag("otel.status_code", statusCode); + if (statusDescription != null) + { + activity?.SetTag("otel.status_description", statusDescription); + } + } + + ActivityStatusCode expectedStatusForTagValue = StatusHelper.GetActivityStatusCodeForTagValue(statusCode); + + if (isStatusMigrationEnabled) + { + Assert.Equal(expectedStatusForTagValue, exportedItems[0].Status); + Assert.Equal(statusDescription, exportedItems[0].StatusDescription); + } + else + { + Assert.Equal(ActivityStatusCode.Unset, exportedItems[0].Status); + Assert.NotEqual(expectedStatusForTagValue, exportedItems[0].Status); + Assert.Null(exportedItems[0].StatusDescription); + } + } } } From 5a8fe2a0d979f4d39ea9fba02622a974b3887017 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 16 Nov 2021 16:41:31 -0800 Subject: [PATCH 09/12] revert console exporter changes --- .../ConsoleActivityExporter.cs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index a47ba7c0c00..33ce096efee 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -83,20 +83,10 @@ public override ExportResult Export(in Batch batch) } } - if (activity.Status != ActivityStatusCode.Unset) - { - this.WriteLine("Activity Status:"); - this.WriteLine($"Status: {activity.Status}"); - if (activity.StatusDescription != null) - { - this.WriteLine($"Status Description: {activity.StatusDescription}"); - } - } - this.WriteLine(string.Empty); } return ExportResult.Success; } } -} +} \ No newline at end of file From 7447b18c26783fd30b2f626da46734a6a87170d2 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 16 Nov 2021 16:44:47 -0800 Subject: [PATCH 10/12] blank before EOF --- src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index 33ce096efee..e52497ba6a5 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -89,4 +89,4 @@ public override ExportResult Export(in Batch batch) return ExportResult.Success; } } -} \ No newline at end of file +} From 1a4f2bf67d00b799c7e0e6ad1df0a5c133c02ffd Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 16 Nov 2021 18:12:32 -0800 Subject: [PATCH 11/12] update changelog --- src/OpenTelemetry/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 5bc237cebce..011e83b7c99 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Set activity `Status` and `StatusDescription` using status tags. + ([2605](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2605)) + * Histogram bounds are validated when added to a View. ([#2573](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2573)) From 562a898befbff702294ea44d5aa02e2e24ec289e Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Fri, 19 Nov 2021 16:11:10 -0800 Subject: [PATCH 12/12] add export status & description to console export --- .../ConsoleActivityExporter.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs index e52497ba6a5..d3379769b42 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs @@ -43,6 +43,17 @@ public override ExportResult Export(in Batch batch) this.WriteLine($"Activity.Kind: {activity.Kind}"); this.WriteLine($"Activity.StartTime: {activity.StartTimeUtc:yyyy-MM-ddTHH:mm:ss.fffffffZ}"); this.WriteLine($"Activity.Duration: {activity.Duration}"); + + if (activity.Status != ActivityStatusCode.Unset) + { + this.WriteLine($"Activity.Status: {activity.Status}"); + } + + if (activity.StatusDescription != null) + { + this.WriteLine($"Activity.StatusDescription: {activity.StatusDescription}"); + } + if (activity.TagObjects.Any()) { this.WriteLine("Activity.TagObjects:");