Skip to content

Add timeout option for telegram notifier#5127

Open
guoard wants to merge 1 commit intoprometheus:mainfrom
guoard:telegram_timeout
Open

Add timeout option for telegram notifier#5127
guoard wants to merge 1 commit intoprometheus:mainfrom
guoard:telegram_timeout

Conversation

@guoard
Copy link
Copy Markdown
Contributor

@guoard guoard commented Mar 28, 2026

This change introduces a Timeout option for the Telegram notifier. When set, the HTTP client used by the notifier will time out if a request to the Telegram API takes longer than the configured duration.

Pull Request Checklist

Please check all the applicable boxes.

  • Please list all open issue(s) discussed with maintainers related to this change
    • Fixes #
  • Is this a new Receiver integration?
  • Is this a bugfix?
    • I have added tests that can reproduce the bug which pass with this bugfix applied
  • Is this a new feature?
    • I have added tests that test the new feature's functionality
  • Does this change affect performance?
    • I have provided benchmarks comparison that shows performance is improved or is not degraded
      • You can use benchstat to compare benchmarks
    • I have added new benchmarks if required or requested by maintainers
  • Is this a breaking change?
    • My changes do not break the existing cluster messages
    • My changes do not break the existing api
  • I have added/updated the required documentation
  • I have signed-off my commits
  • I will follow best practices for contributing to this project

Which user-facing changes does this PR introduce?

NONE

Summary by CodeRabbit

  • New Features

    • Added a configurable timeout for Telegram notifications so users can set how long to wait for a request before it fails and may be retried.
  • Documentation

    • Config docs updated to describe the new optional timeout parameter and its behavior (0 = no timeout; respects group interval limits).
  • Tests

    • Added tests covering timeout behavior to ensure correct success/failure handling.

@guoard guoard requested a review from a team as a code owner March 28, 2026 12:58
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

Added a configurable Timeout to Telegram notifier config, applied to the notifier's HTTP client when >0, documented in the configuration reference, and covered by new tests that assert timeout vs handler latency behaviors.

Changes

Cohort / File(s) Summary
Configuration & Documentation
config/notifiers.go, docs/configuration.md
Added Timeout time.Duration to TelegramConfig (yaml:"timeout", json:"timeout"); documented `timeout:
Implementation & Tests
notify/telegram/telegram.go, notify/telegram/telegram_test.go
In New(...), set the HTTP client's Timeout when conf.Timeout > 0; added TestTelegramTimeout table-driven test validating notifier behavior across latency vs timeout scenarios.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐇 whiskers twitch, I bound and say,
A timeout hops and clears the way,
No waiting long for Telegram's cheer,
Quick paws, swift sends, the path is clear,
I nibble bugs and dance — hooray!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a timeout option for the telegram notifier.
Description check ✅ Passed The description addresses the main change, confirms tests were added for the new feature, documentation was updated, and commits were signed off, meeting key template requirements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/configuration.md`:
- Around line 1805-1809: Update the documentation for the timeout setting to
remove or fix the misleading note about group_interval: clarify that the
Telegram receiver applies timeout directly to the HTTP client (symbol: timeout)
and does not compare or enforce it against group_interval, so setting timeout
higher than group_interval does not get automatically capped; rephrase the
sentence to state this behavior explicitly and ensure the note references the
Telegram receiver behavior rather than implying an enforced relationship with
group_interval.

In `@notify/telegram/telegram_test.go`:
- Around line 227-236: The "success" test case in telegram_test.go has a tight
timing window (latency: 100ms vs timeout: 120ms) which can be flaky on CI;
update the test table entries (the cases with name "success" and "timeout" that
set the latency and timeout fields) to increase the headroom—e.g., raise the
"success" timeout to a safer value (200ms) or reduce the simulated latency—so
the success case has a comfortable margin while keeping the "timeout" case still
below the latency.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eebc574c-57db-4a14-938a-6138ed179007

📥 Commits

Reviewing files that changed from the base of the PR and between 19e792f and c627cec.

📒 Files selected for processing (4)
  • config/notifiers.go
  • docs/configuration.md
  • notify/telegram/telegram.go
  • notify/telegram/telegram_test.go

Comment on lines +227 to +236
name: "success",
latency: 100 * time.Millisecond,
timeout: 120 * time.Millisecond,
wantErr: false,
},
{
name: "timeout",
latency: 100 * time.Millisecond,
timeout: 80 * time.Millisecond,
wantErr: true,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Tight timing window can make this test flaky.

The success case only leaves ~20ms headroom (100ms latency vs 120ms timeout), which is fragile on busy CI runners.

Proposed stabilization tweak
 		{
 			name:    "success",
-			latency: 100 * time.Millisecond,
-			timeout: 120 * time.Millisecond,
+			latency: 50 * time.Millisecond,
+			timeout: 1 * time.Second,
 			wantErr: false,
 		},
 		{
 			name:    "timeout",
-			latency: 100 * time.Millisecond,
-			timeout: 80 * time.Millisecond,
+			latency: 250 * time.Millisecond,
+			timeout: 25 * time.Millisecond,
 			wantErr: true,
 		},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
name: "success",
latency: 100 * time.Millisecond,
timeout: 120 * time.Millisecond,
wantErr: false,
},
{
name: "timeout",
latency: 100 * time.Millisecond,
timeout: 80 * time.Millisecond,
wantErr: true,
name: "success",
latency: 50 * time.Millisecond,
timeout: 1 * time.Second,
wantErr: false,
},
{
name: "timeout",
latency: 250 * time.Millisecond,
timeout: 25 * time.Millisecond,
wantErr: true,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@notify/telegram/telegram_test.go` around lines 227 - 236, The "success" test
case in telegram_test.go has a tight timing window (latency: 100ms vs timeout:
120ms) which can be flaky on CI; update the test table entries (the cases with
name "success" and "timeout" that set the latency and timeout fields) to
increase the headroom—e.g., raise the "success" timeout to a safer value (200ms)
or reduce the simulated latency—so the success case has a comfortable margin
while keeping the "timeout" case still below the latency.

Signed-off-by: Ali Afsharzadeh <afsharzadeh8@gmail.com>
@guoard guoard force-pushed the telegram_timeout branch from c627cec to 881b187 Compare March 28, 2026 17:47
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
notify/telegram/telegram_test.go (1)

240-276: Test logic is sound; consider verifying error type for the timeout case.

The test structure is clean: proper server lifecycle management, correct context setup for testing HTTP client timeout (not context deadline), and appropriate use of table-driven tests.

One optional enhancement: the timeout case could verify the error is specifically a timeout rather than any error, which would catch false positives if the notifier fails for unrelated reasons.

💡 Optional: Verify timeout error type
-			_, err = notifier.Notify(ctx, alert)
-			require.Equal(t, tc.wantErr, err != nil)
+			_, err = notifier.Notify(ctx, alert)
+			if tc.wantErr {
+				require.Error(t, err)
+				var netErr net.Error
+				require.ErrorAs(t, err, &netErr)
+				require.True(t, netErr.Timeout(), "expected timeout error")
+			} else {
+				require.NoError(t, err)
+			}

Would require adding "net" to imports.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@notify/telegram/telegram_test.go` around lines 240 - 276, Update the timeout
test case to assert the error is specifically a timeout: after calling
notifier.Notify(ctx, alert) check that err is non-nil and then assert it
satisfies net.Error && err.Timeout() (or use errors.Is for
context.DeadlineExceeded if appropriate); modify the table-case that sets
tc.timeout/wantErr to also expect a timeout and add the "net" import to the test
file; locate this change around the test loop that constructs cfg, calls
New(...) and notifier.Notify(...) in notify/telegram/telegram_test.go.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@notify/telegram/telegram_test.go`:
- Around line 240-276: Update the timeout test case to assert the error is
specifically a timeout: after calling notifier.Notify(ctx, alert) check that err
is non-nil and then assert it satisfies net.Error && err.Timeout() (or use
errors.Is for context.DeadlineExceeded if appropriate); modify the table-case
that sets tc.timeout/wantErr to also expect a timeout and add the "net" import
to the test file; locate this change around the test loop that constructs cfg,
calls New(...) and notifier.Notify(...) in notify/telegram/telegram_test.go.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f6083ffc-5fd0-4014-84a7-4ec6e119c915

📥 Commits

Reviewing files that changed from the base of the PR and between c627cec and 881b187.

📒 Files selected for processing (4)
  • config/notifiers.go
  • docs/configuration.md
  • notify/telegram/telegram.go
  • notify/telegram/telegram_test.go
✅ Files skipped from review due to trivial changes (1)
  • docs/configuration.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • notify/telegram/telegram.go
  • config/notifiers.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants