Application error monitoring
In this article, we'll learn how to monitor the error rate in our production Angular applications.
Prerequisites
ErrorHandler
Implement Angular 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
PolarisErrorHandler
that implements theErrorHandler
interface. - We inject the
PolarisService
into the constructor. - We implement the
handleError
method and invoke thelogMeasurement
method on thePolarisService
. - We pass the
app-error
measurement name,FAILURE
result, and the error object to thelogMeasurement
method. - Note that we pass
0
as the duration since we don't have a duration for the error.
PolarisErrorHandler
Register the 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
providePolaris
function from the@getpolaris.ai/sdk-angular
package. - We call the
providePolaris
function and pass the `apiKey - We register the
PolarisErrorHandler
as the global error handler by providing it in thebootstrapApplication
providers 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
PolarisModule
from the@getpolaris.ai/sdk-angular
package. - We configure the
PolarisModule
with theforRoot
method and pass the `apiKey - We register the
PolarisErrorHandler
as the global error handler by providing it in theAppModule
providers 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.