mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* 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.
91 lines
3.2 KiB
Go
91 lines
3.2 KiB
Go
package migration
|
|
|
|
import (
|
|
pb "github.com/prometheus/alertmanager/silence/silencepb"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
legacymodels "github.com/grafana/grafana/pkg/services/alerting/models"
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
|
migmodels "github.com/grafana/grafana/pkg/services/ngalert/migration/models"
|
|
migrationStore "github.com/grafana/grafana/pkg/services/ngalert/migration/store"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
|
"github.com/grafana/grafana/pkg/services/secrets"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
// OrgMigration is a helper struct for migrating alerts for a single org. It contains state, services, and caches.
|
|
type OrgMigration struct {
|
|
cfg *setting.Cfg
|
|
log log.Logger
|
|
|
|
migrationStore migrationStore.Store
|
|
encryptionService secrets.Service
|
|
|
|
orgID int64
|
|
silences []*pb.MeshSilence
|
|
titleDeduplicatorForFolder func(folderUID string) *migmodels.Deduplicator
|
|
channelCache *ChannelCache
|
|
|
|
// Migrated folder for a dashboard based on permissions. Parent Folder ID -> unique dashboard permission -> custom folder.
|
|
permissionsMap map[int64]map[permissionHash]*folder.Folder
|
|
folderCache map[int64]*folder.Folder // Folder ID -> Folder.
|
|
folderPermissionCache map[string][]accesscontrol.ResourcePermission // Folder UID -> Folder Permissions.
|
|
generalAlertingFolder *folder.Folder
|
|
|
|
state *migmodels.OrgMigrationState
|
|
}
|
|
|
|
// newOrgMigration creates a new OrgMigration for the given orgID.
|
|
func (ms *migrationService) newOrgMigration(orgID int64) *OrgMigration {
|
|
titlededuplicatorPerFolder := make(map[string]*migmodels.Deduplicator)
|
|
return &OrgMigration{
|
|
cfg: ms.cfg,
|
|
log: ms.log.New("orgID", orgID),
|
|
|
|
migrationStore: ms.migrationStore,
|
|
encryptionService: ms.encryptionService,
|
|
|
|
orgID: orgID,
|
|
silences: make([]*pb.MeshSilence, 0),
|
|
titleDeduplicatorForFolder: func(folderUID string) *migmodels.Deduplicator {
|
|
if _, ok := titlededuplicatorPerFolder[folderUID]; !ok {
|
|
titlededuplicatorPerFolder[folderUID] = migmodels.NewDeduplicator(ms.migrationStore.CaseInsensitive(), store.AlertDefinitionMaxTitleLength)
|
|
}
|
|
return titlededuplicatorPerFolder[folderUID]
|
|
},
|
|
channelCache: &ChannelCache{cache: make(map[any]*legacymodels.AlertNotification)},
|
|
|
|
permissionsMap: make(map[int64]map[permissionHash]*folder.Folder),
|
|
folderCache: make(map[int64]*folder.Folder),
|
|
folderPermissionCache: make(map[string][]accesscontrol.ResourcePermission),
|
|
|
|
state: &migmodels.OrgMigrationState{
|
|
OrgID: orgID,
|
|
CreatedFolders: make([]string, 0),
|
|
},
|
|
}
|
|
}
|
|
|
|
// ChannelCache caches channels by ID and UID.
|
|
type ChannelCache struct {
|
|
cache map[any]*legacymodels.AlertNotification
|
|
}
|
|
|
|
func (c *ChannelCache) LoadChannels(channels []*legacymodels.AlertNotification) {
|
|
for _, channel := range channels {
|
|
c.cache[channel.ID] = channel
|
|
c.cache[channel.UID] = channel
|
|
}
|
|
}
|
|
|
|
func (c *ChannelCache) GetChannelByID(id int64) (*legacymodels.AlertNotification, bool) {
|
|
channel, ok := c.cache[id]
|
|
return channel, ok
|
|
}
|
|
|
|
func (c *ChannelCache) GetChannelByUID(uid string) (*legacymodels.AlertNotification, bool) {
|
|
channel, ok := c.cache[uid]
|
|
return channel, ok
|
|
}
|