|
| 1 | +# OpenCensus Prometheus Stats Exporter |
| 2 | + |
| 3 | +The OpenCensus Prometheus Stats Exporter is a stats exporter that exposes stats |
| 4 | +to the |
| 5 | +[Prometheus cpp client library](https://github.com/jupp0r/prometheus-cpp). |
| 6 | + |
| 7 | +## Quickstart |
| 8 | + |
| 9 | +### Prerequisites |
| 10 | + |
| 11 | +The Prometheus exporter exposes stats on an HTTP endpoint that is scraped by |
| 12 | +a Prometheus server. To install and run the Prometheus server, follow |
| 13 | +the instructions [here](https://prometheus.io/docs/introduction/first_steps/). |
| 14 | + |
| 15 | +### Register the exporter |
| 16 | + |
| 17 | +#### Using the Prometheus client library's exposer |
| 18 | + |
| 19 | +Register the Exposer on a selected port, and then register the OpenCensus |
| 20 | +Prometheus exporter with that exposer. This may be done either before or after |
| 21 | +views are registered for export and data is recorded, but stats will not be |
| 22 | +available to Prometheus until the exporter is registered. |
| 23 | + |
| 24 | +```c++ |
| 25 | + auto exporter = |
| 26 | + std::make_shared<opencensus::exporters::stats::PrometheusExporter>(); |
| 27 | + |
| 28 | + // Expose a Prometheus endpoint. |
| 29 | + prometheus::Exposer exposer("127.0.0.1:8080"); |
| 30 | + exposer.RegisterCollectable(exporter); |
| 31 | +``` |
| 32 | +
|
| 33 | +See also the Prometheus client library's |
| 34 | +[instructions](https://github.com/jupp0r/prometheus-cpp#usage). |
| 35 | +
|
| 36 | +#### Using a custom exposer |
| 37 | +
|
| 38 | +If your application already runs an HTTP server, you may want to expose |
| 39 | +Prometheus stats on a page by that server rather than through the default |
| 40 | +exposer. To do this, create an instance of the `PrometheusExporter` and then |
| 41 | +call `Collect()` to retrieve the latest stats, which can be serialized with the |
| 42 | +tools in the Prometheus client library: |
| 43 | +
|
| 44 | +```c++ |
| 45 | +#include "metrics.pb.h" |
| 46 | +#include "opencensus/exporters/stats/prometheus/prometheus_exporter.h" |
| 47 | +#include "prometheus/text_serializer.h" |
| 48 | +
|
| 49 | +... |
| 50 | +
|
| 51 | +opencensus::exporters::stats::PrometheusExporter exporter; |
| 52 | +prometheus::TextSerializer::TextSerializer serializer; |
| 53 | +
|
| 54 | +const std::vector<io::prometheus::client::MetricFamily> metrics = |
| 55 | + exporter.Collect(); |
| 56 | +const std::string formatted_metrics = serializer.Serialize(metrics); |
| 57 | +
|
| 58 | +``` |
0 commit comments