【问题标题】:Prometheus Pushgateway from Browser JS/AJAX来自浏览器 JS/AJAX 的 Prometheus Pushgateway
【发布时间】:2019-03-07 08:07:50
【问题描述】:

我正在寻找一个关于如何通过 ajax 将指标推送到 pushgateway 的示例。

echo 'some_metric 42' | curl --user user:pass --data-binary @- https://example.com/metrics/job/author_monitoring/jobname/count_open

使用 curl 可以完美运行!

我不知道如何在 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

【问题讨论】:

  • 好吧,一方面,CORS doesn't work like that。但是从您的问题中不清楚您是否遇到了 CORS 问题或其他问题。在任何情况下,删除标题。
  • 我猜这不是 cors 错误,pushgateway 返回 400。text format parsing error in line 1: invalid metric name

标签: javascript client prometheus prometheus-pushgateway


【解决方案1】:

好的,我明白了。

有一个简单的解决方案,导入是字符串末尾的\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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多