From 906a8977fed88cb0198a065c7b4f0d68f1f5cf07 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Fri, 21 Apr 2023 16:38:45 -0500 Subject: [PATCH] alerting: extract more information from PagerDuty error responses Issue: https://github.com/moov-io/achgateway/issues/171 Issue: https://github.com/PagerDuty/go-pagerduty/issues/476 --- internal/alerting/pagerduty.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/alerting/pagerduty.go b/internal/alerting/pagerduty.go index 5f1eb81..bb08e7e 100644 --- a/internal/alerting/pagerduty.go +++ b/internal/alerting/pagerduty.go @@ -5,8 +5,10 @@ import ( "crypto/sha256" "errors" "fmt" + "io" "os" "runtime" + "strings" "time" "github.com/PagerDuty/go-pagerduty" @@ -23,6 +25,9 @@ func NewPagerDutyAlerter(cfg *service.PagerDutyAlerting) (*PagerDuty, error) { client: pagerduty.NewClient(cfg.ApiKey), routingKey: cfg.RoutingKey, } + if notifier.client != nil { + notifier.client.SetDebugFlag(pagerduty.DebugCaptureLastResponse) + } if err := notifier.ping(); err != nil { return nil, err } @@ -69,9 +74,20 @@ func (pd *PagerDuty) AlertError(e error) error { } ctx := context.Background() - _, err = pd.client.ManageEventWithContext(ctx, event) + v2EventResponse, err := pd.client.ManageEventWithContext(ctx, event) if err != nil { - return fmt.Errorf("creating event in PagerDuty: %v", err) + var httpRespBody []byte + httpResp, _ := pd.client.LastAPIResponse() + if httpResp != nil && httpResp.Body != nil { + httpRespBody, _ = io.ReadAll(httpResp.Body) + } + var outErr error + if v2EventResponse != nil { + outErr = fmt.Errorf("%s problem creating PagerDuty event caused by %s: %s", v2EventResponse.Status, v2EventResponse.Message, strings.Join(v2EventResponse.Errors, ", ")) + } else { + outErr = fmt.Errorf("unexpected response of %s from creating event in PagerDuty: %v", string(httpRespBody), err) + } + return outErr } return nil