Skip to content

notify: log a warning when a template fails to render#5388

Open
mihir-dixit2k27 wants to merge 4 commits into
prometheus:mainfrom
mihir-dixit2k27:log-template-errors
Open

notify: log a warning when a template fails to render#5388
mihir-dixit2k27 wants to merge 4 commits into
prometheus:mainfrom
mihir-dixit2k27:log-template-errors

Conversation

@mihir-dixit2k27

Copy link
Copy Markdown
Contributor

Title: notify: log a warning when a template fails to render

Which user-facing changes does this PR introduce?

[ENHANCEMENT] notify: log a warning when a notifier template fails to render.

When a template fails, the error string gets embedded into the rendered output and sent to the handler. Depending on the handler, this can result in a rejected API call with no log entry pointing to a template failure as the root cause (see #5322 for a real Slack example).

This adds TmplTextWithLogger/TmplHTMLWithLogger in notify/util.go and switches all notifiers to use them. On any template error a Warn log line is emitted with the template name and error before the notifier continues. Delivery behaviour is unchanged, per #3490.

Pull Request Checklist

Signed-off-by: Mihir Dixit <dixitmihir1@gmail.com>
@mihir-dixit2k27
mihir-dixit2k27 requested a review from a team as a code owner July 15, 2026 20:18
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

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: 4d692793-19d9-4ee9-b73e-cad8880f9460

📥 Commits

Reviewing files that changed from the base of the PR and between b283f86 and 693b3b6.

📒 Files selected for processing (1)
  • notify/util_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • notify/util_test.go

📝 Walkthrough

Walkthrough

Notification template rendering now uses logger-aware text and HTML helpers across notifier integrations. The helpers log execution failures and propagate errors through existing error handling. Tests cover logged failures and initialize PagerDuty rendering dependencies.

Changes

Logger-aware notification template rendering

Layer / File(s) Summary
Template helper contract
notify/util.go
Adds text and HTML rendering helpers that log execution failures, propagate errors, and preserve short-circuit behavior.
Notifier integration and validation
notify/*/*.go, notify/pagerduty/pagerduty_test.go, notify/util_test.go
Updates notifier template rendering to pass request-scoped loggers and existing error pointers; tests verify failure logging and error propagation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: logging a warning when notifier templates fail to render.
Description check ✅ Passed The description covers the change, motivation, issue link, tests, and sign-off, and includes a usable release-notes entry.
Linked Issues check ✅ Passed The PR logs template render failures and preserves existing error gating so handlers are skipped when templating fails, matching #5322.
Out of Scope Changes check ✅ Passed The changes are focused on logger-aware template rendering and tests, with no clear unrelated additions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@notify/slack/slack.go`:
- Line 68: In Notify, check err immediately after the final TmplTextWithLogger
call and before JSON encoding or Slack delivery; return false and err when
rendering fails, while preserving the existing request flow when err is nil.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d248b26a-6ea7-4a19-9873-1da633f0e7f7

📥 Commits

Reviewing files that changed from the base of the PR and between 3d96a34 and 41b64cc.

📒 Files selected for processing (19)
  • notify/discord/discord.go
  • notify/email/email.go
  • notify/jira/jira.go
  • notify/mattermost/mattermost.go
  • notify/msteams/msteams.go
  • notify/msteamsv2/msteamsv2.go
  • notify/opsgenie/opsgenie.go
  • notify/pagerduty/pagerduty.go
  • notify/pagerduty/pagerduty_test.go
  • notify/pushover/pushover.go
  • notify/rocketchat/rocketchat.go
  • notify/slack/slack.go
  • notify/sns/sns.go
  • notify/telegram/telegram.go
  • notify/util.go
  • notify/victorops/victorops.go
  • notify/webex/webex.go
  • notify/webhook/webhook.go
  • notify/wechat/wechat.go

Comment thread notify/slack/slack.go
var (
data = notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmplText = notify.TmplText(n.tmpl, data, &err)
tmplText = notify.TmplTextWithLogger(n.tmpl, data, &err, logger)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Return template errors before sending the Slack request.

TmplTextWithLogger records failures in err, but Notify never checks err after rendering the request fields. Add an if err != nil { return false, err } check after the final template call and before JSON encoding/network delivery; otherwise a failure short-circuits subsequent fields to empty strings while the malformed request is still sent.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@notify/slack/slack.go` at line 68, In Notify, check err immediately after the
final TmplTextWithLogger call and before JSON encoding or Slack delivery; return
false and err when rendering fails, while preserving the existing request flow
when err is nil.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like a pre-existing issue, but would be nice to adress @mihir-dixit2k27

@TheMeier TheMeier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe a test for TmplTextWithLogger that checks that a log is actually emitted could be added.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@notify/util_test.go`:
- Line 294: Update the warning assertions in the affected tests around the
existing require.Contains checks to validate the complete payload in both text
and HTML log outputs. Assert that each output includes the template name and the
rendering error details in addition to the generic “template execution failed”
message.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e2d7c38c-c2d8-4be9-b07c-a86251b2b9ca

📥 Commits

Reviewing files that changed from the base of the PR and between 03850d4 and b283f86.

📒 Files selected for processing (1)
  • notify/util_test.go

Comment thread notify/util_test.go Outdated
Signed-off-by: Mihir Dixit <dixitmihir1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rendering notification templates should not silently swallow errors

2 participants