Optimize Alert Messages with Templates

Reference guide for variables and template functions in Dash0 check rules. Use these to create dynamic, contextual alert messages with automatic formatting for better incident response.

Overview

Dash0's check rules support dynamic content through variables and template functions in the summary and description fields. This templating system, compatible with Prometheus conventions, allows you to create contextual alert messages that automatically include relevant information from the failing check.

When a check rule detects an issue, you can reference specific label values and the actual metric value that caused the alert. Template functions then help format this data for better readability in notifications.

Configure templates in the check rule's Notifications card:

Summary and description template fields with channel assignment, additional labels, and annotations.

Reference Available Variables

Use these variables to dynamically include metric values and label data in your alert messages. Dash0 provides three primary variables for use in check rule templates:

  • $__threshold contains the threshold value that was exceeded (either degraded or critical, whichever fired). This is the value you configured in the Thresholds section of the check rule. Note the two leading underscores, matching the $__threshold placeholder you use in the check rule expression.
  • $value contains the actual numerical value that caused the check to fail. This represents the last evaluated metric value that exceeded your defined threshold.
  • $labels.<key> references a label of the series that failed the check. Replace <key> with the actual label name. For example, $labels.service_name retrieves the service name, while $labels.k8s_pod_name gets the pod name.

Where labels come from:

$labels holds only the labels of the series that your PromQL query returns. These are the labels in the legend of the evaluation preview chart.

Labels from the Additional Labels section of the check rule are not in $labels. If team is only an additional label, {{ $labels.team }} renders <no value>. To show the value of an additional label in the summary, write the value in the summary itself.

An additional label can use $labels in its value, for example owner: {{ $labels.service_name }}. To show the same value in the summary, repeat the same template there.

You can use these variables in both the summary and description fields of your check rules. They are replaced with actual values when the alert fires, providing specific context about what triggered it.

The check rule name is not a template. Dash0 shows the name exactly as you write it, for example in the subject of an email notification. Use the summary for a title that names the failing series.

Understand Template Validation

Dash0 validates your templates when you save a check rule. If a template uses a variable or function that does not exist, or has a syntax error such as a missing {{ end }}, the check rule is not saved and the error message names the field to fix.

Format Values with Template Functions

Dash0 supports Prometheus alerting-rule template functions for numerical and string formatting. These are rule templates, not Alertmanager notification templates. You can use the following functions in alert messages:

Numerical Formatting

  • humanize converts large numbers into readable formats using decimal-based units. For example, 1000 becomes 1k, and 1000000 becomes 1M.
  • humanize1024 applies binary-based formatting, commonly used for data sizes. It converts 1024 to 1ki and 1048576 to 1Mi. Append the unit yourself, such as B for bytes.
  • humanizeDuration transforms seconds into human-readable time formats. A value of 3661 becomes 1h 1m 1s.
  • humanizePercentage formats decimal values as percentages. For instance, 0.85 becomes 85%.

String Formatting

  • title capitalizes the first letter of each word in the string.
  • toUpper converts the entire string to uppercase.
  • toLower converts the string to lowercase.
  • reReplaceAll replaces each match of a regular expression. For example, {{ reReplaceAll "^(.+)-[a-z0-9]+-[a-z0-9]+$" "$1" $labels.k8s_pod_name }} turns the pod name checkout-7d9f8b6c5-x2k4q into checkout.
Note

Template function autocomplete is not available in the UI editor. Refer to the Prometheus rule-template reference  for the available functions.

Apply Template Functions

Combine variables with template functions using the pipe operator to format values for better readability in notifications. Template functions wrap variables using the {{ }} syntax:

CPU usage is {{ $value | humanizePercentage }} on service {{ $labels.service_name }}
Alert triggered for {{ $labels.deployment_environment_name | title }} environment in {{ $labels.cloud_region | toUpper }}
Memory usage has reached {{ $value | humanize1024 }}B on pod {{ $labels.k8s_pod_name }} in namespace {{ $labels.k8s_namespace }}

Further Reading

Last updated: