|
| 1 | +// Copyright 2025 Prometheus Team |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package incidentio |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "log/slog" |
| 23 | + "net/http" |
| 24 | + "os" |
| 25 | + "strings" |
| 26 | + |
| 27 | + commoncfg "github.com/prometheus/common/config" |
| 28 | + |
| 29 | + "github.com/prometheus/alertmanager/config" |
| 30 | + "github.com/prometheus/alertmanager/notify" |
| 31 | + "github.com/prometheus/alertmanager/template" |
| 32 | + "github.com/prometheus/alertmanager/types" |
| 33 | +) |
| 34 | + |
| 35 | +// Notifier implements a Notifier for incident.io. |
| 36 | +type Notifier struct { |
| 37 | + conf *config.IncidentioConfig |
| 38 | + tmpl *template.Template |
| 39 | + logger *slog.Logger |
| 40 | + client *http.Client |
| 41 | + retrier *notify.Retrier |
| 42 | +} |
| 43 | + |
| 44 | +// New returns a new incident.io notifier. |
| 45 | +func New(conf *config.IncidentioConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) { |
| 46 | + // If alert source token is specified, set authorization in HTTP config |
| 47 | + if conf.HTTPConfig == nil { |
| 48 | + conf.HTTPConfig = &commoncfg.HTTPClientConfig{} |
| 49 | + } |
| 50 | + |
| 51 | + if conf.AlertSourceToken != "" { |
| 52 | + if conf.HTTPConfig.Authorization == nil { |
| 53 | + conf.HTTPConfig.Authorization = &commoncfg.Authorization{ |
| 54 | + Type: "Bearer", |
| 55 | + Credentials: commoncfg.Secret(conf.AlertSourceToken), |
| 56 | + } |
| 57 | + } |
| 58 | + } else if conf.AlertSourceTokenFile != "" { |
| 59 | + content, err := os.ReadFile(conf.AlertSourceTokenFile) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to read alert_source_token_file: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + if conf.HTTPConfig.Authorization == nil { |
| 65 | + conf.HTTPConfig.Authorization = &commoncfg.Authorization{ |
| 66 | + Type: "Bearer", |
| 67 | + Credentials: commoncfg.Secret(strings.TrimSpace(string(content))), |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + client, err := commoncfg.NewClientFromConfig(*conf.HTTPConfig, "incidentio", httpOpts...) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + return &Notifier{ |
| 78 | + conf: conf, |
| 79 | + tmpl: t, |
| 80 | + logger: l, |
| 81 | + client: client, |
| 82 | + // Always retry on 429 (rate limiting) and 5xx response codes. |
| 83 | + retrier: ¬ify.Retrier{ |
| 84 | + RetryCodes: []int{ |
| 85 | + http.StatusTooManyRequests, // 429 |
| 86 | + http.StatusInternalServerError, |
| 87 | + http.StatusBadGateway, |
| 88 | + http.StatusServiceUnavailable, |
| 89 | + http.StatusGatewayTimeout, |
| 90 | + }, |
| 91 | + CustomDetailsFunc: errDetails, |
| 92 | + }, |
| 93 | + }, nil |
| 94 | +} |
| 95 | + |
| 96 | +// Message defines the JSON object sent to incident.io endpoints. |
| 97 | +type Message struct { |
| 98 | + *template.Data |
| 99 | + |
| 100 | + // The protocol version. |
| 101 | + Version string `json:"version"` |
| 102 | + GroupKey string `json:"groupKey"` |
| 103 | + TruncatedAlerts uint64 `json:"truncatedAlerts"` |
| 104 | +} |
| 105 | + |
| 106 | +func truncateAlerts(maxAlerts uint64, alerts []*types.Alert) ([]*types.Alert, uint64) { |
| 107 | + if maxAlerts != 0 && uint64(len(alerts)) > maxAlerts { |
| 108 | + return alerts[:maxAlerts], uint64(len(alerts)) - maxAlerts |
| 109 | + } |
| 110 | + |
| 111 | + return alerts, 0 |
| 112 | +} |
| 113 | + |
| 114 | +// Notify implements the Notifier interface. |
| 115 | +func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) { |
| 116 | + alerts, numTruncated := truncateAlerts(n.conf.MaxAlerts, alerts) |
| 117 | + data := notify.GetTemplateData(ctx, n.tmpl, alerts, n.logger) |
| 118 | + |
| 119 | + groupKey, err := notify.ExtractGroupKey(ctx) |
| 120 | + if err != nil { |
| 121 | + return false, err |
| 122 | + } |
| 123 | + |
| 124 | + n.logger.Debug("incident.io notification", "groupKey", groupKey) |
| 125 | + |
| 126 | + msg := &Message{ |
| 127 | + Version: "4", |
| 128 | + Data: data, |
| 129 | + GroupKey: groupKey.String(), |
| 130 | + TruncatedAlerts: numTruncated, |
| 131 | + } |
| 132 | + |
| 133 | + var buf bytes.Buffer |
| 134 | + if err := json.NewEncoder(&buf).Encode(msg); err != nil { |
| 135 | + return false, err |
| 136 | + } |
| 137 | + |
| 138 | + var url string |
| 139 | + if n.conf.URL != nil { |
| 140 | + url = n.conf.URL.String() |
| 141 | + } else { |
| 142 | + content, err := os.ReadFile(n.conf.URLFile) |
| 143 | + if err != nil { |
| 144 | + return false, fmt.Errorf("read url_file: %w", err) |
| 145 | + } |
| 146 | + url = strings.TrimSpace(string(content)) |
| 147 | + } |
| 148 | + |
| 149 | + if n.conf.Timeout > 0 { |
| 150 | + postCtx, cancel := context.WithTimeoutCause(ctx, n.conf.Timeout, fmt.Errorf("configured incident.io timeout reached (%s)", n.conf.Timeout)) |
| 151 | + defer cancel() |
| 152 | + ctx = postCtx |
| 153 | + } |
| 154 | + |
| 155 | + resp, err := notify.PostJSON(ctx, n.client, url, &buf) |
| 156 | + if err != nil { |
| 157 | + if ctx.Err() != nil { |
| 158 | + err = fmt.Errorf("%w: %w", err, context.Cause(ctx)) |
| 159 | + } |
| 160 | + return true, notify.RedactURL(err) |
| 161 | + } |
| 162 | + defer notify.Drain(resp) |
| 163 | + |
| 164 | + shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body) |
| 165 | + if err != nil { |
| 166 | + return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) |
| 167 | + } |
| 168 | + return shouldRetry, err |
| 169 | +} |
| 170 | + |
| 171 | +// errDetails extracts error details from the response for better error messages. |
| 172 | +func errDetails(status int, body io.Reader) string { |
| 173 | + if body == nil { |
| 174 | + return "" |
| 175 | + } |
| 176 | + |
| 177 | + // Try to decode the error message from JSON response |
| 178 | + var errorResponse struct { |
| 179 | + Message string `json:"message"` |
| 180 | + Errors []string `json:"errors"` |
| 181 | + Error string `json:"error"` |
| 182 | + } |
| 183 | + |
| 184 | + if err := json.NewDecoder(body).Decode(&errorResponse); err != nil { |
| 185 | + return "" |
| 186 | + } |
| 187 | + |
| 188 | + // Format the error message |
| 189 | + var parts []string |
| 190 | + if errorResponse.Message != "" { |
| 191 | + parts = append(parts, errorResponse.Message) |
| 192 | + } |
| 193 | + if errorResponse.Error != "" { |
| 194 | + parts = append(parts, errorResponse.Error) |
| 195 | + } |
| 196 | + if len(errorResponse.Errors) > 0 { |
| 197 | + parts = append(parts, strings.Join(errorResponse.Errors, ", ")) |
| 198 | + } |
| 199 | + |
| 200 | + if len(parts) > 0 { |
| 201 | + return strings.Join(parts, ": ") |
| 202 | + } |
| 203 | + return "" |
| 204 | +} |
0 commit comments