Skip to content

Commit b4f0963

Browse files
committed
Tweak error handling and de-pointer timeout field.
1 parent b6d1f67 commit b4f0963

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

config/notifiers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,9 @@ type WebhookConfig struct {
536536
// allows an unlimited number of alerts.
537537
MaxAlerts uint64 `yaml:"max_alerts" json:"max_alerts"`
538538

539-
// Timeout is the maximum time allowed to invoke the webhook.
540-
Timeout *time.Duration `yaml:"timeout" json:"timeout"`
539+
// Timeout is the maximum time allowed to invoke the webhook. Setting this to 0
540+
// does not impose a timeout.
541+
Timeout time.Duration `yaml:"timeout" json:"timeout"`
541542
}
542543

543544
// UnmarshalYAML implements the yaml.Unmarshaler interface.

docs/configuration.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,9 +1596,10 @@ url_file: <filepath>
15961596
[ max_alerts: <int> | default = 0 ]
15971597
15981598
# The maximum time to wait for a webhook request to complete, before failing the
1599-
# request and allowing it to be retried.
1599+
# request and allowing it to be retried. The default value of 0s indicates that
1600+
# no timeout should be applied.
16001601
# NOTE: This will have no effect if set higher than the group_interval.
1601-
[ timeout: <duration> | default = <none> ]
1602+
[ timeout: <duration> | default = 0s ]
16021603
16031604
```
16041605

notify/webhook/webhook.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"bytes"
1818
"context"
1919
"encoding/json"
20-
"errors"
2120
"fmt"
2221
"log/slog"
2322
"net/http"
@@ -113,16 +112,16 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
113112
url = strings.TrimSpace(string(content))
114113
}
115114

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))
115+
if n.conf.Timeout > 0 {
116+
postCtx, cancel := context.WithTimeoutCause(ctx, n.conf.Timeout, fmt.Errorf("configured webhook timeout reached (%s)", n.conf.Timeout))
118117
defer cancel()
119118
ctx = postCtx
120119
}
121120

122121
resp, err := notify.PostJSON(ctx, n.client, url, &buf)
123122
if err != nil {
124-
if errors.Is(err, context.DeadlineExceeded) && ctx.Err() != nil {
125-
err = context.Cause(ctx)
123+
if ctx.Err() != nil {
124+
err = fmt.Errorf("%w: %w", err, context.Cause(ctx))
126125
}
127126
return true, notify.RedactURL(err)
128127
}

0 commit comments

Comments
 (0)