Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.09k stars 1.2k forks source link

[@azure/monitor-opentelemetry] - Requests with status code 304 are shown as failed in application insights #29398

Closed despgiat closed 5 months ago

despgiat commented 6 months ago

Describe the bug Currently some requests are treated and shown as errors, e.g. 304 which is a redirect / cache-hit. The majority of the errors that we get are 304 requests. Moreover, we don't get any information about these requests in application insights (in the first screenshot below you can see how the the dashboard looks like when the 304 is clicked to reveal more information about the errors). Also, these requests are not shown as errors in transaction search, and when they are filtered (ex. by response code), they do not show in the failed requests graph anymore.

To Reproduce Our setup is similar to this example:

import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from '@azure/monitor-opentelemetry';
import { metrics, trace } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http';
import { MongoDBInstrumentationConfig } from '@opentelemetry/instrumentation-mongodb';
import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { IncomingMessage } from 'http';
import { env } from '../env';

const httpInstrumentationConfig: HttpInstrumentationConfig = {
  enabled: true,
  ignoreIncomingRequestHook: (request: IncomingMessage) => {
    // Ignore OPTIONS incoming requests
    if (request.method === 'OPTIONS') {
      return true;
    }
    return false;
  },
};

const mongoInstrumentationConfig: MongoDBInstrumentationConfig = {
  enabled: true,
  enhancedDatabaseReporting: true,
};

export const initializeTelemetry = () => {
  const customResource = Resource.EMPTY;
  customResource.attributes[SEMRESATTRS_SERVICE_NAME] = 'Backend';

  const options: AzureMonitorOpenTelemetryOptions = {
    resource: customResource,
    azureMonitorExporterOptions: {
      connectionString: env.APPLICATION_INSIGHTS_CONNECTION_STRING,
    },
    instrumentationOptions: {
      http: httpInstrumentationConfig,
      mongoDb: mongoInstrumentationConfig,
    },
  };

  useAzureMonitor(options);
  addOpenTelemetryInstrumentation();
};

/**
 * Add additional OpenTelemetry instrumentation that is not bundled with Azure OpenTelemetry Distro
 */
const addOpenTelemetryInstrumentation = () => {
  const instrumentations = [new ExpressInstrumentation()];
  registerInstrumentations({
    tracerProvider: trace.getTracerProvider(),
    meterProvider: metrics.getMeterProvider(),
    instrumentations: instrumentations,
  });
};

Expected behavior The requests with status code 304 are not shown as errors in application insights.

Screenshots Screenshot 2024-04-23 at 11 11 26 AM

Screenshot 2024-04-23 at 11 38 07 AM

Screenshot 2024-04-23 at 11 38 57 AM

Additional context We played around a bit and tried to set custom attributes to the http requests using a SpanEnrichingProcessor but it seems that no custom attributes could be added to these failed requests.

github-actions[bot] commented 6 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @hectorhdzg @JacksonWeber @ramthi.

JacksonWeber commented 6 months ago

@despgiat I'm not able to reproduce this situation of 304 status codes being shown as errors. Requests marked as response code 304 are being shown as I expect in the transaction blade when using your example code with an express server that returns a 304 response.

Can you provide more information on where these 304 response codes are being returned from or more details for reproducing your scenario? Thanks!

p-hlp commented 6 months ago

@JacksonWeber Guess I can chime in here. You can use this repo for repro. It was initially used for a different problem with application insights js sdk/azure otel distro, but has the same issue with 304's showing up under failures. You can find detailed steps for reproducing the scenario in the readme. For the setup - personally I used two ai resources (web client/api) in same workspace, however a shared one also works.

JacksonWeber commented 6 months ago

@p-hlp Thank you for doing this work to repro. I'll recreate this scenario and work on debug as soon as I can.

p-hlp commented 5 months ago

@JacksonWeber Are there any updates by chance? It's a bit of an annoying problem. Otherwise anything I could do on my side to support with getting this going?

JacksonWeber commented 5 months ago

@p-hlp Apologies I haven't had much time to look into this earlier. Investigating this afternoon and I'll provide an update ASAP.