diff --git a/pkg/services/ngalert/notifier/alertmanager.go b/pkg/services/ngalert/notifier/alertmanager.go index 8a3090e25e5..cb9df23ee88 100644 --- a/pkg/services/ngalert/notifier/alertmanager.go +++ b/pkg/services/ngalert/notifier/alertmanager.go @@ -125,7 +125,7 @@ func newAlertmanager(ctx context.Context, orgID int64, cfg *setting.Cfg, store A Nflog: nflogOptions, } - l := log.New("alertmanager", "org", orgID) + l := log.New("ngalert.notifier.alertmanager", orgID) gam, err := alertingNotify.NewGrafanaAlertmanager("orgID", orgID, amcfg, peer, l, alertingNotify.NewGrafanaAlertmanagerMetrics(m.Registerer)) if err != nil { return nil, err @@ -263,14 +263,14 @@ func (am *Alertmanager) applyConfig(cfg *apimodels.PostableUserConfig, rawConfig cfg.AlertmanagerConfig.Templates = append(cfg.AlertmanagerConfig.Templates, "__default__.tmpl") // next, we need to make sure we persist the templates to disk. - _, templatesChanged, err := PersistTemplates(cfg, am.Base.WorkingDirectory()) + _, templatesChanged, err := PersistTemplates(am.logger, cfg, am.Base.WorkingDirectory()) if err != nil { return false, err } // If neither the configuration nor templates have changed, we've got nothing to do. if !amConfigChanged && !templatesChanged { - am.logger.Debug("neither config nor template have changed, skipping configuration sync.") + am.logger.Debug("Neither config nor template have changed, skipping configuration sync.") return false, nil } diff --git a/pkg/services/ngalert/notifier/config.go b/pkg/services/ngalert/notifier/config.go index 10fb03e47ce..c1e97bb8942 100644 --- a/pkg/services/ngalert/notifier/config.go +++ b/pkg/services/ngalert/notifier/config.go @@ -14,9 +14,7 @@ import ( api "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" ) -var cfglogger = log.New("notifier.config") - -func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool, error) { +func PersistTemplates(logger log.Logger, cfg *api.PostableUserConfig, path string) ([]string, bool, error) { if len(cfg.TemplateFiles) < 1 { return nil, false, nil } @@ -58,7 +56,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool, // Now that we have the list of _actual_ templates, let's remove the ones that we don't need. existingFiles, err := os.ReadDir(path) if err != nil { - cfglogger.Error("unable to read directory for deleting Alertmanager templates", "error", err, "path", path) + logger.Error("Unable to read directory for deleting Alertmanager templates", "error", err, "path", path) } for _, existingFile := range existingFiles { p := filepath.Join(path, existingFile.Name()) @@ -67,7 +65,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool, templatesChanged = true err := os.Remove(p) if err != nil { - cfglogger.Error("unable to delete template", "error", err, "file", p) + logger.Error("Unable to delete template", "error", err, "file", p) } } } diff --git a/pkg/services/ngalert/notifier/config_test.go b/pkg/services/ngalert/notifier/config_test.go index 5650ffb3471..b11140b9b53 100644 --- a/pkg/services/ngalert/notifier/config_test.go +++ b/pkg/services/ngalert/notifier/config_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/grafana/grafana/pkg/infra/log/logtest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -77,7 +78,8 @@ func TestPersistTemplates(t *testing.T) { } c := &api.PostableUserConfig{TemplateFiles: tt.templates} - paths, changed, persistErr := PersistTemplates(c, dir) + testLogger := logtest.Fake{} + paths, changed, persistErr := PersistTemplates(&testLogger, c, dir) files := map[string]string{} readFiles, err := os.ReadDir(dir) diff --git a/pkg/services/ngalert/notifier/crypto.go b/pkg/services/ngalert/notifier/crypto.go index a3ebf0340cd..b0861d205e7 100644 --- a/pkg/services/ngalert/notifier/crypto.go +++ b/pkg/services/ngalert/notifier/crypto.go @@ -53,7 +53,7 @@ func (c *alertmanagerCrypto) LoadSecureSettings(ctx context.Context, orgId int64 currentConfig, err := Load([]byte(amConfig.AlertmanagerConfiguration)) // If the current config is un-loadable, treat it as if it never existed. Providing a new, valid config should be able to "fix" this state. if err != nil { - c.log.Warn("last known alertmanager configuration was invalid. Overwriting...") + c.log.Warn("Last known alertmanager configuration was invalid. Overwriting...") } else { currentReceiverMap = currentConfig.GetGrafanaReceiverMap() } diff --git a/pkg/services/ngalert/notifier/file_store.go b/pkg/services/ngalert/notifier/file_store.go index 3f4c36820aa..cba28381452 100644 --- a/pkg/services/ngalert/notifier/file_store.go +++ b/pkg/services/ngalert/notifier/file_store.go @@ -29,7 +29,7 @@ func NewFileStore(orgID int64, store kvstore.KVStore, workingDirPath string) *Fi workingDirPath: workingDirPath, orgID: orgID, kv: kvstore.WithNamespace(store, orgID, KVNamespace), - logger: log.New("filestore", "org", orgID), + logger: log.New("ngalert.notifier.alertmanager.file_store", orgID), } } @@ -92,11 +92,11 @@ func (fileStore *FileStore) WriteFileToDisk(fn string, content []byte) error { // CleanUp will remove the working directory from disk. func (fileStore *FileStore) CleanUp() { if err := os.RemoveAll(fileStore.workingDirPath); err != nil { - fileStore.logger.Warn("unable to delete the local working directory", "dir", fileStore.workingDirPath, + fileStore.logger.Warn("Unable to delete the local working directory", "dir", fileStore.workingDirPath, "error", err) return } - fileStore.logger.Info("successfully deleted working directory", "dir", fileStore.workingDirPath) + fileStore.logger.Info("Successfully deleted working directory", "dir", fileStore.workingDirPath) } func (fileStore *FileStore) pathFor(fn string) string { diff --git a/pkg/services/ngalert/notifier/multiorg_alertmanager.go b/pkg/services/ngalert/notifier/multiorg_alertmanager.go index c2c41b854a0..b7afa5aeee4 100644 --- a/pkg/services/ngalert/notifier/multiorg_alertmanager.go +++ b/pkg/services/ngalert/notifier/multiorg_alertmanager.go @@ -129,7 +129,7 @@ func (moa *MultiOrgAlertmanager) setupClustering(cfg *setting.Cfg) error { err = peer.Join(cluster.DefaultReconnectInterval, cluster.DefaultReconnectTimeout) if err != nil { - moa.logger.Error("msg", "unable to join gossip mesh while initializing cluster for high availability mode", "error", err) + moa.logger.Error("msg", "Unable to join gossip mesh while initializing cluster for high availability mode", "error", err) } // Attempt to verify the number of peers for 30s every 2s. The risk here is what we send a notification "too soon". // Which should _never_ happen given we share the notification log via the database so the risk of double notification is very low. @@ -143,7 +143,7 @@ func (moa *MultiOrgAlertmanager) setupClustering(cfg *setting.Cfg) error { } func (moa *MultiOrgAlertmanager) Run(ctx context.Context) error { - moa.logger.Info("starting MultiOrg Alertmanager") + moa.logger.Info("Starting MultiOrg Alertmanager") for { select {