DataDog
このスニペットは、DataDogにIoTデータを送信する機能を提供します。
Resources used: HTTPS x 1
Details
send_datadog
関数は、HTTPS経由のAPIを使用して、渡された時系列データオブジェクトをDataDogの「metric」に「points」を POST します。結果(エラー/レスポンス)は、コールバック関数に渡されます。
このスニペットを使用するには、 DD_API_KEY
(DataDogアカウントからのAPI-KEY)と CA
をユーザーが設定する必要があります。
注意: 提供されている関数では、最大4KBまでのデータを取り扱うことができます。より大きなサイズのデータを取り扱う場合は、neqto.jsドキュメントのhttpsオブジェクト、分割書き込みを参考にしてください。
CAの取得方法はこちら。
var CA = "-----BEGIN CERTIFICATE-----\n...<CA>...\n-----END CERTIFICATE-----"
//=================================================================
// DATADOG SNIPPET
//=================================================================
//=================================================================
// The following configuration are MANDATORY. Set by user.
//=================================================================
// The API-KEY that allows reporting access to DataDog.
// eg. 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
var DD_API_KEY = '<YOUR_API-KEY>';
// Public certificate of the certificate authority that signed the DataDog server certificate for SSL/TLS handshake.
// eg. '-----BEGIN CERTIFICATE-----\n...<CA>...\n-----END CERTIFICATE-----'
var CA = '<YOUR_CA>';
//=================================================================
/**
* Post data to DataDog metric using API Key.
* https://docs.datadoghq.com/api/v1/metrics/#submit-metrics
* @function send_datadog
* @param {string} metric - Name of the metric to send to, as a String.
* @param {object} points - Time series data to be sent to DataDog server, as an Object (list of [timestamp, point] pairs). Timestamp must be in unix time (seconds).
* @param {function} callback - User callback to return the result (error/response).
* @returns {undefined}
*/
var send_datadog = function (metric, points, callback) {
/*
// The fields, "host", "interval", "tags", and "type" are optional. See documentation.
// Thus, they are commented.
*/
var body = JSON.stringify({
"series": [
{
// "host": 'NEQTO Device', // OPTIONAL
// "interval": 20, // OPTIONAL
// "tags": [ 'environment:example', 'source:neqto_device' ], // OPTIONAL
// "type": 'rate', // OPTIONAL
"metric": metric,
"points": points
}
]
});
var options = {
"method": 'POST',
"host": 'api.datadoghq.com',
"path": '/api/v1/series',
"headers": {
"DD-API-KEY": DD_API_KEY,
"Content-Type": 'application/json',
"Content-Length": body.length.toString()
},
"ca": CA
};
var request = https.request(options, function (response) {
response.on('end', function () {
callback(null, { "statusCode": response.statusCode, "statusMessage": response.statusMessage, "body": response.read() });
});
});
request.on('error', function () {
callback({ "errCode": request.errCode }, null);
});
request.end(body.toString(), function () {
print("[request] SUCCESS");
});
}
Function Usage Example
/*
<INSERT ABOVE SNIPPET HERE WITH SET CONFIGURATIONS>
*/
//=================================================================
log.setLevel(-1); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
//=================================================================
// MAIN SCENARIO
//=================================================================
/**
* Callback to fetch error/response from the request.
* @function callback
* @param {object} err - Error returned in case of a failed request. Has one property - `errCode`.
* @param {object} data - Response returned by a successfully completed request. Has three properties - `statusCode`, `statusMessage`, and `body`.
*/
var callback = function (err, data) {
if (err) {
print("[error]", err.errCode);
} else {
print("[status]", data.statusCode, data.statusMessage);
print("[response]", data.body);
}
}
var unix_time = Number(new Date().getTime()/1000)
var metric = "example.metric";
var points = [
[unix_time - 10, 1],
[unix_time, 2]
];
send_datadog(metric, points, callback);
上記に記載されている会社名、製品名は、各社の登録商標または商標です。
Updated: 2023-04-14