Skip to content

Commit

Permalink
upgrade for new grafana alert
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Feb 11, 2022
1 parent 65ac357 commit 9e49234
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
22 changes: 0 additions & 22 deletions docs/example.json

This file was deleted.

21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
26 changes: 10 additions & 16 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | undefined>
annotations: Record<string, string | undefined>
dashboardURL: string
}

0 comments on commit 9e49234

Please sign in to comment.