Skip to content

Commit

Permalink
opsgenie logs added
Browse files Browse the repository at this point in the history
  • Loading branch information
kotkovkirill committed Oct 16, 2024
1 parent 8d50818 commit 5276ced
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 22 additions & 5 deletions pkg/services/ngalert/notifier/channels/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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),
Expand All @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/services/ngalert/notifier/channels/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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", "","\"", " ")
Expand Down

0 comments on commit 5276ced

Please sign in to comment.