diff --git a/pkg/services/ngalert/notifier/channels/opsgenie.go b/pkg/services/ngalert/notifier/channels/opsgenie.go index 49d35638a8454..0c06b53eb4834 100644 --- a/pkg/services/ngalert/notifier/channels/opsgenie.go +++ b/pkg/services/ngalert/notifier/channels/opsgenie.go @@ -16,6 +16,7 @@ import ( "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" "github.com/prometheus/common/model" + "github.com/google/uuid" ) const ( @@ -106,30 +107,43 @@ func NewOpsgenieNotifier(config *OpsgenieConfig, ns notifications.WebhookSender, // Notify sends an alert notification to Opsgenie func (on *OpsgenieNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { - on.log.Debug("Executing Opsgenie notification", "notification", on.Name) + + id := uuid.New() + logger := on.log.New("notificationId", id.String()) + + logger.Info("Executing Opsgenie notification", "notification", on.Name) alerts := types.Alerts(as...) if alerts.Status() == model.AlertResolved && !on.SendResolved() { - on.log.Debug("Not sending a trigger to Opsgenie", "status", alerts.Status(), "auto resolve", on.SendResolved()) + logger.Info("Not sending a trigger to Opsgenie", "status", alerts.Status(), "auto resolve", on.SendResolved()) return true, nil } bodyJSON, url, err := on.buildOpsgenieMessage(ctx, alerts, as) if err != nil { - return false, fmt.Errorf("build Opsgenie message: %w", err) + error := fmt.Errorf("build Opsgenie message: %w", err) + logger.Error(error.Error()) + return false, error } if url == "" { // Resolved alert with no auto close. // Hence skip sending anything. + logger.Info("Resolved alert with no auto close, not sending anything") return true, nil } + + body, err := json.Marshal(bodyJSON) if err != nil { - return false, fmt.Errorf("marshal json: %w", err) + error := fmt.Errorf("marshal json: %w", err) + logger.Error(error.Error()) + return false, error } + logger.Info("Sending Opsgenie API request", "url", url, "body", minify(body)) + cmd := &models.SendWebhookSync{ Url: url, Body: string(body), @@ -141,9 +155,12 @@ func (on *OpsgenieNotifier) Notify(ctx context.Context, as ...*types.Alert) (boo } if err := on.ns.SendWebhookSync(ctx, cmd); err != nil { - return false, fmt.Errorf("send notification to Opsgenie: %w", err) + error := fmt.Errorf("send notification to Opsgenie: %w", err) + logger.Error(error.Error()) + return false, error } + logger.Info("Sending Opsgenie API request succeeded", "url", url) return true, nil } diff --git a/pkg/services/ngalert/notifier/channels/slack.go b/pkg/services/ngalert/notifier/channels/slack.go index 36d37aa00d749..45db1569901c2 100644 --- a/pkg/services/ngalert/notifier/channels/slack.go +++ b/pkg/services/ngalert/notifier/channels/slack.go @@ -190,7 +190,6 @@ func (sn *SlackNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, return false, fmt.Errorf("marshal json: %w", err) } - //logger.Info("Sending Slack API request", "url", sn.URL.String(), "data", string(b)) logger.Info("Sending Slack API request", "url", sn.URL.String(), "body", sn.minify(b)) request, err := http.NewRequestWithContext(ctx, http.MethodPost, sn.URL.String(), bytes.NewReader(b)) @@ -352,7 +351,7 @@ func (sn *SlackNotifier) SendResolved() bool { return !sn.GetDisableResolveMessage() } -func (sn *SlackNotifier) minify(src []byte) string { +func minify(src []byte) string { dst := &bytes.Buffer{} if err := json.Compact(dst, []byte(src)); err != nil { r := strings.NewReplacer("\n", "","\"", " ")