Measure Reliability

First, we can start to measure the reliability of critical requests. We'll use the PolarisService to measure the reliability of an HTTP request in a service.

data.service.ts
export class DataService {
  getRows(): Observable<Row[]> {
    const instrument = inject(PolarisService).getInstrument('grid-data');
    return inject(HttpClient).get<Row[]>('/api/rows').pipe(
      tap(() => {
        instrument.done();
      }),
      catchError((error) => {
        instrument.fail(error);
        return throwError(error);
      }),
    );
  }
}

Let's review the code above:

  1. We get an instrument using the getInstrument() method of the PolarisService.
  2. We make an HTTP request using the Angular HttpClient.
  3. We use the tap operator to call the done() method on the Instrument when the request is successful.
  4. We use the catchError operator to call the fail() method on the Instrument when the request fails.

Intercepting HTTP Requests

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.

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
}