Skip to content

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

Open
holger-waschke wants to merge 1 commit into
prometheus:mainfrom
holger-waschke:hwa/add_template_function
Open

feat(template): Add addDuration template helper for time offsets#5550
holger-waschke wants to merge 1 commit 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
Signed-off-by: Holger Waschke <holger.waschke@dvag.com>
@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 Change StackReview 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: Advanced

Run ID: ec8aad9c-a29f-492c-8cd4-268c47c3236b

📥 Commits

Reviewing files that changed from the base of the PR and between 027f11a and c1fbbad.

📒 Files selected for processing (2)
  • template/template.go
  • template/template_test.go

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


📝 Walkthrough

Walkthrough

The template package adds an addDuration helper. It parses duration strings, adds valid durations to time.Time values, and returns Unix timestamps in milliseconds. Tests cover valid and invalid inputs.

Changes

Template duration support

Layer / File(s) Summary
Add and validate duration calculation
template/template.go, template/template_test.go
The addDuration helper parses durations, returns errors for invalid input, and returns adjusted timestamps in Unix milliseconds. Tests cover valid output and the exact invalid-input error.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: 🟡 Moderate · up to c1fbb

Templates using standard pipeline syntax may fail to render when calling the new timestamp-shifting helper. Correct the argument handling and add a pipeline regression test before merging.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the purpose, motivation, implementation changes, usage example, and tests. However, it omits the required Pull Request Checklist, issue information, documentation and sign-off… Add the required Pull Request Checklist with all applicable boxes marked, list related issues or state that none apply, provide the required documentation and sign-off confirmations, and complete the release-notes block with NONE or an appr…
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the template area and the new addDuration helper. It follows the repository's requested area-and-description format.
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.
Full details: Description check

Explanation

The description explains the purpose, motivation, implementation changes, usage example, and tests. However, it omits the required Pull Request Checklist, issue information, documentation and sign-off confirmations, best-practices confirmation, and release-notes section.

Resolution

Add the required Pull Request Checklist with all applicable boxes marked, list related issues or state that none apply, provide the required documentation and sign-off confirmations, and complete the release-notes block with NONE or an appropriate user-facing entry.

  • Fix all pre-merge checks with AI
✨ 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.

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.

1 participant