From d019b4ff1bfc915d058551e992050ffd869bbde5 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Wed, 21 May 2025 10:13:18 -0400 Subject: [PATCH] Alerting: Change logging in Alertmanager (#105704) * change logger to ngalert.notifier and use component label * update alerting module --------- Signed-off-by: Yuri Tseretyan --- go.mod | 2 +- go.sum | 4 ++-- pkg/services/ngalert/notifier/alertmanager.go | 7 +++--- pkg/services/ngalert/notifier/email_test.go | 4 ++-- pkg/services/ngalert/notifier/images.go | 2 +- pkg/services/ngalert/notifier/log.go | 23 ------------------- 6 files changed, 9 insertions(+), 33 deletions(-) delete mode 100644 pkg/services/ngalert/notifier/log.go diff --git a/go.mod b/go.mod index 0749adfee23..a4dc55e3fe0 100644 --- a/go.mod +++ b/go.mod @@ -78,7 +78,7 @@ require ( github.com/googleapis/go-sql-spanner v1.11.1 // @grafana/grafana-search-and-storage github.com/gorilla/mux v1.8.1 // @grafana/grafana-backend-group github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // @grafana/grafana-app-platform-squad - github.com/grafana/alerting v0.0.0-20250519195027-0ce55e7a1d00 // @grafana/alerting-backend + github.com/grafana/alerting v0.0.0-20250521131632-6e476b0b04c3 // @grafana/alerting-backend github.com/grafana/authlib v0.0.0-20250515162837-2f4a8263eabb // @grafana/identity-access-team github.com/grafana/authlib/types v0.0.0-20250325095148-d6da9c164a7d // @grafana/identity-access-team github.com/grafana/dataplane/examples v0.0.1 // @grafana/observability-metrics diff --git a/go.sum b/go.sum index ee6f37625e5..b73237ae465 100644 --- a/go.sum +++ b/go.sum @@ -1569,8 +1569,8 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo= github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA= -github.com/grafana/alerting v0.0.0-20250519195027-0ce55e7a1d00 h1:i0IpxkT1bX96F5D/oIxDdXLu+Nz/pU4rHlmnswpVLF0= -github.com/grafana/alerting v0.0.0-20250519195027-0ce55e7a1d00/go.mod h1:pMfhRxL2LZ3Pm8iy7VcVsb9CLYuBtjFYbf1oxgx7yFA= +github.com/grafana/alerting v0.0.0-20250521131632-6e476b0b04c3 h1:QgV6JOePZeVOqIu0yVrjuryctEpVBvlPI04Zq25PV3c= +github.com/grafana/alerting v0.0.0-20250521131632-6e476b0b04c3/go.mod h1:pMfhRxL2LZ3Pm8iy7VcVsb9CLYuBtjFYbf1oxgx7yFA= github.com/grafana/authlib v0.0.0-20250515162837-2f4a8263eabb h1:oTl2j6/4miQUYmXANp2pBuYCWA5f8NVYFfCWpczpFso= github.com/grafana/authlib v0.0.0-20250515162837-2f4a8263eabb/go.mod h1:PBtQaXwkFu4BAt2aXsR7w8p8NVpdjV5aJYhqRDei9Us= github.com/grafana/authlib/types v0.0.0-20250325095148-d6da9c164a7d h1:34E6btDAhdDOiSEyrMaYaHwnJpM8w9QKzVQZIBzLNmM= diff --git a/pkg/services/ngalert/notifier/alertmanager.go b/pkg/services/ngalert/notifier/alertmanager.go index 85666eb353f..aa570fec558 100644 --- a/pkg/services/ngalert/notifier/alertmanager.go +++ b/pkg/services/ngalert/notifier/alertmanager.go @@ -114,7 +114,7 @@ func NewAlertmanager(ctx context.Context, orgID int64, cfg *setting.Cfg, store A return stateStore.SaveNotificationLog(context.Background(), state) }, } - l := log.New("ngalert.notifier.alertmanager", "org", orgID) + l := log.New("ngalert.notifier") opts := alertingNotify.GrafanaAlertmanagerOpts{ ExternalURL: cfg.AppURL, @@ -127,9 +127,8 @@ func NewAlertmanager(ctx context.Context, orgID int64, cfg *setting.Cfg, store A MaxSilenceSizeBytes: cfg.UnifiedAlerting.AlertmanagerMaxSilenceSizeBytes, }, EmailSender: &emailSender{ns}, - ImageProvider: newImageProvider(store, log.New("ngalert.notifier.image-provider")), + ImageProvider: newImageProvider(store, l.New("component", "image-provider")), Decrypter: decryptFn, - LoggerFactory: LoggerFactory, Version: setting.BuildVersion, TenantKey: "orgID", TenantID: orgID, @@ -149,7 +148,7 @@ func NewAlertmanager(ctx context.Context, orgID int64, cfg *setting.Cfg, store A DefaultConfiguration: cfg.UnifiedAlerting.DefaultConfiguration, Store: store, stateStore: stateStore, - logger: l, + logger: l.New("component", "alertmanager", opts.TenantKey, opts.TenantID), // similar to what the base does } return am, nil diff --git a/pkg/services/ngalert/notifier/email_test.go b/pkg/services/ngalert/notifier/email_test.go index 5ef09d5fae9..ef2b6159e9b 100644 --- a/pkg/services/ngalert/notifier/email_test.go +++ b/pkg/services/ngalert/notifier/email_test.go @@ -7,7 +7,6 @@ import ( "testing" alertingImages "github.com/grafana/alerting/images" - alertingLogging "github.com/grafana/alerting/logging" "github.com/grafana/alerting/receivers" alertingEmail "github.com/grafana/alerting/receivers/email" alertingTemplates "github.com/grafana/alerting/templates" @@ -17,6 +16,7 @@ import ( "github.com/stretchr/testify/require" "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/infra/log/logtest" "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/services/notifications" "github.com/grafana/grafana/pkg/setting" @@ -200,7 +200,7 @@ func createSut(t *testing.T, messageTmpl string, subjectTmpl string, emailTmpl * }, Message: messageTmpl, Subject: subjectTmpl, - }, receivers.Metadata{}, emailTmpl, ns, &alertingImages.UnavailableProvider{}, &alertingLogging.FakeLogger{}) + }, receivers.Metadata{}, emailTmpl, ns, &alertingImages.UnavailableProvider{}, &logtest.Fake{}) } func getSingleSentMessage(t *testing.T, ns *emailSender) *notifications.Message { diff --git a/pkg/services/ngalert/notifier/images.go b/pkg/services/ngalert/notifier/images.go index 40411584e3a..829dbfd4d90 100644 --- a/pkg/services/ngalert/notifier/images.go +++ b/pkg/services/ngalert/notifier/images.go @@ -25,7 +25,7 @@ func newImageProvider(store store.ImageStore, logger log.Logger) alertingImages. return alertingImages.NewTokenProvider(&tokenStore{ store: store, logger: logger, - }, newLogWrapper(logger)) + }, logger) } func (t tokenStore) GetImage(ctx context.Context, token string) (*alertingImages.Image, error) { diff --git a/pkg/services/ngalert/notifier/log.go b/pkg/services/ngalert/notifier/log.go deleted file mode 100644 index 4aef94c49ed..00000000000 --- a/pkg/services/ngalert/notifier/log.go +++ /dev/null @@ -1,23 +0,0 @@ -package notifier - -import ( - alertingLogging "github.com/grafana/alerting/logging" - - "github.com/grafana/grafana/pkg/infra/log" -) - -var LoggerFactory alertingLogging.LoggerFactory = func(logger string, ctx ...any) alertingLogging.Logger { - return &logWrapper{log.New(append([]any{logger}, ctx...)...)} -} - -func newLogWrapper(logger log.Logger, ctx ...any) alertingLogging.Logger { - return &logWrapper{logger.New(ctx...)} -} - -type logWrapper struct { - *log.ConcreteLogger -} - -func (l logWrapper) New(ctx ...any) alertingLogging.Logger { - return logWrapper{l.ConcreteLogger.New(ctx...)} -}