0

我正在寻找一个关于如何通过 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
4

1 回答 1

0

好吧,我明白了。

有一个简单的解决方案,导入是\n在字符串的末尾。

(function ($, $document) {
  "use strict";
  var username = "user";
  var password = "pass";
  var metric = 'some_metric 42\n';

  $.ajax({
    url: "https://example.com/metrics/job/author_monitoring/jobname/count_open",
    data: metric,
    type: 'POST',
    beforeSend: function (xhr) {
      xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
      xhr.setRequestHeader("Content-Type", "text/plain");
    },
    success: function () {
      console.log("Success");
    },
    error: function () {
      console.log('Failed!');
    }
  });
})($, $(document));
于 2019-03-07T08:34:25.323 回答