# Alertmanager "function \"query\" not defined": template debugging

TL;DR: `query` is a Prometheus template function, not an Alertmanager one. Alertmanager cannot run PromQL inside a notification template. Move the query into the alerting rule in Prometheus so the result arrives as a label or annotation, then reference it with the standard template variables in the Alertmanager template.

```text
template: default.tmpl:1: function "query" not defined
```

## Steps

1. Find the offending line. Search your templates for `query` used as a function call; that call can never work in Alertmanager.
**Expected:** You locate the exact template line.

2. Move the PromQL into the Prometheus alerting rule. Compute the value in the rule's expression or annotations at alert time, so it arrives with the alert.
**Expected:** The alert carries the data as labels or annotations.

3. Rewrite the template using only Alertmanager-supported functions: the standard label and annotation variables plus its built-in helpers for text manipulation and formatting.
**Expected:** The template renders without unknown-function errors.

4. Validate locally before deploying. Render a test alert through the template and check the config.
**Expected:** Clean render, no `function not defined`.

5. If you genuinely need a live lookup at notify time, do it in the receiver (a webhook handler) instead of the template. Templates are for formatting, not querying.
**Expected:** The template stays simple and the lookup happens where it belongs.

## Use this when
- Alertmanager fails to render notifications with `function "query" not defined`

## Not for this skill when
- A different function is undefined. Same fix family, but check that function's home first
- Prometheus rule evaluation itself errors. That is the rule, not the template
- The template fails with a nil pointer. That is a missing label, a different bug

## Variant phrasings
### template: alert:1: function "query" not defined
Same error from an inline template rather than a file.

### Another Prometheus-only function shows as not defined
Same root cause: Prometheus template functions do not exist in Alertmanager.

## Why it happens
Alertmanager embeds Go templates with its own small function map. Prometheus adds `query` and friends only in its own template engine, which evaluates rule annotations. The two share syntax but not functions, so templates copied from Prometheus rule examples break in Alertmanager.

## Edge cases
- You cannot add custom functions to Alertmanager without rebuilding it; work within the built-in set
- The same template file can be valid for Prometheus annotations and invalid for Alertmanager; keep the two uses separate
- A webhook receiver can enrich alerts with fresh query results at notify time if the template alone is not enough

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_Y5Gu1N30rT6IaXbvn0ceug
