Skip to content

Commit 86432b0

Browse files
committed
Add timeout option for webhook notifier.
1 parent 82b89dc commit 86432b0

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

config/notifiers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ type WebhookConfig struct {
535535
// Alerts exceeding this threshold will be truncated. Setting this to 0
536536
// allows an unlimited number of alerts.
537537
MaxAlerts uint64 `yaml:"max_alerts" json:"max_alerts"`
538+
539+
// Timeout is the maximum time allowed to invoke the webhook.
540+
Timeout *time.Duration `yaml:"timeout" json:"timeout"`
538541
}
539542

540543
// UnmarshalYAML implements the yaml.Unmarshaler interface.

notify/webhook/webhook.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bytes"
1818
"context"
1919
"encoding/json"
20+
"errors"
2021
"fmt"
2122
"log/slog"
2223
"net/http"
@@ -112,8 +113,17 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
112113
url = strings.TrimSpace(string(content))
113114
}
114115

116+
if n.conf.Timeout != nil {
117+
postCtx, cancel := context.WithTimeoutCause(ctx, *n.conf.Timeout, fmt.Errorf("configured webhook timeout (%s) reached", *n.conf.Timeout))
118+
defer cancel()
119+
ctx = postCtx
120+
}
121+
115122
resp, err := notify.PostJSON(ctx, n.client, url, &buf)
116123
if err != nil {
124+
if errors.Is(err, context.DeadlineExceeded) && ctx.Err() != nil {
125+
err = context.Cause(ctx)
126+
}
117127
return true, notify.RedactURL(err)
118128
}
119129
defer notify.Drain(resp)

test/with_api_v2/acceptance/send_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,52 @@ receivers:
464464

465465
t.Log(co.Check())
466466
}
467+
468+
func TestWebhookTimeout(t *testing.T) {
469+
t.Parallel()
470+
471+
// This integration test uses an extended group_interval to check that
472+
// the webhook level timeout has the desired effect, and that notification
473+
// sending is retried in this case.
474+
conf := `
475+
route:
476+
receiver: "default"
477+
group_by: [alertname]
478+
group_wait: 1s
479+
group_interval: 1m
480+
repeat_interval: 1m
481+
482+
receivers:
483+
- name: "default"
484+
webhook_configs:
485+
- url: 'http://%s'
486+
timeout: 500ms
487+
`
488+
489+
at := NewAcceptanceTest(t, &AcceptanceOpts{
490+
Tolerance: 150 * time.Millisecond,
491+
})
492+
493+
co := at.Collector("webhook")
494+
wh := NewWebhook(t, co)
495+
496+
wh.Func = func(ts float64) bool {
497+
// Make some webhook requests slow enough to hit the webhook
498+
// timeout, but not so slow as to hit the dispatcher timeout.
499+
if ts < 3 {
500+
time.Sleep(time.Second)
501+
return true
502+
}
503+
return false
504+
}
505+
506+
am := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
507+
508+
am.Push(At(1), Alert("alertname", "test1"))
509+
510+
co.Want(Between(3, 4), Alert("alertname", "test1").Active(1))
511+
512+
at.Run()
513+
514+
t.Log(co.Check())
515+
}

0 commit comments

Comments
 (0)