Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,10 @@ type TelegramConfig struct {
Message string `yaml:"message,omitempty" json:"message,omitempty"`
DisableNotifications bool `yaml:"disable_notifications,omitempty" json:"disable_notifications,omitempty"`
ParseMode string `yaml:"parse_mode,omitempty" json:"parse_mode,omitempty"`

// Timeout is the maximum time allowed to invoke the telegram. Setting this to 0
// does not impose a timeout.
Timeout time.Duration `yaml:"timeout" json:"timeout"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
Expand Down
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,12 @@ attributes:

# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]

# The maximum time to wait for a telegram request to complete, before failing the
# 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: <duration> | default = 0s ]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
```

### `<victorops_config>`
Expand Down
4 changes: 4 additions & 0 deletions notify/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func New(conf *config.TelegramConfig, t *template.Template, l *slog.Logger, http
return nil, err
}

if conf.Timeout > 0 {
httpclient.Timeout = conf.Timeout
}

client, err := createTelegramClient(conf.APIUrl.String(), conf.ParseMode, httpclient)
if err != nil {
return nil, err
Expand Down
62 changes: 62 additions & 0 deletions notify/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,65 @@ func TestTelegramNotify(t *testing.T) {
})
}
}

func TestTelegramTimeout(t *testing.T) {
token := "secret"

tests := []struct {
name string
latency time.Duration
timeout time.Duration
wantErr bool
}{
{
name: "success",
latency: 100 * time.Millisecond,
timeout: 120 * time.Millisecond,
wantErr: false,
},
{
name: "timeout",
latency: 100 * time.Millisecond,
timeout: 80 * time.Millisecond,
wantErr: true,
Comment thread
guoard marked this conversation as resolved.
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/bot"+token+"/sendMessage", r.URL.Path)
_, err := io.ReadAll(r.Body)
require.NoError(t, err)
time.Sleep(tc.latency)
w.Write([]byte(`{"ok":true,"result":{"chat":{}}}`))
}))
defer srv.Close()
u, _ := url.Parse(srv.URL)

cfg := &config.TelegramConfig{
Message: "test",
HTTPConfig: &commoncfg.HTTPClientConfig{},
BotToken: commoncfg.Secret(token),
Timeout: tc.timeout,
APIUrl: &amcommoncfg.URL{URL: u},
}

notifier, err := New(cfg, test.CreateTmpl(t), promslog.NewNopLogger())
require.NoError(t, err)

ctx := context.Background()
ctx = notify.WithGroupKey(ctx, "1")

alert := &types.Alert{
Alert: model.Alert{
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Hour),
},
}

_, err = notifier.Notify(ctx, alert)
require.Equal(t, tc.wantErr, err != nil)
})
}
}