Skip to content

Add custom alert URL annotation#7

Merged
lotldev merged 3 commits into
masterfrom
custom-alert-url
Jul 20, 2026
Merged

Add custom alert URL annotation#7
lotldev merged 3 commits into
masterfrom
custom-alert-url

Conversation

@lotldev

@lotldev lotldev commented Jul 20, 2026

Copy link
Copy Markdown
  • autoformatted
  • add alert_url annotation for grafana alerts outside of the default datasource, or alerts eventually generated outside of grafana

@indent-zero

indent-zero Bot commented Jul 20, 2026

Copy link
Copy Markdown
PR Summary

Adds support for a per-alert annotations.alert_url that overrides the primary "📈 Alert link" in formatAlert, and now also surfaces those custom URLs as aggregated "Alert link" entries in the grouped formatAlerts output. Also applies whitespace/style cleanup.

  • formatAlert (per-alert): uses annotations.alert_url as the link when present, skipping the Grafana Explore rewrite (if / else if structure).
  • formatAlerts (grouped, RESPECT_GROUPBY=1): aggregates unique alert_url values into urls (Alert link or Alert link N when multiple), and omitAnnotation suppresses the annotation from the details listing since it's now emitted as a link.
  • Whitespace/style cleanup across the file (if(if (, ternary indentation, spread spacing, function(function ().

Issues

All clear! No issues remaining. 🎉

3 issues already resolved
  • Duplicated alert_url predicate: the Grafana branch re-checks !data.annotations.hasOwnProperty("alert_url") immediately after the if that sets url from it. Use else if (matching the logs_url / logs_template pattern a few lines below) so a future edit to one branch can't drift from the other. (fixed by commit a090a67)
  • In grouped mode, alerts with alert_url are still included in the generatorURLs aggregation, so when GRAFANA_URL + GRAFANA_DATASOURCE are set the message emits both the custom "Alert link" and a Grafana-derived "Alert query" for the same alert. This diverges from formatAlert (per-alert), where alert_url fully replaces the link — filter out alerts with an alert_url before building generatorURLs to keep behavior consistent. (fixed by commit 5926605)
  • alert_url is silently dropped in grouped mode (RESPECT_GROUPBY=1): it was added to omitAnnotation in formatAlerts but no branch there emits it as a link, so the custom URL disappears entirely — no link, no annotation entry. Either render it as a link in formatAlerts (like dashboard_url/runbook_url) or leave it out of omitAnnotation so it still shows in the annotation list. (fixed by commit a090a67)

CI Checks

Waiting for CI checks...

@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 41467ff9-d0c4-4b2f-8665-eb6619472e95

📥 Commits

Reviewing files that changed from the base of the PR and between a090a67 and 5926605.

📒 Files selected for processing (1)
  • src/utils.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/utils.js

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added support for custom alert destinations via the alert_url annotation, which overrides automatically generated Explore links.
    • Custom “Alert link” destinations are now aggregated and deduplicated in the Alerts output.
  • Bug Fixes

    • Hidden alert_url metadata no longer shows in expanded alert details (both common and per-alert views).
    • Updated alert aggregation behavior so alerts with alert_url no longer affect the generated “Alert query” links.

Walkthrough

Changes

Alert formatting now supports an alert_url annotation override, excludes that annotation from rendered details, aggregates unique alert links, and preserves existing optional URL behavior.

Alert formatting

Layer / File(s) Summary
Alert link overrides and annotation rendering
src/utils.js
alert_url overrides the primary alert link and suppresses Grafana URL generation; annotation details omit alert_url, while optional dashboard, runbook, and log links remain supported.
Alert URL aggregation and formatting cleanup
src/utils.js
Unique alert URLs are added to aggregated links, alerts with explicit links are excluded from Grafana query generation, and existing URL collection, silence filters, cloning, mappings, string merging, and Grafana calculations are reformatted or deduplicated.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a custom alert URL annotation.
Description check ✅ Passed The description is directly related to the changes and explains the new alert_url annotation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch custom-alert-url

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


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

Comment thread src/utils.js
Comment thread src/utils.js Outdated

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/utils.js (1)

517-524: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Preserve custom alert links in grouped output.

formatAlerts now omits alert_url from annotation details, but never adds those URLs to urls. Grouped alerts therefore lose the requested custom link and may still show an unrelated generated Grafana query link. Aggregate unique alert_url values and exclude overridden alerts from generator-URL aggregation.

🤖 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 `@src/utils.js` around lines 517 - 524, Update formatAlerts so grouped output
aggregates unique custom alert_url annotation values into urls. Exclude alerts
with an explicit alert_url from generated Grafana query URL aggregation, while
preserving generated URLs for alerts without overrides and the existing
annotation omission behavior.
🤖 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 `@src/utils.js`:
- Line 336: Update the GRAFANA_URL condition in the surrounding annotation-link
logic to use the file’s established truthy presence check instead of comparing
against an empty string. Preserve the existing alert_url guard and link-building
behavior when GRAFANA_URL is configured.

---

Outside diff comments:
In `@src/utils.js`:
- Around line 517-524: Update formatAlerts so grouped output aggregates unique
custom alert_url annotation values into urls. Exclude alerts with an explicit
alert_url from generated Grafana query URL aggregation, while preserving
generated URLs for alerts without overrides and the existing annotation omission
behavior.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 03bce045-1bd3-4b85-8b76-09f00d14f1a2

📥 Commits

Reviewing files that changed from the base of the PR and between 5d02c90 and 992eaa0.

📒 Files selected for processing (1)
  • src/utils.js
📜 Review details
🔇 Additional comments (1)
src/utils.js (1)

213-217: LGTM!

Also applies to: 333-335, 396-398, 407-409, 429-443, 454-458, 486-488, 583-588, 624-628, 636-640, 657-674

Comment thread src/utils.js Outdated

@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 `@src/utils.js`:
- Around line 609-617: Update the grouped alert-link generation around the
alertURLs collection and existing generatorURL loop to exclude alerts whose
annotations.alert_url is present from Grafana grouping. Preserve the custom
alert_url links, while ensuring overridden alerts do not also produce default
“Alert query” links when Grafana is configured.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e6430fee-e12c-4456-b698-c85f33f6fc68

📥 Commits

Reviewing files that changed from the base of the PR and between 992eaa0 and a090a67.

📒 Files selected for processing (1)
  • src/utils.js
📜 Review details
🔇 Additional comments (2)
src/utils.js (2)

333-335: Use a truthy check for GRAFANA_URL (duplicate of the prior review).

process.env.GRAFANA_URL != "" is true when the variable is unset, so this branch can construct an undefined/explore... link. Use the established truthy check instead.


216-217: LGTM!

Also applies to: 395-395, 406-406, 428-440, 456-456, 485-487, 519-519, 586-586, 634-635, 645-647, 667-683

Comment thread src/utils.js
Comment on lines +609 to +617
const alertURLs = new Set(data.alerts
.map(alert => alert.annotations.alert_url)
.filter(Boolean));
let alertURLNum = 1;
for (const alertURL of alertURLs) {
const name = alertURLs.size > 1 ? `Alert link ${alertURLNum}` : "Alert link";
urls.push(`<a href="${alertURL}">📈 ${name}</a>`);
alertURLNum += 1;
}

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

Honor alert_url as an override in grouped mode.

The per-alert path suppresses Grafana URL generation when alert_url exists, but this new grouped link block does not exclude those alerts from the existing generatorURL loop. With Grafana configured, grouped alerts can therefore show both the custom external link and an incorrect/default-datasource “Alert query” link. Filter overridden alerts out of the Grafana groups as well.

🤖 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 `@src/utils.js` around lines 609 - 617, Update the grouped alert-link
generation around the alertURLs collection and existing generatorURL loop to
exclude alerts whose annotations.alert_url is present from Grafana grouping.
Preserve the custom alert_url links, while ensuring overridden alerts do not
also produce default “Alert query” links when Grafana is configured.

Comment thread src/utils.js

@radon-at-beeper radon-at-beeper 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.

So this makes it so that you can set an alert_url annotation, and if that is present, it will be displayed in place of the Grafana link in the Matrix message? Sounds good.

@lotldev
lotldev merged commit d1a74e5 into master Jul 20, 2026
4 checks passed
@lotldev
lotldev deleted the custom-alert-url branch July 20, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants