Created
October 25, 2012 18:25
-
-
Save clutchski/3954487 to your computer and use it in GitHub Desktop.
submit a metric to datadog with node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https'); | |
var API_KEY = 'YOUR_API_KEY_HERE'; | |
var d = new Date(); | |
var n = d.getTime(); | |
console.log( "date is : " + d ); | |
console.log( "time is : " + n ); | |
var ddata = { "series" : | |
[{"metric":"test.metric", | |
"points":[[d.getTime()/1000, 40]], | |
"type":"gauge", | |
"host":"test.example.com", | |
"tags":["environment:test"]} | |
] | |
}; | |
var data = JSON.stringify(ddata); | |
var myheaders = { | |
"Content-Length" : Buffer.byteLength(data, "utf-8"), | |
"Content-Type" : "application/json" | |
}; | |
var options = { | |
host : 'app.datadoghq.com', | |
port : 443, | |
path : '/api/v1/series?api_key=' + API_KEY, | |
method : 'POST', | |
headers : myheaders | |
} | |
function sendToDataDog( JsonMetric ) { | |
var req = https.request(options, function(res) { | |
console.log('STATUS: ' + res.statusCode); | |
console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
// write data to request body | |
req.setSocketKeepAlive(true); | |
req.write(JsonMetric); | |
req.end(); | |
} | |
for( var i = 0; i < 1; i++) { | |
sendToDataDog( data ); | |
console.log("sending Metric to DataDog"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment