Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASP.NET] Migrate to native Activity Status and Status Desc #651

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SpanHelper.cs" Link="Includes\SpanHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\DiagnosticSourceInstrumentation\PropertyFetcher.cs" Link="Includes\PropertyFetcher.cs" />
Expand Down
43 changes: 43 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/SpanHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="SpanHelper.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>

using System.Diagnostics;

namespace OpenTelemetry.Instrumentation.AspNet;

/// <summary>
/// A collection of helper methods to be used when building spans.
/// </summary>
internal static class SpanHelper
{
/// <summary>
/// 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.
/// </summary>
/// <param name="kind">The span kind.</param>
/// <param name="httpStatusCode">Http status code.</param>
/// <returns>Resolved span <see cref="ActivityStatusCode"/> for the Http status code.</returns>
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -324,7 +324,7 @@ public void ExtractContextIrrespectiveOfTheFilterApplied()
Assert.True(isPropagatorCalled);
}

private static Action<Activity, string, object> GetEnrichmentAction(Status statusToBeSet)
private static Action<Activity, string, object> GetEnrichmentAction(ActivityStatusCode statusToBeSet)
{
void EnrichAction(Activity activity, string method, object obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SpanHelper.cs" Link="Includes\SpanHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs"/>
Expand Down