Measure API Error Rate with Angular Interceptor

In this article, we'll learn how to measure the error rate of your API using the provided polarisInterceptor Angular interceptor.

Prerequisites

Angular Interceptor

Our Angular SDK provides an interceptor that measures the error rate of your API.

app.config.ts
import { polarisInterceptor, providePolaris } from '@getpolaris.ai/sdk-angular';

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(withInterceptors([polarisInterceptor]), withFetch()),
    providePolaris({
      apiKey: API_KEY
    }),
  ],
};

Let's review the code above.

  1. We import the polarisInterceptor and providePolaris functions from the @getpolaris.ai/sdk-angular package.
  2. We provide the polarisInterceptor function to the Angular HTTP client using the withInterceptors() function.
  3. We invoke the providePolaris function with the Polaris Application API key.

Angular Modules

Configure the Polaris Angular SDK and interceptor in your module-based application.

app.module.ts
import { PolarisModule, polarisInterceptor } from '@getpolaris.ai/sdk-angular';

@NgModule({
  imports: [
    PolarisModule.forRoot({
      apiKey: API_KEY,
    }),
  ],
  providers: [
    provideHttpClient(withInterceptors([polarisInterceptor]), withFetch()),
  ],
})
export class AppModule {}

Configure Event Name

By default, our interceptor measures the duration and success rate of all HTTP requests, sending each measumrent to Polaris using the request event name. You can easily configure the event name by providing the interceptor option to the providePolaris function.

app.config.ts
import { providePolaris } from '@getpolaris.ai/sdk-angular';

bootstrapApplication(AppComponent, {
  providers: [
    providePolaris({
      apiKey: API_KEY,
      interceptor: {
        eventName: 'api-request'
      }
    }),
  ],
});

If you're using Angular modules in your application, configure the eventName by specifying the options to the forRoot method.

app.module.ts
import { PolarisModule } from '@getpolaris.ai/sdk-angular';

@NgModule({
  imports: [
    PolarisModule.forRoot({
      apiKey: API_KEY,
      interceptor: {
        eventName: 'api-request'
      }
    }),
  ],
})
export class AppModule {}

Interceptor Metadata

The following user-defined metadata is added to each measurement:

  • url: The URL of the HTTP request.
  • method: The HTTP method of the request.
  • status: The HTTP status code of the response.

Here is an example of a measurement with the interceptor metadata:

JSON
  {
    eventName: 'request',
    userMetadata: {
      url: 'https://api.example.com',
      method: 'GET',
      status: 200
    },
    duration: 1000
  }

Create Indicator

Next, we'll create an indicator to measure the error rate of the API.

  1. Create a new indicator in Polaris.
  2. Provide an indicator name. In this example, we'll use API Error Rate.
  3. Select the desired window of time for the indicator. We recommend creating multiple indicators with different time windows to monitor the error rate at different levels of granularity.
  4. Provide the following predicate function that will filter the measurements based on the interceptor's configured event name.
Indicator predicate function
function filter(measurement) {
  return measurement.eventName === 'request';
}

Create Objectives

Finally, we'll create objectives to monitor the error rate of the API.

Let's create an acceptable error rate objective.

  1. Create a new objective in Polaris.
  2. Provide an objective name. In this example, we'll use API Errors / Acceptable.
  3. Select the API Error Rate indicator we created earlier.
  4. Set the desired target for the objective. In this example, we'll use a Lower Threshold with the value 0.01. This threshold establishes an error budget of 1%.
  5. Select the desired percentile for the objective. In this example, we'll use the 95th percentile.

Now, let's create an aspiration error rate objective.

  1. Create a new objective in Polaris.
  2. Provide an objective name. In this example, we'll use API Errors / Aspiration.
  3. Select the API Error Rate indicator we created earlier.
  4. Set the desired target for the objective. In this example, we'll use a Lower Threshold with the value 0.005. This threshold establishes an error budget of 0.5%.
  5. Select the desired percentile for the objective. In this example, we'll use the 95th percentile.

Next Steps