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

    • Configuration docs updated to describe the new optional timeout parameter and its behavior (default 0s = no timeout; values above group_interval do not change behavior).
  • Tests

    • Added tests covering timeout scenarios to verify success or failure based on configured timeouts.

@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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: da16ff4d-f9e3-4c5b-bf53-cd07ce4de6ef

📥 Commits

Reviewing files that changed from the base of the PR and between 11013db and 730f944.

📒 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 (1)
  • notify/telegram/telegram_test.go

📝 Walkthrough

Walkthrough

Adds a Timeout time.Duration field to the Telegram notifier config, applies it to the notifier HTTP client's Timeout when >0, documents the new timeout option, and adds tests validating notifier behavior when handler latency crosses the configured timeout.

Changes

Cohort / File(s) Summary
Configuration
config/notifiers.go
Added Timeout time.Duration to TelegramConfig with yaml:"timeout" and json:"timeout".
Documentation
docs/configuration.md
Documented new `timeout:
Notifier implementation
notify/telegram/telegram.go
When building the notifier, set the underlying HTTP client's Timeout to conf.Timeout if conf.Timeout > 0.
Tests
notify/telegram/telegram_test.go
Added TestTelegramTimeout that spins up a test server simulating delayed /bot<token>/sendMessage responses and asserts success/failure depending on configured Timeout vs handler latency.

Sequence Diagram(s)

sequenceDiagram
  participant Notifier as Notifier (New / Notify)
  participant HTTP as HTTP Client (with Timeout)
  participant API as Telegram API (test server)

  Notifier->>HTTP: create client (apply conf.Timeout if >0)
  Notifier->>HTTP: POST /bot<token>/sendMessage (alert)
  HTTP->>API: request
  API-->>HTTP: delayed response (latency)
  alt latency < Timeout
    HTTP-->>Notifier: 200 OK (forward success)
  else latency >= Timeout
    HTTP--x API: timeout occurs (client cancels)
    HTTP-->>Notifier: timeout error
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add timeout option for telegram notifier' clearly and directly summarizes the main change—adding a timeout configuration feature to the Telegram notifier.
Description check ✅ Passed The PR description covers the main objective, includes a completed checklist confirming tests were added and documentation updated, notes that commits were signed off, and specifies release notes. However, it is missing the related issue number (shows 'Fixes #' unfilled).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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 thread docs/configuration.md
Comment thread notify/telegram/telegram_test.go
@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

Signed-off-by: Ali Afsharzadeh <afsharzadeh8@gmail.com>
@guoard guoard force-pushed the telegram_timeout branch from 11013db to 730f944 Compare April 28, 2026 12:19
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