From 971123a633f5fb1779209405423fa8dc71cf91b6 Mon Sep 17 00:00:00 2001 From: Steve Simpson Date: Fri, 29 Nov 2024 13:40:08 +0100 Subject: [PATCH] Tweak error handling and de-pointer timeout field. Signed-off-by: Steve Simpson --- config/notifiers.go | 5 +++-- docs/configuration.md | 5 +++-- notify/webhook/webhook.go | 9 ++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index 3ff062c1ad..74c74a92a0 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -536,8 +536,9 @@ type WebhookConfig struct { // allows an unlimited number of alerts. MaxAlerts uint64 `yaml:"max_alerts" json:"max_alerts"` - // Timeout is the maximum time allowed to invoke the webhook. - Timeout *time.Duration `yaml:"timeout" json:"timeout"` + // Timeout is the maximum time allowed to invoke the webhook. Setting this to 0 + // does not impose a timeout. + Timeout time.Duration `yaml:"timeout" json:"timeout"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/docs/configuration.md b/docs/configuration.md index 2df61139d3..f2a4f6b197 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1596,9 +1596,10 @@ url_file: [ max_alerts: | default = 0 ] # The maximum time to wait for a webhook request to complete, before failing the -# request and allowing it to be retried. +# request and allowing it to be retried. The default value of 0s indicates that +# no timeout should be applied. # NOTE: This will have no effect if set higher than the group_interval. -[ timeout: | default = ] +[ timeout: | default = 0s ] ``` diff --git a/notify/webhook/webhook.go b/notify/webhook/webhook.go index 801d5c0f5b..41b9f497dc 100644 --- a/notify/webhook/webhook.go +++ b/notify/webhook/webhook.go @@ -17,7 +17,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "log/slog" "net/http" @@ -113,16 +112,16 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er url = strings.TrimSpace(string(content)) } - if n.conf.Timeout != nil { - postCtx, cancel := context.WithTimeoutCause(ctx, *n.conf.Timeout, fmt.Errorf("configured webhook timeout (%s) reached", *n.conf.Timeout)) + if n.conf.Timeout > 0 { + postCtx, cancel := context.WithTimeoutCause(ctx, n.conf.Timeout, fmt.Errorf("configured webhook timeout reached (%s)", n.conf.Timeout)) defer cancel() ctx = postCtx } resp, err := notify.PostJSON(ctx, n.client, url, &buf) if err != nil { - if errors.Is(err, context.DeadlineExceeded) && ctx.Err() != nil { - err = context.Cause(ctx) + if ctx.Err() != nil { + err = fmt.Errorf("%w: %w", err, context.Cause(ctx)) } return true, notify.RedactURL(err) }