Files
grafana/pkg/services/ngalert/prom/models.go
T
Alexander Akhmetov b641fd64f9 Alerting: API to create rule groups using mimirtool (#100558)
What is this feature?

Adds an API endpoint to create alert rules with mimirtool:

- POST /convert/prometheus/config/v1/rules/{NamespaceTitle} - Accepts a single rule group in a Prometheus YAML format and creates or updates a Grafana rule group from it.

The endpoint uses the conversion package from #100224.

Key parts

The API works similarly to the provisioning API. If the rule does not exist, it will be created, otherwise updated. Any rules not present in the new group will be deleted, ensuring the group is fully synchronized with the provided configuration.

Since the API works with namespace titles (folders), the handler automatically creates a folder in the root based on the provided title if it does not exist. It also requires a special header, X-Grafana-Alerting-Datasource-UID. This header specifies which datasource to use for the new rules.

If the rule group's evaluation interval is not specified, it uses the DefaultRuleEvaluationInterval from settings.
2025-02-25 11:26:36 +01:00

40 lines
1.1 KiB
Go

package prom
import (
prommodel "github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/apimachinery/errutil"
)
var (
ErrPrometheusRuleValidationFailed = errutil.ValidationFailed("alerting.prometheusRuleInvalid")
)
type PrometheusRulesFile struct {
Groups []PrometheusRuleGroup `yaml:"groups"`
}
type PrometheusRuleGroup struct {
Name string `yaml:"name"`
Interval prommodel.Duration `yaml:"interval"`
Rules []PrometheusRule `yaml:"rules"`
}
type PrometheusRule struct {
Alert string `yaml:"alert,omitempty"`
Expr string `yaml:"expr,omitempty"`
For *prommodel.Duration `yaml:"for,omitempty"`
KeepFiringFor *prommodel.Duration `yaml:"keep_firing_for,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Annotations map[string]string `yaml:"annotations,omitempty"`
Record string `yaml:"record,omitempty"`
}
func (r *PrometheusRule) Validate() error {
if r.KeepFiringFor != nil {
return ErrPrometheusRuleValidationFailed.Errorf("keep_firing_for is not supported")
}
return nil
}