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:
- We get an instrument using the
getInstrument()
method of thePolarisService
. - We make an HTTP request using the Angular
HttpClient
. - We use the
tap
operator to call thedone()
method on theInstrument
when the request is successful. - We use the
catchError
operator to call thefail()
method on theInstrument
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.
- We import the
polarisInterceptor
andprovidePolaris
functions from the@getpolaris.ai/sdk-angular
package. - We provide the
polarisInterceptor
function to the Angular HTTP client using thewithInterceptors()
function. - 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
}