o11y-analysis-tools

A collection of static analysis and testing tools for PromQL-compatible monitoring systems. All tools are written in Go and designed for use in CI/CD workflows with --check functionality by default.
Table of Contents
Background
This collection of static analysis tools are intended to help:
- maintain PromQL rules, by keeping them readable, consistent, unit tested and most importantly, approachable by non PromQL experts
- identify common bugs regarding labels, time controls, etc
- maintain high quality, actionable alerts, including with the ability to preview how an alert would render
- identifying alerts in need of refinement and even deletion once they are no longer useful
Each of these tools is inspired from the mistakes I've made when writing PromQL rules, issues seen when peer reviewing PromQL PRs or providing PromQL
consultations, during my tenure as a member of Google SRE
Tools
1. promql-fmt - PromQL Expression Formatter
Statically analyzes and formats PromQL expressions for proper multiline formatting.
Features:
- Checks PromQL expressions for multiline formatting standards
- Automatically formats long or complex expressions for better readability
- Integrates with CI to enforce formatting standards
Usage:
# Check formatting (default mode, exits 1 if issues found)
promql-fmt --check ./alerts/
# Automatically fix formatting issues
promql-fmt --fix ./alerts/
promql-fmt --fmt ./alerts/ # alias for --fix
# Verbose output
promql-fmt --verbose --check ./prometheus/
Example:
Before:
expr: sum(rate(http_requests_total{job="api",status=~"5.."}[5m])) by (instance) / sum(rate(http_requests_total{job="api"}[5m])) by (instance)
After:
expr: |
sum (
rate(http_requests_total{job="api",status=~"5.."}[5m])
)
/ on (instance)
sum by (instance) (
rate(http_requests_total{job="api"}[5m])
)
Note: The formatter automatically:
- Removes redundant aggregation clauses from the left operand when both operands share the same
by clause
- Adds explicit
on() clauses for vector matching based on the aggregation labels
- Follows PromQL best practices where only the final operand needs the aggregation
2. label-check - Label Standards Enforcement
Enforces required labels in PromQL expressions to prevent collisions in multi-tenant observability platforms.
Features:
- Validates that all PromQL expressions include required labels
- Default: checks for
job label to prevent tenant collisions
- Configurable for any set of required labels
- Detailed violation reporting with line numbers
Usage:
# Check for default 'job' label
label-check --check ./alerts/
# Check for multiple required labels
label-check --labels=job,namespace ./alerts/
# Check specific file
label-check --labels=job,cluster alerts.yml
Example Output: