我正在寻找一个关于如何通过 ajax 将指标推送到 pushgateway 的示例。
echo 'some_metric 42' | curl --user user:pass --data-binary @- https://example.com/metrics/job/author_monitoring/jobname/count_open
卷曲它完美无缺!
我不知道如何在 js/jquery 中翻译它。也许有人有一个例子
这是我到目前为止得到的。
(function ($, $document) {
"use strict";
function textToBin(text) {
return (
Array
.from(text)
.reduce((acc, char) => acc.concat(char.charCodeAt().toString(2)), [])
.map(bin => '0'.repeat(8 - bin.length) + bin)
.join(' ')
);
}
var username = "user";
var password = "pass";
var metric = 'some_metric 42';
var binaryData = textToBin(metric);
$.ajax({
url: "https://example.com/metrics/job/author_monitoring/jobname/count_open",
data: binaryData,
type: 'POST',
crossDomain: true,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
},
success: function () {
console.log("Success");
},
error: function () {
console.log('Failed!');
}
});
})($, $(document));
这是错误:
text format parsing error in line 1: invalid metric name