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

AspNetCore/Http Instrumentation to leverage native Activity Status. #3555

Merged
merged 9 commits into from
Aug 10, 2022
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
6 changes: 3 additions & 3 deletions src/OpenTelemetry.Api/Internal/SpanHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ internal static class SpanHelper
/// <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 Status ResolveSpanStatusForHttpStatusCode(ActivityKind kind, int httpStatusCode)
public static ActivityStatusCode ResolveSpanStatusForHttpStatusCode(ActivityKind kind, int httpStatusCode)
{
var upperBound = kind == ActivityKind.Client ? 399 : 499;
if (httpStatusCode >= 100 && httpStatusCode <= upperBound)
{
return Status.Unset;
return ActivityStatusCode.Unset;
}

return Status.Error;
return ActivityStatusCode.Error;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,17 @@ public override void OnStopActivity(Activity activity, object payload)

var response = context.Response;

activity.SetTag(SemanticConventions.AttributeHttpStatusCode, response.StatusCode);
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

#if !NETSTANDARD2_0
if (this.options.EnableGrpcAspNetCoreSupport && TryGetGrpcMethod(activity, out var grpcMethod))
{
AddGrpcAttributes(activity, grpcMethod, context);
}
else if (activity.GetStatus().StatusCode == StatusCode.Unset)
else if (activity.Status == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, response.StatusCode));
}
#else
if (activity.GetStatus().StatusCode == StatusCode.Unset)
if (activity.Status == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, response.StatusCode));
}
Expand Down Expand Up @@ -298,7 +296,7 @@ public override void OnException(Activity activity, object payload)
activity.RecordException(exc);
}

activity.SetStatus(Status.Error.WithDescription(exc.Message));
activity.SetStatus(ActivityStatusCode.Error, exc.Message);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,22 @@ public override void OnStopActivity(Activity activity, object payload)
// requestTaskStatus is not null
_ = this.stopRequestStatusFetcher.TryFetch(payload, out var requestTaskStatus);

StatusCode currentStatusCode = activity.GetStatus().StatusCode;
ActivityStatusCode currentStatusCode = activity.Status;
if (requestTaskStatus != TaskStatus.RanToCompletion)
{
if (requestTaskStatus == TaskStatus.Canceled)
{
if (currentStatusCode == StatusCode.Unset)
if (currentStatusCode == ActivityStatusCode.Unset)
{
activity.SetStatus(Status.Error);
activity.SetStatus(ActivityStatusCode.Error);
}
}
else if (requestTaskStatus != TaskStatus.Faulted)
{
if (currentStatusCode == StatusCode.Unset)
if (currentStatusCode == ActivityStatusCode.Unset)
{
// Faults are handled in OnException and should already have a span.Status of Error w/ Description.
activity.SetStatus(Status.Error);
activity.SetStatus(ActivityStatusCode.Error);
}
}
}
Expand All @@ -192,7 +192,7 @@ public override void OnStopActivity(Activity activity, object payload)
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);

if (currentStatusCode == StatusCode.Unset)
if (currentStatusCode == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public override void OnException(Activity activity, object payload)

if (exc is HttpRequestException)
{
activity.SetStatus(Status.Error.WithDescription(exc.Message));
activity.SetStatus(ActivityStatusCode.Error, exc.Message);
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ private static void AddExceptionTags(Exception exception, Activity activity)
return;
}

Status status;
ActivityStatusCode status;
string exceptionMessage = null;

if (exception is WebException wexc)
{
if (wexc.Response is HttpWebResponse response)
Expand All @@ -156,28 +158,31 @@ private static void AddExceptionTags(Exception exception, Activity activity)
{
case WebExceptionStatus.Timeout:
case WebExceptionStatus.RequestCanceled:
status = Status.Error;
status = ActivityStatusCode.Error;
break;
case WebExceptionStatus.SendFailure:
case WebExceptionStatus.ConnectFailure:
case WebExceptionStatus.SecureChannelFailure:
case WebExceptionStatus.TrustFailure:
case WebExceptionStatus.ServerProtocolViolation:
case WebExceptionStatus.MessageLengthLimitExceeded:
status = Status.Error.WithDescription(exception.Message);
status = ActivityStatusCode.Error;
exceptionMessage = exception.Message;
break;
default:
status = Status.Error.WithDescription(exception.Message);
status = ActivityStatusCode.Error;
exceptionMessage = exception.Message;
break;
}
}
}
else
{
status = Status.Error.WithDescription(exception.Message);
status = ActivityStatusCode.Error;
exceptionMessage = exception.Message;
}

activity.SetStatus(status);
activity.SetStatus(status, exceptionMessage);
if (Options.RecordException)
{
activity.RecordException(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ void ConfigureTestServices(IServiceCollection services)
Assert.Single(exportedItems);
var activity = exportedItems[0];

Assert.Equal(200, activity.GetTagValue(SemanticConventions.AttributeHttpStatusCode));
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

var status = activity.GetStatus();
Assert.Equal(status, Status.Unset);

Assert.Equal(ActivityStatusCode.Unset, activity.Status);
ValidateAspNetCoreActivity(activity, "/api/values");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public async void ExampleTest()
Assert.Equal("localhost:5000", activity.GetTagValue(SemanticConventions.AttributeHttpHost));
Assert.Equal("GET", activity.GetTagValue(SemanticConventions.AttributeHttpMethod));
Assert.Equal("1.1", activity.GetTagValue(SemanticConventions.AttributeHttpFlavor));
Assert.Equal(200, activity.GetTagValue(SemanticConventions.AttributeHttpStatusCode));
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
Assert.True(activity.Status == ActivityStatusCode.Unset);
Assert.True(activity.StatusDescription is null);
}

public async void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,25 @@ public async Task SuccessfulTemplateControllerCallGeneratesASpan(
Assert.Equal("http", activity.GetTagValue(SemanticConventions.AttributeHttpScheme));
Assert.Equal(urlPath, activity.GetTagValue(SemanticConventions.AttributeHttpTarget));
Assert.Equal($"http://localhost{urlPath}{query}", activity.GetTagValue(SemanticConventions.AttributeHttpUrl));
Assert.Equal(statusCode, activity.GetTagValue(SemanticConventions.AttributeHttpStatusCode));
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

if (statusCode == 503)
{
Assert.Equal(Status.Error.StatusCode, activity.GetStatus().StatusCode);
Assert.Equal(ActivityStatusCode.Error, activity.Status);
}
else
{
Assert.Equal(Status.Unset, activity.GetStatus());
Assert.Equal(ActivityStatusCode.Unset, activity.Status);
}

// Instrumentation is not expected to set status description
// as the reason can be inferred from SemanticConventions.AttributeHttpStatusCode
if (!urlPath.EndsWith("exception"))
{
Assert.True(string.IsNullOrEmpty(activity.GetStatus().Description));
Assert.True(string.IsNullOrEmpty(activity.StatusDescription));
}
else
{
Assert.Equal("exception description", activity.GetStatus().Description);
Assert.Equal("exception description", activity.StatusDescription);
}

if (recordException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,11 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut
Assert.Equal(tc.SpanName, activity.DisplayName);

// Assert.Equal(tc.SpanStatus, d[span.Status.CanonicalCode]);
Assert.Equal(
tc.SpanStatus,
activity.GetTagValue(SpanAttributeConstants.StatusCodeKey) as string);
Assert.Equal(tc.SpanStatus, activity.Status.ToString());

if (tc.SpanStatusHasDescription.HasValue)
{
var desc = activity.GetTagValue(SpanAttributeConstants.StatusDescriptionKey) as string;
var desc = activity.StatusDescription;
Assert.Equal(tc.SpanStatusHasDescription.Value, !string.IsNullOrEmpty(desc));
}

Expand Down Expand Up @@ -194,7 +192,7 @@ public async Task DebugIndividualTestAsync()
""responseCode"": 399,
""responseExpected"": true,
""spanName"": ""HTTP GET"",
""spanStatus"": ""UNSET"",
""spanStatus"": ""Unset"",
""spanKind"": ""Client"",
""spanAttributes"": {
""http.scheme"": ""http"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ public async Task TestRequestWithException(string method)
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
Assert.Equal("Stop", exceptionEvent.Key);

Assert.NotNull(activity.GetTagValue(SpanAttributeConstants.StatusCodeKey));
Assert.NotNull(activity.GetTagValue(SpanAttributeConstants.StatusDescriptionKey));
Assert.True(exceptionEvent.Value.Status != ActivityStatusCode.Unset);
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotNull(exceptionEvent.Value.StatusDescription);
}

/// <summary>
Expand Down Expand Up @@ -583,8 +583,8 @@ public async Task TestCanceledRequest(string method)
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
Assert.Equal("Stop", exceptionEvent.Key);

Assert.NotNull(exceptionEvent.Value.GetTagValue(SpanAttributeConstants.StatusCodeKey));
Assert.Null(exceptionEvent.Value.GetTagValue(SpanAttributeConstants.StatusDescriptionKey));
Assert.True(exceptionEvent.Value.Status != ActivityStatusCode.Unset);
Assert.True(exceptionEvent.Value.StatusDescription == null);
}

/// <summary>
Expand Down Expand Up @@ -622,8 +622,8 @@ public async Task TestSecureTransportFailureRequest(string method)
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
Assert.Equal("Stop", exceptionEvent.Key);

Assert.NotNull(exceptionEvent.Value.GetTagValue(SpanAttributeConstants.StatusCodeKey));
Assert.NotNull(exceptionEvent.Value.GetTagValue(SpanAttributeConstants.StatusDescriptionKey));
Assert.True(exceptionEvent.Value.Status != ActivityStatusCode.Unset);
Assert.NotNull(exceptionEvent.Value.StatusDescription);
}

/// <summary>
Expand Down Expand Up @@ -664,8 +664,8 @@ public async Task TestSecureTransportRetryFailureRequest(string method)
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
Assert.Equal("Stop", exceptionEvent.Key);

Assert.NotNull(exceptionEvent.Value.GetTagValue(SpanAttributeConstants.StatusCodeKey));
Assert.NotNull(exceptionEvent.Value.GetTagValue(SpanAttributeConstants.StatusDescriptionKey));
Assert.True(exceptionEvent.Value.Status != ActivityStatusCode.Unset);
Assert.NotNull(exceptionEvent.Value.StatusDescription);
}

[Fact]
Expand Down
Loading