Tutorial Intro
Let's discover Sloscribe in less than 5 minutes.
Getting Started​
Get started by installing sloscribe.
Add sloscribe annotations to your codebase​
Add comments to your source code. See declarative comments for more info.
metric.go
// @sloth service chatgpt
var (
// @sloth.slo name chat-gpt-availability
// @sloth.slo objective 95.0
// @sloth.sli error_query sum(rate(tenant_failed_login_operations_total{client="chat-gpt"}[{{.window}}])) OR on() vector(0)
// @sloth.sli total_query sum(rate(tenant_login_operations_total{client="chat-gpt"}[{{.window}}]))
// @sloth.slo description 95% of logins to the chat-gpt app should be successful.
// @sloth.alerting name ChatGPTAvailability
metricTenantLogins = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "chatgpt",
Subsystem: "auth0",
Name: "tenant_login_operations_total",
})
metricTenantFailedLogins = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "chatgpt",
Subsystem: "auth0",
Name: "tenant_failed_login_operations_total",
})
)
Generate sloth SLO specifications from the codebase​
Run sloscribe init in the project's root directory. This will parse the root directory's different source files for @sloth annotations and
generate a specification (s) for the SLO definitions, finally printing them to standard out.
sloscribe init
You can also specify a specific file to parse by using the -f/--file flag.
sloscribe init -f metrics.go
Another way to parse a source file is to pass the input file through pipe to sloscribe.
cat metrics.go | ./sloscribe init -f -
Output:
# Code generated by sloscribe: https://github.com/slosive/sloscribe.
# DO NOT EDIT.
version: prometheus/v1
service: chatgpt
slos:
- name: chat-gpt-availability
description: 95% of logins to the chat-gpt app should be successful.
objective: 95
sli:
events:
error_query: sum(rate(tenant_failed_login_operations_total{client="chat-gpt"}[{{.window}}])) OR on() vector(0)
total_query: sum(rate(tenant_login_operations_total{client="chat-gpt"}[{{.window}}]))
alerting:
name: ChatGPTAvailability
The resulting specification can also be piped to a file rather than printed to the stdout.
sloscribe init > chatgpt.yaml
Generate Prometheus alerting group​
The sloth SLO specification can be used to generate a Prometheus alert group rules.yaml which can be used by a Prometheus instance.
sloth generate -i chatgpt.yaml -o ./rules.yml
Resulting alert groups.
Add Prometheus alert group to a Prometheus configuration​
The rules.yml from the previous steps can then be referenced in the Prometheus instance configuration, under the rule_files field.
# my global config
global:
scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "exporter"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9301"]