The OpenCensus Prometheus Stats Exporter is a stats exporter that exports data to Prometheus. Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud.
To use this exporter, you need to install, configure and start Prometheus first. Follow the instructions here.
For Maven add to your pom.xml
:
<dependencies>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>0.28.3</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-stats-prometheus</artifactId>
<version>0.28.3</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>0.28.3</version>
<scope>runtime</scope>
</dependency>
</dependencies>
For Gradle add to your dependencies:
compile 'io.opencensus:opencensus-api:0.28.3'
compile 'io.opencensus:opencensus-exporter-stats-prometheus:0.28.3'
runtime 'io.opencensus:opencensus-impl:0.28.3'
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.
// ...
}
}
In this example, you should be able to see all the OpenCensus Prometheus metrics by visiting localhost:9091/metrics. Every time when you visit localhost:9091/metrics, the metrics will be collected from OpenCensus library and refreshed.
After collecting stats from OpenCensus, there are multiple options for exporting them. See Exporting via HTTP, Exporting to a Pushgateway and Bridges.
Java 7 or above is required for using this exporter.