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

[BUG] AzureMonitorExporter marks 404 response as errors, even if explicitly marked as successful in OpenTelemetry #41993

Closed
rvanheest opened this issue Feb 14, 2024 · 5 comments · Fixed by #43594
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. Monitor - Exporter Monitor OpenTelemetry Exporter needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@rvanheest
Copy link
Contributor

rvanheest commented Feb 14, 2024

Library name and version

Azure.Monitor.OpenTelemetry.Exporter

Describe the bug

My webapi yields a 404 response when the requested data cannot be found. In my case, this is not supposed to be marked as an error when exporting the OpenTelemetry data to ApplicationInsights.

As suggested in various other places, I have added an EnrichWithHttpResponse to the AddAspNetCoreInstrumentation to manually change the activity's status to ActivityStatusCode.Ok.

builder
    .AddAspNetCoreInstrumentation(options =>
    {
        options.EnrichWithHttpResponse = (activity, response) =>
        {
            if (response.StatusCode == 404)
            {
                activity.SetStatus(ActivityStatusCode.Ok);
            }
        };
    })
    .AddConsoleExporter()
    .AddAzureMonitorTraceExporter(options =>
    {
        options.ConnectionString = "<my connection string>";
    });

However, I'm still getting my 404 responses marked as errors in ApplicationInsights, even though the console exporter clearly shows the status of the activity to be marked as successful.

Expected behavior

I would expect the manually set activity.Status to be respected here, rather than it being overridden by interpreting the response code. It would still make sense to look at the response code when the value of activity.Status is ActivityStatusCode.Unset, but not when the activity.Status is set to AcitivityStatusCode.Ok.

Actual behavior

In the ApplicationInsights exporter, the Activity containing the 404 response is transformed into a RequestData, where the Success property is determined by the IsSuccess function.

internal static bool IsSuccess(Activity activity, string? responseCode, OperationType operationType)
{
if (operationType.HasFlag(OperationType.Http)
&& responseCode != null
&& int.TryParse(responseCode, out int statusCode))
{
bool isSuccessStatusCode = statusCode != 0 && statusCode < 400;
return activity.Status != ActivityStatusCode.Error && isSuccessStatusCode;
}
else
{
return activity.Status != ActivityStatusCode.Error;
}
}

Since the operationType is an http call and the response code is set to 404, the if-branch applies, evaluating the value for isSuccessStatusCode to false and making the function return false, even though activity.Status != ActivityStatusCode.Error evaluates to true.

In this IsSuccess function is where I would expect the predetermined activity.Status to be respected. Perhaps the function should be something like

internal static bool IsSuccess(Activity activity, string? responseCode, OperationType operationType)
{
    if (activity.Status == ActivityStatusCode.Unset
        && operationType.HasFlag(OperationType.Http)
        && responseCode != null
        && int.TryParse(responseCode, out int statusCode))
    {
        return statusCode != 0 && statusCode < 400;
    }
    else
    {
        return activity.Status != ActivityStatusCode.Error;
    }
}

Reproduction Steps

See bug description

Environment

No response

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 14, 2024
@jsquire jsquire added Service Attention Workflow: This issue is responsible by Azure service team. Client This issue points to a problem in the data-plane of the library. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team Monitor - Exporter Monitor OpenTelemetry Exporter and removed needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Feb 15, 2024
Copy link

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @cijothomas @rajkumar-rangaraj @reyang @TimothyMothra @vishweshbankwar.

1 similar comment
Copy link

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @cijothomas @rajkumar-rangaraj @reyang @TimothyMothra @vishweshbankwar.

@rvanheest
Copy link
Contributor Author

Any updates on this? Not sure what the process/next step is. Do you want me to create a PR for this with my proposed solution?

@vishweshbankwar
Copy link
Contributor

@rvanheest - Apologies for the delay. I think the change you have suggested is reasonable. Would you be able to create the PR for this?

@rvanheest
Copy link
Contributor Author

@vishweshbankwar sure, here you go - #43594

@github-actions github-actions bot locked and limited conversation to collaborators Jul 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. Monitor - Exporter Monitor OpenTelemetry Exporter needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
4 participants