-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprometheus.ts
More file actions
68 lines (60 loc) · 1.7 KB
/
prometheus.ts
File metadata and controls
68 lines (60 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { is } from 'typia';
import { a, cond, fmt, ul } from '../../src/formatting/formatting';
import { WebhookMessage, PluginBase } from '../../src/pluginApi/v2';
type AlertStatus = 'resolved' | 'firing';
export interface PrometheusWebhook {
version: '4';
groupKey: string;
truncatedAlerts: number;
receiver: string;
status: AlertStatus;
groupLabels: Record<string, string>;
commonLabels: Record<string, string>;
commonAnnotations: Record<string, string>;
externalURL: string;
alerts: Alert[];
}
interface Alert {
status: AlertStatus;
labels: Record<string, string>;
annotations: Record<string, string>;
startsAt: string;
endsAt: string;
generatorURL: string;
fingerprint: string;
}
const alertStatusIcon: Record<AlertStatus, string> = {
resolved: '🟢',
firing: '🔴',
};
export const format = 'prometheus';
export default class WebhookPlugin extends PluginBase {
public async init() { }
public async transform(body: unknown): Promise<WebhookMessage | undefined> {
if (!is<PrometheusWebhook>(body)) {
return undefined;
}
const icon = alertStatusIcon[body.status];
const makeAlert = (alert: Alert) =>
fmt(
alert.annotations.summary as string,
' (',
a(alert.generatorURL, 'view'),
')',
);
return {
username: 'Prometheus',
icon: {
url: 'https://prometheus.io/assets/favicons/android-chrome-192x192.png',
},
text: fmt(
icon,
` ${body.alerts.length} alert`,
cond(body.alerts.length > 1, 's'),
` ${body.status}`,
cond(body.truncatedAlerts > 0, ` (${body.truncatedAlerts} truncated)`),
ul(...body.alerts.map(makeAlert)),
),
};
}
}