Skip to content

Instantly share code, notes, and snippets.

@kohlerdominik
Last active December 15, 2020 18:54
Show Gist options
  • Save kohlerdominik/544ce6476a15e153ab492540d5151900 to your computer and use it in GitHub Desktop.
Save kohlerdominik/544ce6476a15e153ab492540d5151900 to your computer and use it in GitHub Desktop.
Google Cloud Function to parse and send a Stackdriver Incident to Google Chat (Hangouts)
/*
Example Request
{
"incident":{
"incident_id":"0.123",
"resource_id":"",
"resource_name":"some-resource-name",
"resource":{
"type":"gce_instance",
"labels":{
"instance_id":"123",
"zone":"some-zone-a"
}
},
"started_at":1579625776,
"policy_name":"VM instance",
"condition_name":"GCE VM Instance - CPU utilization",
"url":"https://app.google.stackdriver.com/incidents/123?project=my-project",
"state":"open",
"ended_at":null,
"summary":"CPU utilization for some-resource with metric labels [...] is above the threshold of 0.01 with a value of 0.027."
},
"version":"1.2"
}
*/
exports.sendToChat = function sendToChat (req, res) {
var uri = 'https://chat.googleapis.com/v1/spaces/[...]';
var body = {
"cards":[
{
"header":{
"title":"Stackdriver Monitoring Alert"
},
"sections":[
{
"widgets":[
{
"keyValue":{
"topLabel":"Summary",
"content":req.body.incident.summary,
"contentMultiline":"true"
}
},
{
"keyValue":{
"topLabel":"Policy",
"content":req.body.incident.policy_name
}
},
{
"keyValue":{
"topLabel":"Condition",
"content":req.body.incident.condition_name
}
},
{
"keyValue":{
"topLabel":"Resource",
"content":req.body.incident.resource_name + " (" + req.body.incident.resource_id + ")"
}
}
]
},
{
"widgets":[
{
"buttons":[
{
"textButton":{
"text":"Show in Console",
"onClick":{
"openLink":{
"url":req.body.incident.url
}
}
}
}
]
}
]
}
]
}
]
};
const axios = require('axios');
axios.post(uri,body);
};
{
"dependencies": {
"axios": "^0.19.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment