Skip to content

Commit 11013db

Browse files
committed
Add timeout option for telegram notifier
Signed-off-by: Ali Afsharzadeh <afsharzadeh8@gmail.com>
1 parent dcbdc8c commit 11013db

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

config/notifiers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,10 @@ type TelegramConfig struct {
938938
Message string `yaml:"message,omitempty" json:"message,omitempty"`
939939
DisableNotifications bool `yaml:"disable_notifications,omitempty" json:"disable_notifications,omitempty"`
940940
ParseMode string `yaml:"parse_mode,omitempty" json:"parse_mode,omitempty"`
941+
942+
// Timeout is the maximum time allowed to invoke the telegram. Setting this to 0
943+
// does not impose a timeout.
944+
Timeout time.Duration `yaml:"timeout" json:"timeout"`
941945
}
942946

943947
// UnmarshalYAML implements the yaml.Unmarshaler interface.

docs/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,12 @@ attributes:
18031803

18041804
# The HTTP client's configuration.
18051805
[ http_config: <http_config> | default = global.http_config ]
1806+
1807+
# The maximum time to wait for a telegram request to complete, before failing the
1808+
# request and allowing it to be retried. The default value of 0s indicates that
1809+
# no timeout should be applied.
1810+
# NOTE: This will have no effect if set higher than the group_interval.
1811+
[ timeout: <duration> | default = 0s ]
18061812
```
18071813
18081814
### `<victorops_config>`

notify/telegram/telegram.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func New(conf *config.TelegramConfig, t *template.Template, l *slog.Logger, http
5050
return nil, err
5151
}
5252

53+
if conf.Timeout > 0 {
54+
httpclient.Timeout = conf.Timeout
55+
}
56+
5357
client, err := createTelegramClient(conf.APIUrl.String(), conf.ParseMode, httpclient)
5458
if err != nil {
5559
return nil, err

notify/telegram/telegram_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,65 @@ func TestTelegramNotify(t *testing.T) {
213213
})
214214
}
215215
}
216+
217+
func TestTelegramTimeout(t *testing.T) {
218+
token := "secret"
219+
220+
tests := []struct {
221+
name string
222+
latency time.Duration
223+
timeout time.Duration
224+
wantErr bool
225+
}{
226+
{
227+
name: "success",
228+
latency: 100 * time.Millisecond,
229+
timeout: 120 * time.Millisecond,
230+
wantErr: false,
231+
},
232+
{
233+
name: "timeout",
234+
latency: 100 * time.Millisecond,
235+
timeout: 80 * time.Millisecond,
236+
wantErr: true,
237+
},
238+
}
239+
240+
for _, tc := range tests {
241+
t.Run(tc.name, func(t *testing.T) {
242+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
243+
require.Equal(t, "/bot"+token+"/sendMessage", r.URL.Path)
244+
_, err := io.ReadAll(r.Body)
245+
require.NoError(t, err)
246+
time.Sleep(tc.latency)
247+
w.Write([]byte(`{"ok":true,"result":{"chat":{}}}`))
248+
}))
249+
defer srv.Close()
250+
u, _ := url.Parse(srv.URL)
251+
252+
cfg := &config.TelegramConfig{
253+
Message: "test",
254+
HTTPConfig: &commoncfg.HTTPClientConfig{},
255+
BotToken: commoncfg.Secret(token),
256+
Timeout: tc.timeout,
257+
APIUrl: &amcommoncfg.URL{URL: u},
258+
}
259+
260+
notifier, err := New(cfg, test.CreateTmpl(t), promslog.NewNopLogger())
261+
require.NoError(t, err)
262+
263+
ctx := context.Background()
264+
ctx = notify.WithGroupKey(ctx, "1")
265+
266+
alert := &types.Alert{
267+
Alert: model.Alert{
268+
StartsAt: time.Now(),
269+
EndsAt: time.Now().Add(time.Hour),
270+
},
271+
}
272+
273+
_, err = notifier.Notify(ctx, alert)
274+
require.Equal(t, tc.wantErr, err != nil)
275+
})
276+
}
277+
}

0 commit comments

Comments
 (0)