API Reliability Monitoring with Axios

In this article, you'll learn how to measure the performance and reliability of your Axios requests.

Prerequisites

Setup Axios interceptors

Now that you've connected your application to Polaris, you can start sending request measurements to Polaris. To do this, you'll need to set up Axios interceptors.

React
import axios from 'axios';
import { measure, Instrument } from '@getpolaris.ai/sdk';

let instrument: Instrument;
axios.interceptors.request.use((config) => {
  instrument = measure('request');
  return config;
});

axios.interceptors.response.use(
  (response) => {
    instrument.done();
    return response;
  },
  (error) => {
    instrument.fail({ error });
    return Promise.reject(error);
});

Now, you can use Axios to make requests. All requests made with Axios will send measurements to Polaris.

Create Average Response Time Indicator

Next, let's create an indicator of performance in Polaris for the request event in our app:

  • Log in to Polaris and navigate to the Indicators for your application.
  • Click the Create Indicator button.
  • Enter a name for the indicator, such as API Request.
  • Choose a duration for the indicator, such as the Last Hour.
  • Next, choose the Average operation to measure the average duration of API requests in the last hour.
  • Finally, provide the following predicate function:
ESM
function main(measurement) {
  return measurement.eventName === 'request';
}

The indicator predicate function above will filter all measurements sent to Polaris for those where the eventName is request.

Create Error Rate Indicator

Next, let's create an indicator of reliability in Polaris for the request event in our app:

  • Log in to Polaris and navigate to the Indicators for your application.
  • Click the Create Indicator button.
  • Enter a name for the indicator, such as API Error Rate.
  • Choose a duration for the indicator, such as the Last Hour.
  • Next, choose the Rate operation to measure the error rate of API requests in the last hour.
  • Finally, provide the following predicate function:
ESM
function main(measurement) {
  return measurement.eventName === 'request';
}

Next Steps

Congratulations! You've successfully set up Axios to send request measurements to Polaris.