将Prometheus PushGateway与OpenCensus Java Client一起使用

时间:2019-07-11 18:09:03

标签: prometheus opencensus prometheus-java

我正在运行一个CronJob,我想将OpenCensus集成到其中以导出到Prometheus。但是,目前,我必须在完成工作后增加1分钟的睡眠时间,以确保Prometheus取消了我的指标。

如果可能的话,我想使用Prometheus PushGateway避免额外的睡眠,但是我不知道如何将其连接到OpenCensus。

这里是提及它的文档:https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/prometheus-它表示以下内容:

public class MyMainClass {
  public static void main(String[] args) {
    // Creates a PrometheusStatsCollector and registers it to the default Prometheus registry.
    PrometheusStatsCollector.createAndRegister();

    // Uses a simple Prometheus HTTPServer to export metrics. 
    // You can use a Prometheus PushGateway instead, though that's discouraged by Prometheus:
    // https://prometheus.io/docs/practices/pushing/#should-i-be-using-the-pushgateway.
    io.prometheus.client.exporter.HTTPServer server = 
      new HTTPServer(/*host*/ "localhost", /*port*/  9091, /*daemon*/ true);

    // Your code here.
    // ...
  }
}

但是,没有关于如何在OpenCensus中实际使用它的示例。有人做过吗?如何做?

1 个答案:

答案 0 :(得分:0)

我认为您可能[不想|不能]将OpenCensus用于您想做的事情。

OpenCensus做什么

OpenCensus将指标推送到例如Stackdriver,Datadog等。但是,对于Prometheus来说,请求-响应流是反转的,并且度量通常是从以Prometheus博览会格式呈现度量的端点中提取的。

OpenCensus的Prometheus导出器(对于Java,您引用的示例)将创建一个HTTP服务器,您将在其中倒入度量标准(请参见this示例,因为原理相同)。

Prometheus(或其他了解Prometheus度量标准格式的服务)随后将定期抓取该端点并提取度量标准。

Prometheus Pushgateway的作用

您的Cron作业将发出必须被推送到例如普罗米修斯Pushgateway。在这种体系结构中,Pushgateway 替换 OpenCensus Prometheus Exporter端点。

您所需要的-可能只是添加到cron作业中的curl语句而已-将会生成HTTP请求并将其发送到Pushgateway。参见:

https://github.com/prometheus/pushgateway#command-line https://github.com/prometheus/pushgateway#api

然后,您将配置Prometheus服务器以刮入Pushgateway端点以摄取您的cron作业指标。

HTH!