xref: /aosp_15_r20/external/opencensus-java/exporters/stats/prometheus/README.md (revision a24ffb47c3166327784aa05b149974e82e8f71b8)
1*a24ffb47SSadaf Ebrahimi# OpenCensus Prometheus Stats Exporter
2*a24ffb47SSadaf Ebrahimi
3*a24ffb47SSadaf EbrahimiThe *OpenCensus Prometheus Stats Exporter* is a stats exporter that exports data to
4*a24ffb47SSadaf EbrahimiPrometheus. [Prometheus](https://prometheus.io/) is an open-source systems monitoring and alerting
5*a24ffb47SSadaf Ebrahimitoolkit originally built at [SoundCloud](https://soundcloud.com/).
6*a24ffb47SSadaf Ebrahimi
7*a24ffb47SSadaf Ebrahimi## Quickstart
8*a24ffb47SSadaf Ebrahimi
9*a24ffb47SSadaf Ebrahimi### Prerequisites
10*a24ffb47SSadaf Ebrahimi
11*a24ffb47SSadaf EbrahimiTo use this exporter, you need to install, configure and start Prometheus first. Follow the
12*a24ffb47SSadaf Ebrahimiinstructions [here](https://prometheus.io/docs/introduction/first_steps/).
13*a24ffb47SSadaf Ebrahimi
14*a24ffb47SSadaf Ebrahimi### Hello "Prometheus Stats"
15*a24ffb47SSadaf Ebrahimi
16*a24ffb47SSadaf Ebrahimi#### Add the dependencies to your project
17*a24ffb47SSadaf Ebrahimi
18*a24ffb47SSadaf EbrahimiFor Maven add to your `pom.xml`:
19*a24ffb47SSadaf Ebrahimi```xml
20*a24ffb47SSadaf Ebrahimi<dependencies>
21*a24ffb47SSadaf Ebrahimi  <dependency>
22*a24ffb47SSadaf Ebrahimi    <groupId>io.opencensus</groupId>
23*a24ffb47SSadaf Ebrahimi    <artifactId>opencensus-api</artifactId>
24*a24ffb47SSadaf Ebrahimi    <version>0.28.3</version>
25*a24ffb47SSadaf Ebrahimi  </dependency>
26*a24ffb47SSadaf Ebrahimi  <dependency>
27*a24ffb47SSadaf Ebrahimi    <groupId>io.opencensus</groupId>
28*a24ffb47SSadaf Ebrahimi    <artifactId>opencensus-exporter-stats-prometheus</artifactId>
29*a24ffb47SSadaf Ebrahimi    <version>0.28.3</version>
30*a24ffb47SSadaf Ebrahimi  </dependency>
31*a24ffb47SSadaf Ebrahimi  <dependency>
32*a24ffb47SSadaf Ebrahimi    <groupId>io.opencensus</groupId>
33*a24ffb47SSadaf Ebrahimi    <artifactId>opencensus-impl</artifactId>
34*a24ffb47SSadaf Ebrahimi    <version>0.28.3</version>
35*a24ffb47SSadaf Ebrahimi    <scope>runtime</scope>
36*a24ffb47SSadaf Ebrahimi  </dependency>
37*a24ffb47SSadaf Ebrahimi</dependencies>
38*a24ffb47SSadaf Ebrahimi```
39*a24ffb47SSadaf Ebrahimi
40*a24ffb47SSadaf EbrahimiFor Gradle add to your dependencies:
41*a24ffb47SSadaf Ebrahimi```groovy
42*a24ffb47SSadaf Ebrahimicompile 'io.opencensus:opencensus-api:0.28.3'
43*a24ffb47SSadaf Ebrahimicompile 'io.opencensus:opencensus-exporter-stats-prometheus:0.28.3'
44*a24ffb47SSadaf Ebrahimiruntime 'io.opencensus:opencensus-impl:0.28.3'
45*a24ffb47SSadaf Ebrahimi```
46*a24ffb47SSadaf Ebrahimi
47*a24ffb47SSadaf Ebrahimi#### Register the exporter
48*a24ffb47SSadaf Ebrahimi
49*a24ffb47SSadaf Ebrahimi```java
50*a24ffb47SSadaf Ebrahimipublic class MyMainClass {
51*a24ffb47SSadaf Ebrahimi  public static void main(String[] args) {
52*a24ffb47SSadaf Ebrahimi    // Creates a PrometheusStatsCollector and registers it to the default Prometheus registry.
53*a24ffb47SSadaf Ebrahimi    PrometheusStatsCollector.createAndRegister();
54*a24ffb47SSadaf Ebrahimi
55*a24ffb47SSadaf Ebrahimi    // Uses a simple Prometheus HTTPServer to export metrics.
56*a24ffb47SSadaf Ebrahimi    // You can use a Prometheus PushGateway instead, though that's discouraged by Prometheus:
57*a24ffb47SSadaf Ebrahimi    // https://prometheus.io/docs/practices/pushing/#should-i-be-using-the-pushgateway.
58*a24ffb47SSadaf Ebrahimi    io.prometheus.client.exporter.HTTPServer server =
59*a24ffb47SSadaf Ebrahimi      new HTTPServer(/*host*/ "localhost", /*port*/  9091, /*daemon*/ true);
60*a24ffb47SSadaf Ebrahimi
61*a24ffb47SSadaf Ebrahimi    // Your code here.
62*a24ffb47SSadaf Ebrahimi    // ...
63*a24ffb47SSadaf Ebrahimi  }
64*a24ffb47SSadaf Ebrahimi}
65*a24ffb47SSadaf Ebrahimi```
66*a24ffb47SSadaf Ebrahimi
67*a24ffb47SSadaf EbrahimiIn this example, you should be able to see all the OpenCensus Prometheus metrics by visiting
68*a24ffb47SSadaf Ebrahimilocalhost:9091/metrics. Every time when you visit localhost:9091/metrics, the metrics will be
69*a24ffb47SSadaf Ebrahimicollected from OpenCensus library and refreshed.
70*a24ffb47SSadaf Ebrahimi
71*a24ffb47SSadaf Ebrahimi#### Exporting
72*a24ffb47SSadaf Ebrahimi
73*a24ffb47SSadaf EbrahimiAfter collecting stats from OpenCensus, there are multiple options for exporting them.
74*a24ffb47SSadaf EbrahimiSee [Exporting via HTTP](https://github.com/prometheus/client_java#http), [Exporting to a Pushgateway](https://github.com/prometheus/client_java#exporting-to-a-pushgateway)
75*a24ffb47SSadaf Ebrahimiand [Bridges](https://github.com/prometheus/client_java#bridges).
76*a24ffb47SSadaf Ebrahimi
77*a24ffb47SSadaf Ebrahimi#### Java Versions
78*a24ffb47SSadaf Ebrahimi
79*a24ffb47SSadaf EbrahimiJava 7 or above is required for using this exporter.
80*a24ffb47SSadaf Ebrahimi
81*a24ffb47SSadaf Ebrahimi## FAQ
82