Skip to content

Commit 21e5044

Browse files
More improvements
1 parent caffbc1 commit 21e5044

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/utils.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const jsdiff = require('diff');
22

33
const segmentString = (string) => {
4-
return Array.from(string.matchAll(/[a-z0-9-]+|[^a-z0-9-]+/gi).map(match => match[0]));
4+
return Array.from(string.matchAll(/[a-z0-9.-]+|[^a-z0-9.-]+/gi).map(match => match[0]));
55
}
66

77
const diffSegmented = (left, right) => {
@@ -432,6 +432,9 @@ const utils = {
432432
.map(alert => {
433433
alert = {...alert};
434434
alert.summary = alert.annotations.summary || alert.labels.alertname;
435+
if (alert.labels.env) {
436+
alert.summary += ` (${alert.labels.env})`;
437+
}
435438
if (!alert.labels.logs_url && !alert.labels.logs_template) {
436439
for (const labelSet of [
437440
["env", "cluster_id", "namespace", "pod"],
@@ -605,9 +608,20 @@ const utils = {
605608
.map(alert => alert.annotations.dashboard_url)
606609
.filter(Boolean));
607610
let dashboardNum = 1;
608-
for (const dashboardURL of dashboardURLs) {
611+
for (let dashboardURL of dashboardURLs) {
612+
// For now, we'll only support replacing labels with a
613+
// single value. In future it would be straightforward to
614+
// duplicate query parameters in the URL to support
615+
// passing all the values to the dashboard.
616+
const alerts = data.alerts.filter(
617+
alert => alert.annotations.dashboard_url === dashboardURL,
618+
);
619+
dashboardURL = dashboardURL.replace(/\$([a-z0-9_]+)/g, (_, label) => {
620+
const values = new Set(alerts.map(alert => alert.labels[label]).filter(Boolean));
621+
return values.size > 0 ? [...values][0] : `$` + label;
622+
});
609623
const name = dashboardURLs.size > 1 ? `Dashboard ${dashboardNum}` : "Dashboard";
610-
urls.push(`<a href="${dashboardURL}>🚦 ${name}</a>`);
624+
urls.push(`<a href="${dashboardURL}">🚦 ${name}</a>`);
611625
dashboardNum += 1;
612626
}
613627

@@ -617,7 +631,7 @@ const utils = {
617631
let runbookNum = 1;
618632
for (const runbookURL of runbookURLs) {
619633
const name = runbookURLs.size > 1 ? `Runbook ${runbookNum}` : "Runbook";
620-
urls.push(`<a href="${runbookURL}>🗒️ ${name}</a>`);
634+
urls.push(`<a href="${runbookURL}">🗒️ ${name}</a>`);
621635
}
622636

623637
let logsURLs = new Set(data.alerts
@@ -642,7 +656,7 @@ const utils = {
642656
alert.annotations.logs_datasource || defaultDatasource === logsDatasource
643657
),
644658
);
645-
const expr = logsTemplate.replace(/=~?"\$([a-z0-9_]+)"/g, function(_, label) {
659+
const expr = logsTemplate.replace(/=~?"\$([a-z0-9_]+)"/g, (_, label) => {
646660
const values = new Set(alerts.map(alert => alert.labels[label]).filter(Boolean));
647661
const regex = values.size > 0 ? [...values].join("|") : ".+";
648662
return `=~"${regex}"`;

0 commit comments

Comments
 (0)