mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: In migration, create one label per channel (#76527)
* In migration, create one label per channel This PR changes how routing is done by the legacy alerting migration. Previously, we created a single label on each alert rule that contained an array of contact point names. Ex: __contact__="slack legacy testing","slack legacy testing2" This label was then routed against a series of regex-matching policies with continue=true. Ex: __contacts__ =~ .*"slack legacy testing".* In the case of many contact points, this array could quickly become difficult to manage and difficult to grok at-a-glance. This PR replaces the single __contact__ label with multiple __legacy_c_{contactname}__ labels and simple equality-matching policies. These channel-specific policies are nested in a single route under the top-level route which matches against __legacy_use_channels__ = true for ease of organization. This should improve the experience for users wanting to keep the default migrated routing strategy but who also want to modify which contact points an alert sends to.
This commit is contained in:
79
pkg/services/ngalert/migration/models/alertmanager.go
Normal file
79
pkg/services/ngalert/migration/models/alertmanager.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/prometheus/alertmanager/config"
|
||||
"github.com/prometheus/alertmanager/pkg/labels"
|
||||
"github.com/prometheus/common/model"
|
||||
|
||||
apiModels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
)
|
||||
|
||||
// Alertmanager is a helper struct for creating migrated alertmanager configs.
|
||||
type Alertmanager struct {
|
||||
Config *apiModels.PostableUserConfig
|
||||
legacyRoute *apiModels.Route
|
||||
}
|
||||
|
||||
// NewAlertmanager creates a new Alertmanager.
|
||||
func NewAlertmanager() *Alertmanager {
|
||||
c, r := createBaseConfig()
|
||||
return &Alertmanager{
|
||||
Config: c,
|
||||
legacyRoute: r,
|
||||
}
|
||||
}
|
||||
|
||||
// AddRoute adds a route to the alertmanager config.
|
||||
func (am *Alertmanager) AddRoute(route *apiModels.Route) {
|
||||
am.legacyRoute.Routes = append(am.legacyRoute.Routes, route)
|
||||
}
|
||||
|
||||
// AddReceiver adds a receiver to the alertmanager config.
|
||||
func (am *Alertmanager) AddReceiver(recv *apiModels.PostableApiReceiver) {
|
||||
am.Config.AlertmanagerConfig.Receivers = append(am.Config.AlertmanagerConfig.Receivers, recv)
|
||||
}
|
||||
|
||||
// createBaseConfig creates an alertmanager config with the root-level route, default receiver, and nested route
|
||||
// for migrated channels.
|
||||
func createBaseConfig() (*apiModels.PostableUserConfig, *apiModels.Route) {
|
||||
defaultRoute, nestedRoute := createDefaultRoute()
|
||||
return &apiModels.PostableUserConfig{
|
||||
AlertmanagerConfig: apiModels.PostableApiAlertingConfig{
|
||||
Receivers: []*apiModels.PostableApiReceiver{
|
||||
{
|
||||
Receiver: config.Receiver{
|
||||
Name: "autogen-contact-point-default",
|
||||
},
|
||||
PostableGrafanaReceivers: apiModels.PostableGrafanaReceivers{
|
||||
GrafanaManagedReceivers: []*apiModels.PostableGrafanaReceiver{},
|
||||
},
|
||||
},
|
||||
},
|
||||
Config: apiModels.Config{
|
||||
Route: defaultRoute,
|
||||
},
|
||||
},
|
||||
}, nestedRoute
|
||||
}
|
||||
|
||||
// createDefaultRoute creates a default root-level route and associated nested route that will contain all the migrated channels.
|
||||
func createDefaultRoute() (*apiModels.Route, *apiModels.Route) {
|
||||
nestedRoute := createNestedLegacyRoute()
|
||||
return &apiModels.Route{
|
||||
Receiver: "autogen-contact-point-default",
|
||||
Routes: []*apiModels.Route{nestedRoute},
|
||||
GroupByStr: []string{ngmodels.FolderTitleLabel, model.AlertNameLabel}, // To keep parity with pre-migration notifications.
|
||||
RepeatInterval: nil,
|
||||
}, nestedRoute
|
||||
}
|
||||
|
||||
// createNestedLegacyRoute creates a nested route that will contain all the migrated channels.
|
||||
// This route is matched on the UseLegacyChannelsLabel and mostly exists to keep the migrated channels separate and organized.
|
||||
func createNestedLegacyRoute() *apiModels.Route {
|
||||
mat, _ := labels.NewMatcher(labels.MatchEqual, ngmodels.MigratedUseLegacyChannelsLabel, "true")
|
||||
return &apiModels.Route{
|
||||
ObjectMatchers: apiModels.ObjectMatchers{mat},
|
||||
Continue: true,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user