opentelemetry-dotnet icon indicating copy to clipboard operation
opentelemetry-dotnet copied to clipboard

Cancelled Requests Fail to Report Correct Status Code

Open RehanSaeed opened this issue 3 years ago • 3 comments

Bug Report

  • OpenTelemetry.Exporter.Console 1.3.0
  • OpenTelemetry.Extensions.Hosting 1.0.0-rc9.6
  • OpenTelemetry.Instrumentation.AspNetCore 1.0.0-rc9.6
  • OpenTelemetry.Instrumentation.StackExchangeRedis 1.0.0-rc9.7

Runtime version: net6.0

Symptom

When a client cancels a request, I want to be able to set a 499 'Client Closed Request' status code for logging/telemetry purposes and shortcut the ASP.NET middle-ware pipeline to save time. This is described further in Andrew Lock's blog post here:

https://andrewlock.net/using-cancellationtokens-in-asp-net-core-minimal-apis/

What is the expected behavior?

I expect Open Telemetry to log the correct status code in http.status_code, so I can view telemetry and see all cancelled 499 responses.

What is the actual behavior?

A status code of zero is logged by Open Telemetry while Serilog is correctly logging 499.

image

Reproduce

Add this controller to an open telemetry project with a console exporter.

public class SomeController : ControllerBase
{
    [HttpGet("/foo")]
    public async Task Foo(CancellationToken cancellationToken)
    {
        try
        {
            await Task.Delay(10_000, cancellationToken);
        }
        catch (OperationCanceledException)
        when (cancellationToken.IsCancellationRequested)
        {
            this.HttpContext.Response.StatusCode = 499;
            this.HttpContext.Features.Get<IHttpActivityFeature>()?.Activity.AddEvent(new ActivityEvent("Cancelled"));
        }
    }
}

RehanSaeed avatar Aug 24 '22 15:08 RehanSaeed

This has purposely been removed in the following fix: https://github.com/open-telemetry/opentelemetry-dotnet/pull/2904

DavidStevensWillow avatar Aug 26 '22 05:08 DavidStevensWillow

@DavidStevensWillow That PR is talking about SpanStatus and seems to say nothing about the StatusCode.

RehanSaeed avatar Aug 26 '22 08:08 RehanSaeed

I'm getting something similar as OpenTelemetry Status Code is 0 when Serilog Request logging reports it as a 302. I don't have replication steps at the moment.

Edit: also noting that the status code is Error.

AlexandruRus23 avatar Aug 30 '22 09:08 AlexandruRus23