Skip to content

Commit 0545aa5

Browse files
Use model.Duration for timeout option in webhook notifier
Signed-off-by: Alexander Akhmetov <[email protected]>
1 parent 0ce3cfb commit 0545aa5

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

config/notifiers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ type WebhookConfig struct {
538538

539539
// Timeout is the maximum time allowed to invoke the webhook. Setting this to 0
540540
// does not impose a timeout.
541-
Timeout time.Duration `yaml:"timeout" json:"timeout"`
541+
Timeout model.Duration `yaml:"timeout" json:"timeout"`
542542
}
543543

544544
// UnmarshalYAML implements the yaml.Unmarshaler interface.

config/notifiers_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
package config
1515

1616
import (
17+
"encoding/json"
1718
"errors"
1819
"net/mail"
20+
"net/url"
1921
"reflect"
2022
"strings"
2123
"testing"
24+
"time"
2225

26+
"github.com/prometheus/common/model"
2327
"github.com/stretchr/testify/require"
2428
"gopkg.in/yaml.v2"
2529
)
@@ -1110,6 +1114,24 @@ parse_mode: invalid
11101114
}
11111115
}
11121116

1117+
func TestWebhookJSONMarshalling(t *testing.T) {
1118+
u, err := url.Parse("http://example.com/")
1119+
require.NoError(t, err)
1120+
1121+
cfg := WebhookConfig{
1122+
URL: &SecretURL{u},
1123+
Timeout: model.Duration(500 * time.Millisecond),
1124+
NotifierConfig: NotifierConfig{
1125+
VSendResolved: false,
1126+
},
1127+
}
1128+
1129+
c, err := json.Marshal(cfg)
1130+
1131+
require.NoError(t, err)
1132+
require.JSONEq(t, `{"url":"<secret>","url_file":"","max_alerts":0,"timeout":"500ms", "send_resolved": false}`, string(c))
1133+
}
1134+
11131135
func newBoolPointer(b bool) *bool {
11141136
return &b
11151137
}

notify/webhook/webhook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"os"
2424
"strings"
25+
"time"
2526

2627
commoncfg "github.com/prometheus/common/config"
2728

@@ -113,7 +114,7 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
113114
}
114115

115116
if n.conf.Timeout > 0 {
116-
postCtx, cancel := context.WithTimeoutCause(ctx, n.conf.Timeout, fmt.Errorf("configured webhook timeout reached (%s)", n.conf.Timeout))
117+
postCtx, cancel := context.WithTimeoutCause(ctx, time.Duration(n.conf.Timeout), fmt.Errorf("configured webhook timeout reached (%s)", n.conf.Timeout))
117118
defer cancel()
118119
ctx = postCtx
119120
}

0 commit comments

Comments
 (0)