Measure API Error Rate

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

Prerequisites

Angular Interceptor

Angular interceptors are an excellent choice for measuring the error rate of your API.

You can create your own Angular interceptor to measure the error rate of your API, or you can choose to use the provided polarisInterceptor Angular interceptor. Let's look at an example of creating your own Angular interceptor.

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

export const measureApiInterceptor: HttpInterceptorFn = (req, next) => {
  // optionally filter requests

  const instrument = measure('request', {
    method: req.method,
    url: req.url,
  });
  return next(req).pipe(
    tap((event) => {
      if (event.type === HttpEventType.Response) {
        instrument.done({
          status: event.status,
        });
      }
    }),
    catchError((error) => {
      instrument.fail({ error });
      throw error;
    })
  );
};

Let's review the code above.

  1. Import the measure function from the Polaris SDK.
  2. Create an interceptor function that takes the HTTP request and the next interceptor in the chain.
  3. Optionally filter the requests.
  4. Create a new measurement using the measure function with the event name request and the metadata method and url.
  5. Return the next interceptor in the chain and add a tap operator to handle the response.
  6. If the response is successful, call the done method on the measurement with the metadata status.
  7. If the response fails, call the fail method on the measurement with the metadata error.

If you're using Angular modules in your application, you can create an Angular interceptor by implementing the HttpInterceptor interface.

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

@Injectable()
export class MeasureApiInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const instrument = measure('request', {
      method: req.method,
      url: req.url,
    });
    return next.handle(req).pipe(
      tap((event) => {
        if (event.type === HttpEventType.Response) {
          instrument.done({
            status: event.status,
          });
        }
      }),
      catchError((error) => {
        instrument.fail({ error });
        throw error;
      })
    );
  }
}

Provide polarisInterceptor to Angular HTTP client

Provide the polarisInterceptor function to the Angular HTTP client using the withInterceptors() function.

React
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { providePolaris, polarisInterceptor } from '@getpolaris.ai/sdk-angular';

bootstrapApplication(AppComponent, {
  providers: [
    providePolaris({
      apiKey: API_KEY,
    }),
    provideHttpClient(withInterceptors([polarisInterceptor]))
  ],
});

Configure Event Name

You can customize the eventName of the measurement. The default event name value is request.

React
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.

React
import { PolarisModule } from '@getpolaris.ai/sdk-angular';

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

Interceptor Metadata

If you are using the Angular interceptor for all HTTP requests, the following user-defined metadata will be added to the 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:

TS
  {
    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.
ESM
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