Application error monitoring
In this article, we'll learn how to monitor the error rate in our production Angular applications.
Prerequisites
Implement Angular ErrorHandler
We will use Angular's ErrorHandler to catch and log errors in our application.
We will create a new class that implements the ErrorHandler interface and use it to log errors to Polaris.
class PolarisErrorHandler implements ErrorHandler {
constructor(private readonly polaris: PolarisService) {}
handleError(error: any): void {
this.polaris.logMeasurement(
'app-error',
MeasurementResult.FAILURE,
0,
{ error }
);
}
}
Let's review the code above:
- We create a new class
PolarisErrorHandlerthat implements theErrorHandlerinterface. - We inject the
PolarisServiceinto the constructor. - We implement the
handleErrormethod and invoke thelogMeasurementmethod on thePolarisService. - We pass the
app-errormeasurement name,FAILUREresult, and the error object to thelogMeasurementmethod. - Note that we pass
0as the duration since we don't have a duration for the error.
Register the PolarisErrorHandler
We need to register the PolarisErrorHandler as the global error handler in our application.
import { providePolaris } from '@getpolaris.ai/sdk-angular';
bootstrapApplication(AppComponent, {
providers: [
providePolaris({
apiKey: API_KEY,
}),
{
provide: ErrorHandler,
useClass: PolarisErrorHandler,
deps: [PolarisService],
},
],
});
Let's review the code above:
- We import the
providePolarisfunction from the@getpolaris.ai/sdk-angularpackage. - We call the
providePolarisfunction and pass the `apiKey - We register the
PolarisErrorHandleras the global error handler by providing it in thebootstrapApplicationproviders array.
If you are using modules in Angular, we'll configure PolarisErrorHandler in the AppModule:
import { PolarisModule } from '@getpolaris.ai/sdk-angular';
@NgModule({
imports: [
PolarisModule.forRoot({
apiKey: API_KEY,
}),
],
providers: [
{
provide: ErrorHandler,
useClass: PolarisErrorHandler,
deps: [PolarisService],
},
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Let's review the code above:
- We import the
PolarisModulefrom the@getpolaris.ai/sdk-angularpackage. - We configure the
PolarisModulewith theforRootmethod and pass the `apiKey - We register the
PolarisErrorHandleras the global error handler by providing it in theAppModuleproviders array.
Create Indicators
Next, we'll create an indicator to measure the error rate of the API.
- Create a new indicator in Polaris.
- Provide an indicator name. In this example, we'll use App Error Rate.
- Select the desired window of time for the indicator.
- Provide the following predicate function that will filter the measurements based on the interceptor's configured event name.
function filter(measurement) {
return measurement.eventName === 'app-error'
&& measurement.result === 'FAILURE';
}
We recommend creating multiple indicators with different time windows to monitor the error rate at different levels of granularity.
Create Objectives
Finally, we'll create objectives to monitor the error rate of the application.
Let's create an acceptable error rate objective.
- Create a new objective in Polaris.
- Provide an objective name. In this example, we'll use App Errors / Acceptable.
- Select the App Error Rate indicator we created earlier.
- 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%.
- 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.
- Create a new objective in Polaris.
- Provide an objective name. In this example, we'll use App Errors / Aspiration.
- Select the App Error Rate indicator we created earlier.
- 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%.
- Select the desired percentile for the objective. In this example, we'll use the 95th percentile.