mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 09:34:27 +08:00

API Changes: - Fixes validation in template CRUD API to be closer to how the running alertmanager will use the template. Should remove some incorrect validation errors. - Adds some missing default placeholder labels to receiver testing that are used during template testing but missing during receiver testing Template Preview: - Replaced basic preview with a readonly CodeEditor for better whitespace and alignment clarity (also adds support for future syntax highlighting in template previews for upcoming webhook payload templates) Template Selector (Receiver Form): - Refactored to use same components as Template editor for preview. - Fixed preview to work with multi-definition templates - Fixed copy to correctly copy the template contents instead of {{ template "<name>" . }}. Template Editor: - Fixed detection of when to display functions vs snippets in multi-line expressions
64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package notifier
|
|
|
|
import (
|
|
"context"
|
|
|
|
alertingModels "github.com/grafana/alerting/models"
|
|
alertingNotify "github.com/grafana/alerting/notify"
|
|
amv2 "github.com/prometheus/alertmanager/api/v2/models"
|
|
prometheusModel "github.com/prometheus/common/model"
|
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
)
|
|
|
|
type TestTemplatesResults = alertingNotify.TestTemplatesResults
|
|
|
|
var (
|
|
DefaultLabels = map[string]string{
|
|
prometheusModel.AlertNameLabel: `TestAlert`,
|
|
alertingModels.FolderTitleLabel: `Test Folder`,
|
|
}
|
|
DefaultAnnotations = map[string]string{
|
|
alertingModels.ValuesAnnotation: `{"B":22,"C":1}`,
|
|
alertingModels.ValueStringAnnotation: `[ var='B' labels={__name__=go_threads, instance=host.docker.internal:3000, job=grafana} value=22 ], [ var='C' labels={__name__=go_threads, instance=host.docker.internal:3000, job=grafana} value=1 ]`,
|
|
alertingModels.OrgIDAnnotation: `1`,
|
|
alertingModels.DashboardUIDAnnotation: `dashboard_uid`,
|
|
alertingModels.PanelIDAnnotation: `1`,
|
|
}
|
|
)
|
|
|
|
// TestTemplate tests the given template string against the given alerts. Existing templates are used to provide context for the test.
|
|
// If an existing template of the same filename as the one being tested is found, it will not be used as context.
|
|
func (am *alertmanager) TestTemplate(ctx context.Context, c apimodels.TestTemplatesConfigBodyParams) (*TestTemplatesResults, error) {
|
|
for _, alert := range c.Alerts {
|
|
AddDefaultLabelsAndAnnotations(alert)
|
|
}
|
|
|
|
return am.Base.TestTemplate(ctx, alertingNotify.TestTemplatesConfigBodyParams{
|
|
Alerts: c.Alerts,
|
|
Template: c.Template,
|
|
Name: c.Name,
|
|
})
|
|
}
|
|
|
|
// AddDefaultLabelsAndAnnotations is a slimmed down version of state.StateToPostableAlert and state.GetRuleExtraLabels using default values.
|
|
func AddDefaultLabelsAndAnnotations(alert *amv2.PostableAlert) {
|
|
if alert.Labels == nil {
|
|
alert.Labels = make(map[string]string)
|
|
}
|
|
for k, v := range DefaultLabels {
|
|
if _, ok := alert.Labels[k]; !ok {
|
|
alert.Labels[k] = v
|
|
}
|
|
}
|
|
|
|
if alert.Annotations == nil {
|
|
alert.Annotations = make(map[string]string)
|
|
}
|
|
for k, v := range DefaultAnnotations {
|
|
if _, ok := alert.Annotations[k]; !ok {
|
|
alert.Annotations[k] = v
|
|
}
|
|
}
|
|
}
|