Skip to content

feat(template): Add addDuration template helper for time offsets - #5550

Open
holger-waschke wants to merge 2 commits into
prometheus:mainfrom
holger-waschke:hwa/add_template_function
Open

holger-waschke wants to merge 2 commits into
prometheus:mainfrom
holger-waschke:hwa/add_template_function

Conversation

@holger-waschke

Copy link
Copy Markdown
Contributor

Summary
Adds an addDuration helper function to Alertmanager templates. This allows templates to shift timestamps using Go duration strings (e.g., "-30m", "1h") and returns the resulting value as Unix milliseconds for direct use in links.

Why?
Before this change, achieving relative time windows required raw nanosecond arithmetic, which is hard to read and error-prone:

{{- $start := (.StartsAt.Add -1800000000000).UnixMilli -}}
{{- $end   := (.StartsAt.Add 3600000000000).UnixMilli -}}

With addDuration, the same logic becomes much more expressive and maintainable:

{{- $start := (addDuration .StartsAt "-30m") -}}
{{- $end   := (addDuration .StartsAt "60m") -}}
{{- $url   := printf "https://app.checklyhq.com/checks/%s?startTime=%d&endTime=%d" .Labels.check_id $start $end -}}
[Checkly Playwright Report|{{ $url }}]

Changes

  • Registered addDuration in the default template function map.
  • Added unit tests covering:
  • Valid duration offset parsing ("-30m", "1h").
  • Error handling for invalid duration strings.

@holger-waschke
holger-waschke requested a review from a team as a code owner September 10, 2026 10:13
@holger-waschke
holger-waschke force-pushed the hwa/add_template_function branch from a729d96 to c1fbbad Compare September 10, 2026 10:14
@coderabbitai

coderabbitai Bot commented Sep 10, 2026

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: prometheus/alertmanager/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 29b8ff1c-fcfe-4620-a720-22b6a408dba3

📥 Commits

Reviewing files that changed from the base of the PR and between b28b549 and 86aaf2f.

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

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

The template package adds an addDuration helper and changes YAML-rendered string handling. The helper returns Unix milliseconds or a parse error. DeepCopyWithTemplate conditionally decodes valid JSON strings.

Changes

Template behavior updates

Layer / File(s) Summary
Add and validate duration calculation
template/template.go, template/template_test.go
The addDuration helper parses durations, adds valid durations to time.Time values, and returns Unix timestamps in milliseconds. Tests cover valid output and the exact invalid-input error.
Preserve rendered string values
template/template.go
DeepCopyWithTemplate returns a decoded string scalar only when the rendered value is valid JSON. Other rendered strings remain unchanged, while structured YAML values continue through normalization.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 86aaf

The helper adds duration offsets for template-generated links and reports invalid durations safely. Current evidence supports merging.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: adding the addDuration template helper.
Description check ✅ Passed The description explains the feature, motivation, usage, implementation changes, and test coverage. It does not include the repository checklist or release-notes entry, but the core required change in…
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

@TheMeier

Copy link
Copy Markdown
Contributor

So if you flip the args addDuration(ds string, t time.Time) then both template forms would work

{{ addDuration "-30m" .StartsAt }}
{{ .StartsAt | addDuration "-30m" }}

If I read the coderabbit comment correctly

@fluktuid fluktuid 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.

@TheMeier If I understand your comment correctly, this is how it should look for best compatibility.

@holger-waschke hope this helps

Comment thread template/template.go Outdated
Comment thread template/template_test.go Outdated
Signed-off-by: Holger Waschke <holger.waschke@dvag.com>
@holger-waschke
holger-waschke force-pushed the hwa/add_template_function branch from c760ea4 to b28b549 Compare September 22, 2026 09:35
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.

3 participants