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 4 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
20 changes: 20 additions & 0 deletions src/OpenTelemetry.Contrib.Shared/Api/SpanHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ public static Status ResolveSpanStatusForHttpStatusCode(ActivityKind kind, int h

return Status.Error;
}

#if NET462 || NET6_0_OR_GREATER
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
/// <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="Status"/> 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;
}
#endif
}
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))
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

## 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 @@ -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