diff --git a/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md index 71a4bfa708..76782773cf 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Migrate to native Activity `Status` and `StatusDesciption`. + ([#651](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/651)) + ## 1.0.0-rc9.5 (source code moved to contrib repo) Released 2022-Jun-21 diff --git a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs index 716b9c0136..a8c1ddb9a4 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs @@ -128,9 +128,9 @@ private void OnStopActivity(Activity activity, HttpContext context) activity.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode); - if (activity.GetStatus().StatusCode == StatusCode.Unset) + if (activity.Status == ActivityStatusCode.Unset) { - activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, response.StatusCode)); + activity.SetStatus(SpanHelper.ResolveActivityStatusForHttpStatusCode(activity.Kind, response.StatusCode)); } var routeData = context.Request.RequestContext.RouteData; @@ -181,7 +181,7 @@ private void OnException(Activity activity, HttpContext context, Exception excep activity.RecordException(exception); } - activity.SetStatus(Status.Error.WithDescription(exception.Message)); + activity.SetStatus(ActivityStatusCode.Error, exception.Message); try { diff --git a/src/OpenTelemetry.Instrumentation.AspNet/OpenTelemetry.Instrumentation.AspNet.csproj b/src/OpenTelemetry.Instrumentation.AspNet/OpenTelemetry.Instrumentation.AspNet.csproj index 03f775a3d7..060706d0c6 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/OpenTelemetry.Instrumentation.AspNet.csproj +++ b/src/OpenTelemetry.Instrumentation.AspNet/OpenTelemetry.Instrumentation.AspNet.csproj @@ -14,7 +14,6 @@ - diff --git a/src/OpenTelemetry.Instrumentation.AspNet/SpanHelper.cs b/src/OpenTelemetry.Instrumentation.AspNet/SpanHelper.cs new file mode 100644 index 0000000000..14e6bc4e2c --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.AspNet/SpanHelper.cs @@ -0,0 +1,43 @@ +// +// 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.Diagnostics; + +namespace OpenTelemetry.Instrumentation.AspNet; + +/// +/// A collection of helper methods to be used when building spans. +/// +internal static class SpanHelper +{ + /// + /// Helper method that populates Activity Status from http status code according + /// to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status. + /// + /// The span kind. + /// Http status code. + /// Resolved span for the Http status code. + public static ActivityStatusCode ResolveActivityStatusForHttpStatusCode(ActivityKind kind, int httpStatusCode) + { + var upperBound = kind == ActivityKind.Client ? 399 : 499; + if (httpStatusCode >= 100 && httpStatusCode <= upperBound) + { + return ActivityStatusCode.Unset; + } + + return ActivityStatusCode.Error; + } +} diff --git a/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs b/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs index 0e3bd5eef2..70d477c627 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs @@ -137,7 +137,7 @@ public void AspNetRequestsAreCollectedSuccessfully( return httpContext.Request.Path != filter; }; - options.Enrich = GetEnrichmentAction(setStatusToErrorInEnrich ? Status.Error : default); + options.Enrich = GetEnrichmentAction(setStatusToErrorInEnrich ? ActivityStatusCode.Error : default); options.RecordException = recordException; }) @@ -217,27 +217,27 @@ public void AspNetRequestsAreCollectedSuccessfully( if (recordException) { - var status = span.GetStatus(); - Assert.Equal(Status.Error.StatusCode, status.StatusCode); - Assert.Equal("Operation is not valid due to the current state of the object.", status.Description); + var status = span.Status; + Assert.Equal(ActivityStatusCode.Error, span.Status); + Assert.Equal("Operation is not valid due to the current state of the object.", span.StatusDescription); } else if (setStatusToErrorInEnrich) { // This validates that users can override the // status in Enrich. - Assert.Equal(Status.Error, span.GetStatus()); + Assert.Equal(ActivityStatusCode.Error, span.Status); // Instrumentation is not expected to set status description // as the reason can be inferred from SemanticConventions.AttributeHttpStatusCode - Assert.True(string.IsNullOrEmpty(span.GetStatus().Description)); + Assert.True(string.IsNullOrEmpty(span.StatusDescription)); } else { - Assert.Equal(Status.Unset, span.GetStatus()); + Assert.Equal(ActivityStatusCode.Unset, span.Status); // Instrumentation is not expected to set status description // as the reason can be inferred from SemanticConventions.AttributeHttpStatusCode - Assert.True(string.IsNullOrEmpty(span.GetStatus().Description)); + Assert.True(string.IsNullOrEmpty(span.StatusDescription)); } } @@ -324,7 +324,7 @@ public void ExtractContextIrrespectiveOfTheFilterApplied() Assert.True(isPropagatorCalled); } - private static Action GetEnrichmentAction(Status statusToBeSet) + private static Action GetEnrichmentAction(ActivityStatusCode statusToBeSet) { void EnrichAction(Activity activity, string method, object obj) { diff --git a/test/OpenTelemetry.Instrumentation.AspNet.Tests/OpenTelemetry.Instrumentation.AspNet.Tests.csproj b/test/OpenTelemetry.Instrumentation.AspNet.Tests/OpenTelemetry.Instrumentation.AspNet.Tests.csproj index e3499a281e..50803ac337 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.Tests/OpenTelemetry.Instrumentation.AspNet.Tests.csproj +++ b/test/OpenTelemetry.Instrumentation.AspNet.Tests/OpenTelemetry.Instrumentation.AspNet.Tests.csproj @@ -27,7 +27,6 @@ -