0

I've got a CronJob running that I'd like to integrate OpenCensus into to export to Prometheus. However I currently have to add a 1 minute sleep after my job finishes to make sure that Prometheus has scraped my metrics.

I'd like to use the Prometheus PushGateway to avoid the extra sleep if possible, but I can't figure out how to hook it up to OpenCensus.

Here's the documentation for it that mentions it: https://github.com/census-instrumentation/opencensus-java/tree/master/exporters/stats/prometheus - it says the following:

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.
    // ...
  }
}

However there's no examples of how I actually would use it with OpenCensus. Has anyone done it before, and how?

Paul
  • 4,422
  • 5
  • 29
  • 55

1 Answers1

0

I think you likely [don't want to|can't] use OpenCensus for what you want to do.

What OpenCensus does

OpenCensus pushes metrics to e.g. Stackdriver, Datadog etc. but, for Prometheus -- the request-response flow is inverted and metrics are generally pulled from an endpoint that renders metrics in Prometheus' exposition format.

OpenCensus's Prometheus exporter(s) -- and, for Java, the example you reference -- creates an HTTP server into which you'd pour your metrics (see this example as the principle is the same).

Prometheus -- or some other service that understands Prometheus' metric format -- would then periodically scrape this endpoint and ingest the metrics.

What Prometheus Pushgateway does

Your cron job will emit metrics that must be pushed into e.g. Prometheus Pushgateway. In this architecture, the Pushgateway replaces the OpenCensus Prometheus Exporter endpoint.

All you need -- possibly nothing more than curl statements added to your cron jobs -- is something that will generate HTTP requests and ship these to a Pushgateway. See:

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

Then you'd configure a Prometheus server to scrape the Pushgateway endpoint to ingest your cron job metrics.

HTH!

DazWilkin
  • 32,823
  • 5
  • 47
  • 88