For assistance setting up Prometheus, Click here for a guided codelab.

Creating the exporter

To create the exporter, we’ll need to:

Import and use the Prometheus exporter package

Define a namespace that will uniquely identify our metrics when viewed on Prometheus

Expose a port on which we shall run a /metrics endpoint

With the defined port, we’ll need a Promethus configuration file so that Prometheus can scrape from this endpoint

Add this to your pom.xml file:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><opencensus.version>0.15.0</opencensus.version><!-- The OpenCensus version to use --></properties><dependencies><dependency><groupId>io.opencensus</groupId><artifactId>opencensus-api</artifactId><version>${opencensus.version}</version></dependency><dependency><groupId>io.opencensus</groupId><artifactId>opencensus-impl</artifactId><version>${opencensus.version}</version></dependency><dependency><groupId>io.opencensus</groupId><artifactId>opencensus-exporter-stats-prometheus</artifactId><version>${opencensus.version}</version></dependency><dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_httpserver</artifactId><version>0.3.0</version></dependency></dependencies>

We also need to expose the Prometheus endpoint say on address “localhost:8888”.

packageio.opencensus.tutorial.prometheus;importio.opencensus.exporter.stats.prometheus.PrometheusStatsCollector;importio.prometheus.client.exporter.HTTPServer;publicclassPrometheusTutorial{publicstaticvoidmain(String...args){// Register the Prometheus exporter
PrometheusStatsCollector.createAndRegister();// Run the server as a daemon on address "localhost:8888"
HTTPServerserver=newHTTPServer("localhost",8888,true);}}