Skip to content

Commit 2099215

Browse files
Document support of Prometheus Client 1.x
Closes micrometer-metricsgh-4957
1 parent 453862c commit 2099215

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

docs/modules/ROOT/pages/implementations/_install.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[[implementations-installing]]
2-
== Installing
1+
[id=installing-micrometer-registry-{system}]
2+
== Installing micrometer-registry-{system}
33

44
For Gradle, add the following implementation:
55

docs/modules/ROOT/pages/implementations/prometheus.adoc

+55-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
= Micrometer Prometheus
22
:sectnums:
3-
:system: prometheus
43

54
Prometheus is a dimensional time series database with a built-in UI, a custom query language, and math operations.
65
Prometheus is designed to operate on a pull model, periodically scraping metrics from application instances, based on service discovery.
76

7+
Micrometer uses the Prometheus Java Client under the hood; there are two versions of it and Micrometer supports both. If you want to use the "new" client (`1.x`), use `micrometer-registry-prometheus` but if you want to use the "legacy" client (`0.x`), use `micrometer-registry-prometheus-simpleclient`.
8+
9+
:system: prometheus
10+
include::_install.adoc[]
11+
12+
:system: prometheus-simpleclient
813
include::_install.adoc[]
914

1015
== Configuring
@@ -31,13 +36,25 @@ try {
3136
});
3237
3338
new Thread(server::start).start();
34-
} catch (IOException e) {
39+
}
40+
catch (IOException e) {
3541
throw new RuntimeException(e);
3642
}
3743
----
3844
<1> The `PrometheusMeterRegistry` has a `scrape()` function that knows how to supply the String data necessary for the scrape. All you have to do is wire it to an endpoint.
3945

40-
You can alternatively use `io.prometheus.client.exporter.HTTPServer`, which you can find in `io.prometheus:simpleclient_httpserver`:
46+
If you use the "new" client (`micrometer-registry-prometheus`), you can alternatively use `io.prometheus.metrics.exporter.httpserver.HTTPServer`, which you can find in `io.prometheus:prometheus-metrics-exporter-httpserver`:
47+
48+
[source,java]
49+
----
50+
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
51+
HTTPServer.builder()
52+
.port(8080)
53+
.registry(prometheusRegistry.getPrometheusRegistry())
54+
.buildAndStart();
55+
----
56+
57+
If you use the "legacy" client (`micrometer-registry-prometheus-simpleclient`), you can alternatively use `io.prometheus.client.exporter.HTTPServer`, which you can find in `io.prometheus:simpleclient_httpserver`:
4158

4259
[source,java]
4360
----
@@ -46,12 +63,20 @@ PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(Prometh
4663
new HTTPServer(new InetSocketAddress(8080), prometheusRegistry.getPrometheusRegistry(), true);
4764
----
4865

49-
Another alternative can be `io.prometheus.client.exporter.MetricsServlet`, which you can find in `io.prometheus:simpleclient_servlet` in case your app is running in a servlet container (such as Tomcat):
66+
If you use the "new" client (`micrometer-registry-prometheus`), another alternative can be `io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet`, which you can find in `io.prometheus:prometheus-metrics-exporter-servlet-jakarta` in case your app is running in a servlet container (such as Tomcat):
67+
68+
[source,java]
69+
----
70+
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
71+
HttpServlet servlet = new PrometheusMetricsServlet(prometheusRegistry.getPrometheusRegistry());
72+
----
73+
74+
If you use the "legacy" client (`micrometer-registry-prometheus-simpleclient`), another alternative can be `io.prometheus.client.exporter.MetricsServlet`, which you can find in `io.prometheus:simpleclient_servlet` in case your app is running in a servlet container (such as Tomcat):
5075

5176
[source,java]
5277
----
5378
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
54-
HttpServlet metricsServlet = new MetricsServlet(prometheusRegistry.getPrometheusRegistry());
79+
HttpServlet servlet = new MetricsServlet(prometheusRegistry.getPrometheusRegistry());
5580
----
5681

5782
=== Scrape Format
@@ -60,7 +85,14 @@ By default, the `PrometheusMeterRegistry` `scrape()` method returns the https://
6085

6186
The https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md[OpenMetrics] format can also be produced.
6287
To specify the format to be returned, you can pass a content type to the `scrape` method.
63-
For example, to get the OpenMetrics 1.0.0 format scrape, you could use the Prometheus Java client constant for it, as follows:
88+
For example, to get the OpenMetrics 1.0.0 format scrape, you could use the Prometheus Java client constant for it, as follows in case of the "new" client (`micrometer-registry-prometheus`):
89+
90+
[source,java]
91+
----
92+
String openMetricsScrape = registry.scrape("application/openmetrics-text");
93+
----
94+
95+
if you use the "legacy" client (`micrometer-registry-prometheus-simpleclient`):
6496

6597
[source,java]
6698
----
@@ -177,11 +209,25 @@ registry.throwExceptionOnRegistrationFailure();
177209

178210
== Exemplars
179211

180-
Exemplars are metadata that you can attach to the value of your time series. They can reference data outside of your metrics. A common use case is storing tracing information (`traceId`, `spanId`). Exemplars are not tags/dimensions (labels in Prometheus terminology), they will not increase cardinality since they belong to the values of the time series.
212+
Exemplars are metadata that you can attach to the value of your time series. They can reference reference data outside of your metrics. A common use case is storing tracing information (`traceId`, `spanId`). Exemplars are not tags/dimensions (labels in Prometheus terminology), they will not increase cardinality since they belong to the values of the time series.
213+
214+
In order to setup Exemplars for `PrometheusMeterRegistry`, you will need a component that provides you the tracing information. If you use the "new" client (`micrometer-registry-prometheus`), this component is the `io.prometheus.metrics.tracer.common.SpanContext` while if you use the "legacy" client (`micrometer-registry-prometheus-simpleclient`), it is the `SpanContextSupplier`.
181215

182-
In order to setup Exemplars for `PrometheusMeterRegistry`, you will need a component that provides you the tracing information: `SpanContextSupplier`.
216+
Setting them up are somewhat similar, if you use the "new" client (`micrometer-registry-prometheus`):
217+
[source,java]
218+
----
219+
PrometheusMeterRegistry registry = new PrometheusMeterRegistry(
220+
PrometheusConfig.DEFAULT,
221+
new PrometheusRegistry(),
222+
Clock.SYSTEM,
223+
new MySpanContext() <1>
224+
);
225+
registry.counter("test").increment();
226+
System.out.println(registry.scrape("application/openmetrics-text"));
227+
----
228+
<1> You need to implement `MySpanContext` (`class MySpanContext implements SpanContext { ... }`) or use an implementation that already exists.
183229

184-
You can set it up as follows:
230+
But if you use the "legacy" client (`micrometer-registry-prometheus-simpleclient`):
185231
[source,java]
186232
----
187233
PrometheusMeterRegistry registry = new PrometheusMeterRegistry(

0 commit comments

Comments
 (0)