Slide 1 of 3

Build an alert with code

Build an alert with the Code mode

You just built an alert in Builder mode. Code mode follows the same rule flow. The difference is how you write the query: you type PromQL instead of picking the metric and operations from menus in the UI.

Use caseRecommended mode
Standard metric, labels, and common operationsBuilder
Complex nested expressions or exact syntax controlCode
Team members new to PromQLBuilder

Start in Builder when you’re learning. Switch to Code when you need more control than Builder can express clearly.

Path

This Path walks you through writing the alert query in PromQL, then creating a silence to suppress notifications during maintenance.

Learning path

First alert rule with query language

Build a Grafana alert rule in Code mode — write a PromQL error-rate query, set a condition, add labels, configure evaluation and notifications, and save.

14 min
Intermediate
Docs & blog posts

Open in Grafana Cloud

Complete this learning path directly in your Grafana Cloud stack with an interactive learning experience.

Script

You built your first alert in Builder mode. Code mode uses the same alert rule flow, but you write the query in PromQL instead of picking metrics and operations from menus.

Choose Code mode when you need query patterns that are hard or impossible to express in Builder, or when you already know the PromQL you want to reuse.

This Path walks you through writing the query in Code mode, then creating a silence so you can suppress notifications during maintenance.