Files
grafana/pkg/services/ngalert/state/historian/backend.go
T
Alexander Akhmetov ad683f83ff Alerting: Add state history backend to write ALERTS metric (#104361)
**What is this feature?**

This PR implements a new Prometheus historian backend that allows Grafana alerting to write alert state history as Prometheus-compatible `ALERTS` metrics to remote Prometheus-compatible data sources.

The metric includes a few additional labels:

* `grafana_alertstate`: Grafana's full alert state, more granular than Prometheus.
* `grafana_rule_uid`: Grafana's alert rule UID.

Grafana states are included in the `grafana_alertstate` label also mapped to Prometheus-compatible `alertstate` values:

| Grafana alert state | `alertstate`          | `grafana_alertstate`  |
|---------------------|-----------------------|-----------------------|
| `Alerting`          | `firing`              | `alerting`            |
| `Recovering`        | `firing`              | `recovering`          |
| `Pending`           | `pending`             | `pending`             |
| `Error`             | `firing`              | `error`               |
| `NoData`            | `firing`              | `nodata`              |
| `Normal`            | _(no metric emitted)_ | _(no metric emitted)_ |
2025-06-18 07:17:57 +02:00

40 lines
939 B
Go

package historian
import (
"fmt"
"strings"
)
// BackendType identifies different kinds of state history backends.
type BackendType string
// String implements Stringer for BackendType.
func (bt BackendType) String() string {
return string(bt)
}
const (
BackendTypeAnnotations BackendType = "annotations"
BackendTypeLoki BackendType = "loki"
BackendTypeMultiple BackendType = "multiple"
BackendTypePrometheus BackendType = "prometheus"
BackendTypeNoop BackendType = "noop"
)
func ParseBackendType(s string) (BackendType, error) {
norm := strings.ToLower(strings.TrimSpace(s))
types := map[BackendType]struct{}{
BackendTypeAnnotations: {},
BackendTypeLoki: {},
BackendTypeMultiple: {},
BackendTypePrometheus: {},
BackendTypeNoop: {},
}
p := BackendType(norm)
if _, ok := types[p]; !ok {
return "", fmt.Errorf("unrecognized state history backend: %s", p)
}
return p, nil
}