grafana/pkg/services/provisioning/alerting/provisioner.go
Sofia Papagiannaki 17ca61d7f8
Alerting: Export and provisioning rules into subfolders (#77450)
* Folders: Optionally include fullpath in service responses
* Alerting: Export folder fullpath instead of title
* Escape separator in folder title
* Add support for provisiong alret rules into subfolders
* Use FolderService for creating folders during provisioning
* Export WithFullpath() folder service function

---------

Co-authored-by: Tania B <yalyna.ts@gmail.com>
Co-authored-by: Yuri Tseretyan <yuriy.tseretyan@grafana.com>
2024-05-31 11:09:20 +03:00

81 lines
2.6 KiB
Go

package alerting
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
)
type ProvisionerConfig struct {
Path string
FolderService folder.Service
DashboardProvService dashboards.DashboardProvisioningService
RuleService provisioning.AlertRuleService
ContactPointService provisioning.ContactPointService
NotificiationPolicyService provisioning.NotificationPolicyService
MuteTimingService provisioning.MuteTimingService
TemplateService provisioning.TemplateService
}
func Provision(ctx context.Context, cfg ProvisionerConfig) error {
logger := log.New("provisioning.alerting")
cfgReader := newRulesConfigReader(logger)
files, err := cfgReader.readConfig(ctx, cfg.Path)
if err != nil {
return err
}
logger.Info("starting to provision alerting")
logger.Debug("read all alerting files", "file_count", len(files))
cpProvisioner := NewContactPointProvisoner(logger, cfg.ContactPointService)
err = cpProvisioner.Provision(ctx, files)
if err != nil {
return fmt.Errorf("contact points: %w", err)
}
mtProvisioner := NewMuteTimesProvisioner(logger, cfg.MuteTimingService)
err = mtProvisioner.Provision(ctx, files)
if err != nil {
return fmt.Errorf("mute times: %w", err)
}
ttProvsioner := NewTextTemplateProvisioner(logger, cfg.TemplateService)
err = ttProvsioner.Provision(ctx, files)
if err != nil {
return fmt.Errorf("text templates: %w", err)
}
npProvisioner := NewNotificationPolicyProvisoner(logger, cfg.NotificiationPolicyService)
err = npProvisioner.Provision(ctx, files)
if err != nil {
return fmt.Errorf("notification policies: %w", err)
}
err = npProvisioner.Unprovision(ctx, files)
if err != nil {
return fmt.Errorf("notification policies: %w", err)
}
err = mtProvisioner.Unprovision(ctx, files)
if err != nil {
return fmt.Errorf("mute times: %w", err)
}
err = ttProvsioner.Unprovision(ctx, files)
if err != nil {
return fmt.Errorf("text templates: %w", err)
}
ruleProvisioner := NewAlertRuleProvisioner(
logger,
cfg.FolderService,
cfg.DashboardProvService,
cfg.RuleService)
err = ruleProvisioner.Provision(ctx, files)
if err != nil {
return fmt.Errorf("alert rules: %w", err)
}
err = cpProvisioner.Unprovision(ctx, files) // Unprovision contact points after rules to make sure all references in rules are updated
if err != nil {
return fmt.Errorf("contact points: %w", err)
}
logger.Info("finished to provision alerting")
return nil
}