Skip to content

Commit 9f9a1b9

Browse files
committed
refactor: NO-JIRA: Simplify/improve alert matching code and error handling.
Corrections to the fix for #152
1 parent 2ac11d6 commit 9f9a1b9

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

web/src/__tests__/alert.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('AlertNode.fromURL', () => {
99
'container=bad-deployment&endpoint=https-main&job=kube-state-metrics&namespace=default&pod=bad-deployment&' +
1010
'reason=CrashLoopBackOff&service=kube-state-metrics&uid=00000000-0000-0000-0000-000000000000',
1111
query:
12-
'alert:alert:{"prometheus":"openshift-monitoring/k8s","severity":"warning","alertname":"KubePodCrashLooping","container":"bad-deployment",' +
12+
'alert:alert:{"severity":"warning","alertname":"KubePodCrashLooping","container":"bad-deployment",' +
1313
'"endpoint":"https-main","job":"kube-state-metrics","namespace":"default","pod":"bad-deployment",' +
1414
'"reason":"CrashLoopBackOff","service":"kube-state-metrics","uid":"00000000-0000-0000-0000-000000000000"}',
1515
},

web/src/components/topology/LoadingTopology.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as React from 'react';
21
import {
32
action,
43
BreadthFirstLayout,
@@ -18,6 +17,7 @@ import {
1817
VisualizationProvider,
1918
VisualizationSurface,
2019
} from '@patternfly/react-topology';
20+
import * as React from 'react';
2121

2222
const baselineComponentFactory: ComponentFactory = (kind: ModelKind, type: string) => {
2323
switch (type) {
@@ -75,6 +75,7 @@ export const LoadingTopology: React.FC = () => {
7575
resetViewCallback: action(() => {
7676
controller.getGraph().reset();
7777
controller.getGraph().layout();
78+
controller.getGraph().fit(30);
7879
}),
7980
legend: false,
8081
zoomInDisabled: true,

web/src/korrel8r/alert.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@ export class AlertDomain extends Domain {
1414

1515
// Convert a Query to a relative URI reference.
1616
linkToQuery(link: URIRef): Query {
17-
const m = link.pathname.match(/monitoring\/(?:alerts|alertrules)(?:\/(.*))?$/);
17+
const m = link.pathname.match(/monitoring\/(?:alerts|alertrules)(?:\/([^/]*))?/);
1818
if (!m) throw this.badLink(link);
1919
const ruleID = m?.[1];
20-
let selector: { alertname?: string };
20+
let selector: { [key: string]: string };
2121
if (ruleID) {
2222
// Search for alerts belonging to a specific alerting rule.
2323
selector = Object.fromEntries(link.searchParams);
24-
// Get the name from the path if not found in search parameters.
25-
if (!selector.alertname) {
26-
const name = this?.idToName?.get(ruleID);
27-
if (name) selector.alertname = name;
28-
}
24+
selector['alertname'] ||= this?.idToName?.get(ruleID); // Look up name from ID if missing
2925
// Must have an alertname for a specific rule search.
30-
if (!selector.alertname) throw this.badLink(link, 'cannot find alertname');
26+
if (!selector['alertname']) throw this.badLink(link, 'cannot find alertname');
27+
nonLabelParams.forEach((key: string) => delete selector[key]);
3128
} else {
3229
// Generic alert search across rules. Empty selector is allowed - means "all alerts"
3330
selector = parseKeyValueList(link.searchParams.get('alerts'));
@@ -36,7 +33,20 @@ export class AlertDomain extends Domain {
3633
}
3734

3835
queryToLink(query: Query): URIRef {
39-
const selectors = keyValueList(JSON.parse(query.selector));
40-
return new URIRef(`monitoring/alerts`, { alerts: selectors || undefined });
36+
// Use "alerts" parameter to search for alerts of possibly mixed alertnames.
37+
try {
38+
const selectors = keyValueList(JSON.parse(query.selector));
39+
return new URIRef(`monitoring/alerts`, { alerts: selectors || undefined });
40+
} catch (e) {
41+
throw this.badQuery(query, e.toString());
42+
}
4143
}
4244
}
45+
46+
// URL parameters that are not alert labels, remove them from the query.
47+
const nonLabelParams = new Set<string>([
48+
'prometheus',
49+
'rowFilter-alert-state',
50+
'rowFilter-alert-source',
51+
'rowFilter-alerting-rule-source',
52+
]);

0 commit comments

Comments
 (0)