diff --git a/docs/example.json b/docs/example.json deleted file mode 100644 index 0d2bce6..0000000 --- a/docs/example.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "dashboardId": 1, - "evalMatches": [ - { - "value": 1, - "metric": "Count", - "tags": {} - } - ], - "imageUrl": "https://grafana.com/assets/img/blog/mixed_styles.png", - "message": "Notification Message", - "orgId": 1, - "panelId": 2, - "ruleId": 1, - "ruleName": "Panel Title alert", - "ruleUrl": "http://localhost:3000/d/hZ7BuVbWz/test-dashboard?fullscreen\u0026edit\u0026tab=alert\u0026panelId=2\u0026orgId=1", - "state": "alerting", - "tags": { - "tag name": "tag value" - }, - "title": "[Alerting] Panel Title alert" -} diff --git a/src/index.ts b/src/index.ts index 1f3cb13..9fefc21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { AlertRequest } from './types' +import { Alert, AlertRequest } from './types' function getProp(key: string) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -9,12 +9,25 @@ const WEBHOOK_ID = 'e9b36487-e723-4ed3-b78d-45d72f1f599d' const WEBHOOK_SECRET = getProp('WEBHOOK_SECRET') function doPost(e: GoogleAppsScript.Events.DoPost) { - const params: AlertRequest = JSON.parse(e.postData.contents) - const description = params.message ? `${params.message}\n` : `\n` - const text = `## [${params.title}](${params.ruleUrl})\n` + `${description}` + const req: AlertRequest = JSON.parse(e.postData.contents) + const alertMessages = req.alerts.map(alertToMessage) + const text = alertMessages.join('\n\n') sendMessage(text) } +function alertToMessage(alert: Alert) { + const title = `## ${alert.labels.alertname ?? 'Blank alert name'}` + const message = alert.annotations.message ?? 'Blank message' + const link = + alert.dashboardURL !== '' ? `[dashboard link](${alert.dashboardURL})` : '' + + return ` +${title} +${message} +${link} +`.trim() +} + function sendMessage(message: string) { const signature = Utilities.computeHmacSignature( Utilities.MacAlgorithm.HMAC_SHA_1, diff --git a/src/types.ts b/src/types.ts index e0d272e..ff50595 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,20 +1,14 @@ +/** + * @see https://grafana.com/docs/grafana/latest/alerting/unified-alerting/contact-points/#webhook + */ export interface AlertRequest { - dashbordId: number - evalMatches: EvalMatches[] - imageUrl: string - message: string - orgId: number - manelId: number - ruleId: number - ruleName: string - ruleUrl: string - state: string - tags: unknown - title: string + status: string + alerts: Alert[] } -interface EvalMatches { - value: number - metric: string - tags: unknown +export interface Alert { + status: string + labels: Record + annotations: Record + dashboardURL: string }