Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.