aboutsummaryrefslogtreecommitdiff
path: root/exporters/stats/prometheus/README.md
blob: fa19efc930d90224891daf39ec9301eaf5656b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# OpenCensus Prometheus Stats Exporter

The *OpenCensus Prometheus Stats Exporter* is a stats exporter that exports data to 
Prometheus. [Prometheus](https://prometheus.io/) is an open-source systems monitoring and alerting 
toolkit originally built at [SoundCloud](https://soundcloud.com/).

## Quickstart

### Prerequisites

To use this exporter, you need to install, configure and start Prometheus first. Follow the 
instructions [here](https://prometheus.io/docs/introduction/first_steps/).

### Hello "Prometheus Stats"

#### Add the dependencies to your project

For Maven add to your `pom.xml`:
```xml
<dependencies>
  <dependency>
    <groupId>io.opencensus</groupId>
    <artifactId>opencensus-api</artifactId>
    <version>0.16.1</version>
  </dependency>
  <dependency>
    <groupId>io.opencensus</groupId>
    <artifactId>opencensus-exporter-stats-prometheus</artifactId>
    <version>0.16.1</version>
  </dependency>
  <dependency>
    <groupId>io.opencensus</groupId>
    <artifactId>opencensus-impl</artifactId>
    <version>0.16.1</version>
    <scope>runtime</scope>
  </dependency>
</dependencies>
```

For Gradle add to your dependencies:
```groovy
compile 'io.opencensus:opencensus-api:0.16.1'
compile 'io.opencensus:opencensus-exporter-stats-prometheus:0.16.1'
runtime 'io.opencensus:opencensus-impl:0.16.1'
```

#### Register the exporter
 
```java
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.

#### Exporting

After collecting stats from OpenCensus, there are multiple options for exporting them. 
See [Exporting via HTTP](https://github.com/prometheus/client_java#http), [Exporting to a Pushgateway](https://github.com/prometheus/client_java#exporting-to-a-pushgateway)
and [Bridges](https://github.com/prometheus/client_java#bridges).

#### Java Versions

Java 7 or above is required for using this exporter.

## FAQ