Prometheus Alertmanager
# AGENTS.md
Guidance for AI coding agents working in this repository. Keep this file concise and current. For endβuser docs, see `README.md` and https://prometheus.io/docs/alerting/alertmanager/.
## Project overview
Alertmanager handles alerts sent by clients such as Prometheus. It deduplicates, groups, routes, silences, and inhibits alerts, and dispatches them to receiver integrations (email, PagerDuty, Slack, webhook, etc.).
- Language: Go (see `go.mod` for the required version, currently `go 1.25`).
- Frontend (legacy): Elm app under `ui/app/` (Node version pinned in `.nvmrc`).
- Frontend (new): React + TypeScript + Mantine under `ui/mantine-ui/`.
- API: OpenAPI v2 spec in `api/v2/openapi.yaml`; Go server/client/models are generated via `scripts/swagger.sh`.
- Module path: `github.com/prometheus/alertmanager`.
## Repository layout
Topβlevel packages worth knowing:
- `cmd/alertmanager/` β main binary entry point (`main.go`); thin wrapper that parses flags and calls `app`.
- `app/` β embeddable Alertmanager runtime extracted from `cmd/alertmanager`. Owns the process lifecycle (`New`/`Start`/`Stop`/`Reload`/`Run`), subsystem wiring (`setup`), config-reload subgraph (`reloader`), listeners and `Options`. Lets tests and other binaries run Alertmanager inβprocess. See https://github.com/prometheus/alertmanager/issues/406.
- `cmd/amtool/` β CLI for interacting with the Alertmanager API.
- `api/` β HTTP API. `api/v2/` is the active API; `api/v1_deprecation_router.go` only returns deprecation responses.
- `cli/` β `amtool` command implementations.
- `cluster/` β HA gossip clustering (memberlist-based).
- `config/` β YAML config parsing, validation, secrets, coordinator.
- `dispatch/` β routing tree, alert grouping, dispatcher loop.
- `inhibit/` β inhibition rule engine.
- `mute/`, `silence/`, `timeinterval/` β muting, silences (with nflog), timeβbased mutes.
- `nflog/` β notification log (persisted dedup state).
- `notify/` β notification pipeline plus one subpackage per integration (`slack`, `pagerduty`, `email`, `webhook`, `discord`, `jira`, `msteams`, `msteamsv2`, `opsgenie`, `pushover`, `rocketchat`, `sns`, `telegram`, `victorops`, `webex`, `wechat`, `mattermost`, `incidentio`).
- `template/` β notification templating (Go templates + default email template). `template/email.tmpl` is generated from `template/email.html`.
- `provider/mem/` β inβmemory alert provider (the only provider).
- `store/` β alert/silence store helpers.
- `types/` β core domain types (`Alert`, `Silence`, `Matcher`, ...).
- `matcher/` β label matcher parsing (used by routes, silences, inhibitions).
- `featurecontrol/` β feature flags wired through `--enable-feature`.
- `limit/`, `httpserver/`, `tracing/` β server plumbing.
- `ui/web.go` β embeds the built UI assets and serves them.
- `examples/`, `doc/examples/` β example configs and HA setups.
- `test/with_api_v2/` and `test/cli/` β integration/e2e tests.
Generated code (do not handβedit):
- `api/v2/{models,restapi,client}/` β regenerated by `scripts/swagger.sh` from `api/v2/openapi.yaml`.
- `ui/app/src/Data/` β Elm types generated from the OpenAPI spec.
- `template/email.tmpl` β generated from `template/email.html` via `template/Makefile`.
- `cluster/clusterpb/cluster.pb.go`, `nflog/nflogpb/nflog.pb.go`, `silence/silencepb/silence.pb.go` β regenerate via `scripts/genproto.sh` if the `.proto` schema must change.
## Common commands
Builds and tests are driven by the shared `Makefile.common` plus the repoβspecific `Makefile`.
- `make build` β build `alertmanager` and `amtool` (also builds Elm UI assets).
- `make build-all` β UI assets + regenerated API + binaries.
- `make build BINARIES=amtool` β build a single binary.
- `make assets` β regenerate Elm `Data` types and `template/email.tmpl`.
- `make apiv2` β regenerate `api/v2/{models,restapi,client}` from `api/v2/openapi.yaml` (runs `scripts/swagger.sh`; needs Docker).
- `make test` β full Go test suite (`go test ./...`) plus UI tests.
- `make test-short` (via `Makefile.common`) β `go test -short ./...`.
- `make lint` β `golangci-lint run` (version pinned in `Makefile.common`) plus UI lint.
- `make common-format` β `go fmt` + `golangci-lint fmt` (gofumpt + goimports with local prefix `github.com/prometheus/alertmanager`).
- `make fuzz-config` β short fuzz run over `./config`.
- `make clean` β remove generated API code, generated email template, and UI build output.
Run a single Go test package or test:
```
go test ./notify/slack/...
go test ./dispatch -run TestDispatcherRace -v
```
Run the UI dev servers directly when iterating on the frontend:
```
# Legacy Elm UI
cd ui/app && make build # see ui/app/CONTRIBUTING.md
# New Mantine UI
cd ui/mantine-ui && npm install && npm run dev
```
Run Alertmanager locally:
```
./alertmanager --config.file=doc/examples/simple.yml
# multi-node HA via Procfile (requires goreman):
goreman start
```
## Conventions
- Follow the lint rules in `.golangci.yml`. Notable forbidden imports (enforced by `depguard`):
- Use `github.com/stretchr/testify/require`, not `.../testify/assert`.
- Use `github.com/go-kit/log`, not `github.com/go-kit/kit/log`.
- Use the standard `errors`/`fmt`, not `github.com/pkg/errors`.
- Formatting: `gofumpt` + `goimports` with local prefix `github.com/prometheus/alertmanager`. Run `make common-format` before committing.
- Comments on exported identifiers must be full sentences ending in a period (`godot` lint).
- Logging uses `log/slog` (`sloglint`). Prefer structured key/value logging.
- Errors: wrap with `fmt.Errorf("...: %w", err)` and check with `errors.Is`/`errors.As` (`errorlint`).
- Keep packageβlevel documentation up to date (`revive: package-comments`).
- Tests live next to the code as `*_test.go`. Larger integration tests live under `test/`. The `notify/test` package provides shared testing helpers for notifier integrations.
## When changing the API
1. Edit `api/v2/openapi.yaml`.
2. Run `make apiv2` to regenerate `api/v2/{models,restapi,client}` (requires Docker; see `scripts/swagger.sh`).
3. Run `make assets` so the Elm UI's `Data` types stay in sync.
4. Update `api/v2/api.go` handlers for any new operations.
5. Run `make test lint`.
Do not handβedit generated files under `api/v2/models`, `api/v2/restapi`, `api/v2/client`, or `ui/app/src/Data`.
## When adding or modifying a notifier
- Each integration lives in its own package under `notify/<name>/`.
- Add config in `config/notifiers.go` (struct, validation, defaults) and wire it into `config/config.go` receivers.
- Register the notifier in `cmd/alertmanager/main.go` where receivers are built.
- Add unit tests in the notifier package; reuse helpers from `notify/test/`.
- Update `template/default.tmpl` only if you are introducing new default templates.
- Note the change in `CHANGELOG.md` under the unreleased section.
## When touching configuration
- `config/config.go` owns topβlevel YAML parsing; field changes typically need:
- Struct + `yaml` tags.
- `UnmarshalYAML` validation.
- Defaults in `DefaultGlobalConfig` / receiver defaults.
- Tests in `config/config_test.go`, possibly `testdata/` fixtures.
- Validate behaviour with `make fuzz-config` for parser changes.
## CI expectations
GitHub Actions and CircleCI run build, tests, and `golangci-lint`. Before pushing:
```
make common-format
make lint
make test
```
If you regenerate API code or UI data, commit the regenerated files alongside the source changes.
## Things to avoid
- Don't reintroduce APIv1; it was removed in 0.27.0. The only thing left under `api/v1_deprecation_router.go` is a deprecation responder.
- Don't bypass the notification pipeline (`notify/notify.go`) β new stages should be added as `notify.Stage` implementations.
- Don't add new topβlevel dependencies without checking `.golangci.yml` `depguard` rules and `go.mod` minimums.
- Don't commit generated artifacts unless the corresponding source (`openapi.yaml`, `email.html`, etc.) changed in the same commit.
## Useful references
- Architecture diagram: `doc/arch.svg` (source `doc/arch.xml`).
- Example configs: `doc/examples/simple.yml`, `examples/ha/`, `examples/webhook/`.
- Maintainers and ownership: `MAINTAINERS.md`, `CODEOWNERS`.
- Release process: `RELEASE.md`.
- UI contributing guide: `ui/app/CONTRIBUTING.md`.