Skip to content

Instantly share code, notes, and snippets.

@mivano
Created October 31, 2018 09:20
Show Gist options
  • Save mivano/9865b9eea295677223461720cfd4e763 to your computer and use it in GitHub Desktop.
Save mivano/9865b9eea295677223461720cfd4e763 to your computer and use it in GitHub Desktop.
You can do this by using statuspage.io and running a webjob that every 5 minutes retrieves a metric from App Insights and pushes it to statuspage.io. Here’s a sample script for you that runs in my webjob.
// Application Insights Application ID and API key:
var appID = “YOUR APP ID”;
var apiKey = “YOUR API KEY”;
// Statuspage metric info
var api_key = “YOUR KEY”;
var page_id = “YOUR PAGE ID”;
var metric_id = YOUR METRIC ID
var api_base = “https://api.statuspage.io/v1”;;
var request1 = require(“request”);
var request2 = require(“request”);
// Get a time range of 1 minute ending 5 minutes ago
var d = new Date();
var n = d.getTime();
var d_start = new Date(n – 6*60*1000);
var d_end = new Date(n – 5*60*1000);
var timeRange = d_start.toISOString() + “/” + d_end.toISOString();
request1.get(“https://api.applicationinsights.io/beta/apps/”; + appID + “/metrics/request/duration?timespan=” + timeRange + “&api_key=” + apiKey,
function(error, response, body) {
console.log("\nStatus code (get to Application Insights): " + response.statusCode);
if(response.statusCode == 200){
//console.log(body);
data = JSON.parse(body);
requests = parseFloat(data.value[“request/duration”].avg)
console.log("\n\nRequest duration: " + requests + " ms");
ts = Math.round(Date.parse(data.value.end)/1000);
console.log("End time: " + data.value.end + " / " + ts + " seconds Unix time");
request2.post({
url: api_base + “/pages/” + page_id + “/metrics/” + metric_id + “/data.json”,
headers: {
Authorization: "OAuth " + api_key
},
json: {
data: {
timestamp: ts,
value: requests
}
}
}, function(error, response, body) {
if(response.statusCode == 201){
console.log("\nOK: status code (post to Statuspage): " + response.statusCode);
} else {
console.log("\nStatus code (post to Statuspage): " + response.statusCode);
}
});
} else {
console.log("\nExit due to Get status code = " + response.statusCode);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment