TypeScript
Adding alerts and SLOs

Adding alerts using Service-Level Objectives (SLOs) in TypeScript

Autometrics makes it easy to add Prometheus alerts using Service-Level Objectives (SLOs) to a function or group of functions.

This works using pre-defined Prometheus alerting rules, which can be loaded via the rule_files field in your Prometheus configuration. By default, most of the recording rules are dormant. They are enabled by specific metric labels that can be automatically attached by autometrics.

Pre-requisites

  1. Make sure you have setup and initialized Autometrics libraries as described in Quickstart section.

  2. Add the pre-configured Prometheus rules file to your Prometheus configuration. This file is located in the Autometrics shared repository (opens in a new tab)

Usage

Create Objectives with your desired SLOs

To use autometrics SLOs and alerts, create one or multiple Objectives based on the function(s) success rate and/or latency, as shown below.

import { autometrics, Objective, ObjectiveLatency, ObjectivePercentile } from "autometrics";
 
const API_SLO: Objective = {
  name: 'api',
  successRate: ObjectivePercentile.P99_9,
  latency: [ObjectiveLatency.Ms250, ObjectivePercentile.P99],
};

Add Objectives to the autometrics wrapper or decorator as an options object

The Objective (API_SLO in this case) can be added to the options object, passed into the autometrics wrapper including the given function in that objective.

Note: the options object, must always be the first item in the wrapper

import { autometrics, Objective, ObjectiveLatency, ObjectivePercentile } from "@autometrics/autometrics";
 
const API_SLO: Objective = {
  name: 'api',
  successRate: ObjectivePercentile.P99_9,
  latency: [ObjectiveLatency.Ms250, ObjectivePercentile.P99],
};
 
const apiHandlerFn = autometrics({ objective: API_SLO }, function apiHandler(
  // ...
));

Viewing SLOs and Alerts

Once you've added objectives to your code, you can use the Autometrics Service-Level Objectives (SLO) Dashboards (opens in a new tab) to visualize the current status of your objective(s).