Skip to content

Instantly share code, notes, and snippets.

@shadowlik
Last active December 20, 2021 21:18
Show Gist options
  • Save shadowlik/f5177d6a59bf5185495d704c5bbb772e to your computer and use it in GitHub Desktop.
Save shadowlik/f5177d6a59bf5185495d704c5bbb772e to your computer and use it in GitHub Desktop.

Google Cloud Monitoring/Alert Notification Webhook to Discord

I was struggling to find the webhook API documentation, if someone know where it is, please share!

I've created a simple google cloud function (index.js) with NodeJS to push the monitoring alert to Discord.

You can also find a webhook body data as an example and a typescript interface.

export interface Body {
incident: Incident;
version: string;
}
export interface Incident {
observed_value: string;
metadata: Metadata;
resource_id: string;
scoping_project_id: string;
resource: Resource;
started_at: number;
scoping_project_number: number;
url: string;
policy_name: string;
resource_type_display_name: string;
ended_at: null | string;
metric: Metric;
incident_id: string;
condition: Condition;
condition_name: string;
summary: string;
resource_name: string;
state: string;
threshold_value: string;
}
export interface Condition {
conditionThreshold: ConditionThreshold;
displayName: string;
name: string;
}
export interface ConditionThreshold {
aggregations: Aggregation[];
trigger: Trigger;
duration: string;
comparison: string;
filter: string;
thresholdValue: number;
}
export interface Aggregation {
groupByFields: string[];
alignmentPeriod: string;
crossSeriesReducer: string;
perSeriesAligner: string;
}
export interface Trigger {
count: number;
}
export interface Metadata {
system_labels: SystemLabelsClass;
user_labels: SystemLabelsClass;
}
export interface SystemLabelsClass {
}
export interface Metric {
labels: SystemLabelsClass;
displayName: string;
type: string;
}
export interface Resource {
type: string;
labels: ResourceLabels;
}
export interface ResourceLabels {
host: string;
project_id: string;
}
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
const axios = require('axios');
const DISCORD_WEBHOOK='https://discord.com/api/webhooks/921049208423858177/r2EAEDdVIGrrpz4rYmjwX1E8U_Z4kEZQpUe3dKa2mafIOffEtXVdbzm0D-HRkauO8Brh';
const COLORS = {
red: 15548997,
green: 5763719,
yellow: 16705372,
}
exports.run = async (req, res) => {
const { incident } = req.body;
const {
summary,
condition_name,
policy_name,
started_at,
scoping_project_id,
resource_type_display_name,
url,
ended_at } = incident;
const color = ended_at ? COLORS.green : COLORS.red;
const unixTimestamp = ended_at || started_at;
const timestamp = new Date(unixTimestamp * 1000).toISOString();
const discordBody = {
title: condition_name,
description: summary,
url,
color,
timestamp,
};
try {
const { data } = await axios.post(DISCORD_WEBHOOK, {
"content": resource_type_display_name,
embeds: [discordBody]
});
console.log(data);
} catch (error) {
console.log(error.response, error.response.data);
} finally {
res.status(200).send();
}
};
{
"incident": {
"observed_value": "4.000",
"metadata": {
"system_labels": {},
"user_labels": {}
},
"resource_id": "",
"scoping_project_id": "project-id",
"resource": {
"type": "uptime_url",
"labels": {
"host": "example.com",
"project_id": "project-id"
}
},
"started_at": 1640031275,
"scoping_project_number": 766016839238,
"url": "https://console.cloud.google.com/monitoring/alerting/incidents/0.mb2gdsa5dkwcn4?project=project-id",
"policy_name": "stage-api.loyall.com.br uptime failure",
"resource_type_display_name": "Uptime Check URL",
"ended_at": null,
"metric": {
"labels": {},
"displayName": "Check passed",
"type": "monitoring.googleapis.com/uptime_check/check_passed"
},
"incident_id": "0.mb2g35dkwcn4",
"condition": {
"conditionThreshold": {
"aggregations": [
{
"groupByFields": [
"resource.label.*"
],
"alignmentPeriod": "1200s",
"crossSeriesReducer": "REDUCE_COUNT_FALSE",
"perSeriesAligner": "ALIGN_NEXT_OLDER"
}
],
"trigger": {
"count": 1
},
"duration": "60s",
"comparison": "COMPARISON_GT",
"filter": "metric.type=\"monitoring.googleapis.com/uptime_check/check_passed\" AND metric.label.check_id=\"example.com.br\" AND resource.type=\"uptime_url\"",
"thresholdValue": 1
},
"displayName": "Failure of uptime check_id example.com.br",
"name": "projects/project-id/alertPolicies/16385515134911267361/conditions/16385515134911269630"
},
"condition_name": "Failure of uptime check_id example.com.br",
"summary": "An uptime check on project-id Uptime Check URL labels {project_id=project-id, host=example.com} is failing.",
"resource_name": "project-id Uptime Check URL labels {project_id=project-id, host=example.com}",
"state": "open",
"threshold_value": "1"
},
"version": "1.2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment