How to ignore http status code 501 from being considered as error in APM

Hi,

We are using elastic-apm-node package to send APM to our Elastic Cloud deployment. One of our services tends to receive a lot of requests from a 3rd party which we don't support at this time and so we return 501 status code.

How do we avoid 501 from getting considered as an error in APM Failed transaction rate?

Hi @Anandakumar

5XX responses are considered transaction errors and you see int in Kibana as such. They count in the "failed transaction rate" and you see the failed status in the transaction details. like here

Since the issue is a 3rd party app/service calling an endpoint where they shouldn't I'd suggest to change the HTTP status code to 4XX indicating there is an issue in the request. 4XX statuses are not reported as failed in APM.

If changing the HTTP status is not possible there is another solution that requires updating the instrumentation of your app. You should use addTransactionFilter API to change the outcome of the transaction from failure to unknown or success (probably you'd prefer the later).

Here is a small snippet on how to change the status of a transaction

// Keep a reference to the APM agent
const apm = require('elastic-apm-node').start({
  serviceName: 'example-trace-http',
  usePathAsTransactionName: true,
});

// call the agent's API to modify transaction payloads
// before sending
apm.addTransactionFilter((payload) => {
  if (
    payload &&
    payload.context &&
    payload.context.response &&
    payload.context.response.status_code === 501
  ) {
    payload.outcome = 'unknown';
  }
  return payload;
});

Hope this helps on your issue.
Cheers,
David