Rust
Adding alerts and SLOs

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

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.

use autometrics::objectives::{Objective, ObjectiveLatency, ObjectivePercentile};
 
const API_SLO: Objective = Objective::new("api")
    .success_rate(ObjectivePercentile::P99_9)
    .latency(ObjectiveLatency::Ms250, ObjectivePercentile::P99);
 

Add Objectives to the autometrics macro

The Objective (API_SLO in this case) can be passed as an argument to the autometrics macro to include the given function in that objective.

#[autometrics(objective = API_SLO)]
pub fn create_user() {
        // ...
}

You can group multiple functions into a single objective by passing the same objective to multiple functions:

#[autometrics(objective = API_SLO)]
pub fn create_user() {
        // ...
}
 
#[autometrics(objective = API_SLO)]
pub fn get_user() {
        // ...
}
 
#[autometrics(objective = API_SLO)]
pub fn delete_user() {
        // ...
}

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).