Skip to content

Instantly share code, notes, and snippets.

@soudai
Created December 29, 2017 09:12
Show Gist options
  • Save soudai/be7205b297badb2fe0aca81cfd05acd7 to your computer and use it in GitHub Desktop.
Save soudai/be7205b297badb2fe0aca81cfd05acd7 to your computer and use it in GitHub Desktop.
Send Endpoint JSON of Google Apps Script to Mackerel service metric
var scriptProp = PropertiesService.getScriptProperties().getProperties();
var mackerelApiKey = scriptProp.MACKEREL_API_KEY;
var serviceName = scriptProp.MACKEREL_SERVICE_NAME;
var BaseURL = scriptProp.BASE_URL;
function main() {
var mackerelPostRes = postMackerelServiceMetric(mackerelApiKey, serviceName, getEndpointJSON());
}
function postMackerelServiceMetric(apiKey, serviceName, payload) {
var url = "https://mackerel.io/api/v0/services/" + serviceName + "/tsdb";
return UrlFetchApp.fetch(
url,
{
"contentType" : "application/json",
"method" : "post",
"headers" : {
"X-Api-Key" : apiKey
},
"payload" : JSON.stringify(JSON.parse(payload)),
"muteHttpExceptions" : true
}
);
}
function getEndpointJSON() {
var url = BaseURL;
var options = {
'method' : 'get',
'muteHttpExceptions' : true
}
Logger.log(url);
Logger.log(UrlFetchApp.fetch(url, options));
return UrlFetchApp.fetch(url, options);
}
@soudai
Copy link
Author

soudai commented Dec 29, 2017

以下のようなフォーマットでJSONを吐くエンドポイントを作ればそれをMackerelのサービスメトリックに投稿してくれる。

[{
    name: "Custom.name",
    time: 1514537878,
    value: 10
}, {
    name: "Custom.age",
    time: 1514537878,
    value: 15
}]

PHPだと以下のような感じ

<?php
$time = time();
$list = [
                ["name" => "Custom.name", "time" => $time, "value" => 10],
                ["name" => "Custom.age", "time" => $time, "value" => 15]
        ];

echo json_encode($list);

nameは Custom の部分がグラフ名で . 以降がグラフのラベル名になる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment