|
| 1 | +package publisher |
| 2 | + |
| 3 | +//go:generate go tool mockgen -package mock_publisher -destination ../mock/publisher/webhook.go -source webhook.go -mock_names httpClient=Mock_httpClient httpClient |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + |
| 13 | + "github.com/flashbots/amp-alerts-sink/config" |
| 14 | + "github.com/flashbots/amp-alerts-sink/db" |
| 15 | + "github.com/flashbots/amp-alerts-sink/logutils" |
| 16 | + "github.com/flashbots/amp-alerts-sink/types" |
| 17 | + "go.uber.org/zap" |
| 18 | +) |
| 19 | + |
| 20 | +type webhook struct { |
| 21 | + url string |
| 22 | + method string |
| 23 | + sendBody bool |
| 24 | + |
| 25 | + client httpClient |
| 26 | + db db.DB |
| 27 | +} |
| 28 | + |
| 29 | +type httpClient interface { |
| 30 | + Do(req *http.Request) (*http.Response, error) |
| 31 | +} |
| 32 | + |
| 33 | +func NewWebhook(cfg *config.Webhook, db db.DB) Publisher { |
| 34 | + method := cfg.Method |
| 35 | + if method == "" { |
| 36 | + method = "POST" |
| 37 | + } |
| 38 | + |
| 39 | + return &webhook{ |
| 40 | + url: cfg.URL, |
| 41 | + method: method, |
| 42 | + sendBody: cfg.SendBody, |
| 43 | + |
| 44 | + client: http.DefaultClient, |
| 45 | + db: db, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (w *webhook) Publish( |
| 50 | + ctx context.Context, |
| 51 | + source string, |
| 52 | + alert *types.AlertmanagerAlert, |
| 53 | +) error { |
| 54 | + l := logutils.LoggerFromContext(ctx) |
| 55 | + l.Info("Publishing alert", zap.Any("alert", alert)) |
| 56 | + |
| 57 | + isDup, err := w.checkDup(ctx, alert) |
| 58 | + if isDup { |
| 59 | + // Enter this branch even with non-nil err; |
| 60 | + // Only for ErrAlreadyLocked, so that lambda execution will be restarted |
| 61 | + l.Info("Duplicate alert detected", zap.Error(err)) |
| 62 | + return err |
| 63 | + } |
| 64 | + // not a duplicate |
| 65 | + if err != nil { |
| 66 | + l.Error("Failed to check for duplicate alert, sending webhook", zap.Error(err)) |
| 67 | + } |
| 68 | + |
| 69 | + err = w.sendWebhook(ctx, source, alert) |
| 70 | + if err != nil { |
| 71 | + l.Error("Failed to send alert", zap.Error(err)) |
| 72 | + } else { |
| 73 | + // sent correctly, prevent other instances from sending |
| 74 | + _ = w.db.Set(ctx, alert.MessageDedupKey(), timeoutWebhookExpiry, "1") |
| 75 | + } |
| 76 | + return err |
| 77 | +} |
| 78 | + |
| 79 | +func (w *webhook) checkDup(ctx context.Context, alert *types.AlertmanagerAlert) (isDup bool, err error) { |
| 80 | + v, err := w.db.Get(ctx, alert.MessageDedupKey()) |
| 81 | + if err != nil { |
| 82 | + return false, fmt.Errorf("failed to check for duplicate alert: %w", err) |
| 83 | + } |
| 84 | + if v != "" { |
| 85 | + return true, nil |
| 86 | + } |
| 87 | + |
| 88 | + didLock, err := w.db.Lock(ctx, alert.MessageDedupKey(), timeoutLock) |
| 89 | + if err != nil { |
| 90 | + return false, fmt.Errorf("failed to lock alert: %w", err) |
| 91 | + } |
| 92 | + if !didLock { |
| 93 | + // another instance is about to publish |
| 94 | + return true, ErrAlreadyLocked |
| 95 | + } |
| 96 | + |
| 97 | + return false, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (w *webhook) sendWebhook( |
| 101 | + ctx context.Context, |
| 102 | + source string, |
| 103 | + alert *types.AlertmanagerAlert, |
| 104 | +) error { |
| 105 | + l := logutils.LoggerFromContext(ctx) |
| 106 | + |
| 107 | + var reqBody io.Reader |
| 108 | + var contentType string |
| 109 | + |
| 110 | + if w.sendBody { |
| 111 | + buf, err := w.encodeAlert(source, alert) |
| 112 | + if err != nil { |
| 113 | + l.Error("Failed to encode alert", zap.Error(err)) |
| 114 | + return err |
| 115 | + } |
| 116 | + l.Debug("Webhook payload", zap.ByteString("body", buf.Bytes())) |
| 117 | + |
| 118 | + reqBody = buf |
| 119 | + contentType = "application/json" |
| 120 | + } |
| 121 | + |
| 122 | + req, err := http.NewRequestWithContext(ctx, w.method, w.url, reqBody) |
| 123 | + if err != nil { |
| 124 | + l.Error("Failed to create webhook request", zap.Error(err)) |
| 125 | + return fmt.Errorf("failed to create webhook request: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + if contentType != "" { |
| 129 | + req.Header.Set("Content-Type", contentType) |
| 130 | + } |
| 131 | + |
| 132 | + l.Info("Sending webhook request", |
| 133 | + zap.String("url", w.url), |
| 134 | + zap.String("method", w.method), |
| 135 | + zap.Bool("send_body", w.sendBody), |
| 136 | + zap.String("alert_fingerprint", alert.MessageDedupKey()), |
| 137 | + ) |
| 138 | + |
| 139 | + resp, err := w.client.Do(req) |
| 140 | + if err != nil { |
| 141 | + l.Error("Webhook request failed", zap.Error(err)) |
| 142 | + return fmt.Errorf("webhook request failed: %w", err) |
| 143 | + } |
| 144 | + defer resp.Body.Close() |
| 145 | + |
| 146 | + respBody, readErr := io.ReadAll(resp.Body) |
| 147 | + if readErr != nil { |
| 148 | + l.Warn("Failed to read webhook response body", zap.Error(readErr)) |
| 149 | + } |
| 150 | + |
| 151 | + if resp.StatusCode != http.StatusOK { |
| 152 | + l.Error("Webhook returned non-200 status", |
| 153 | + zap.Int("status_code", resp.StatusCode), |
| 154 | + zap.String("response_body", string(respBody)), |
| 155 | + ) |
| 156 | + return fmt.Errorf("webhook returned status %d: %s", resp.StatusCode, resp.Status) |
| 157 | + } |
| 158 | + |
| 159 | + l.Info("Successfully published alert to webhook", |
| 160 | + zap.String("response_body", string(respBody)), |
| 161 | + ) |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func (w *webhook) encodeAlert(source string, alert *types.AlertmanagerAlert) (*bytes.Buffer, error) { |
| 166 | + body := types.AlertmanagerWebhook{ |
| 167 | + Version: "4", |
| 168 | + GroupKey: alert.MessageDedupKey(), |
| 169 | + |
| 170 | + AlertmanagerMessage: types.AlertmanagerMessage{ |
| 171 | + Receiver: source, |
| 172 | + Status: alert.Status, |
| 173 | + Alerts: []types.AlertmanagerAlert{*alert}, |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + buf := &bytes.Buffer{} |
| 178 | + if err := json.NewEncoder(buf).Encode(body); err != nil { |
| 179 | + return nil, fmt.Errorf("failed to marshal webhook payload: %w", err) |
| 180 | + } |
| 181 | + |
| 182 | + return buf, nil |
| 183 | +} |
0 commit comments