Skip to content

Commit 520ca8b

Browse files
committed
Document metrics collection support
1 parent f3b2148 commit 520ca8b

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
<imagesdir>./images</imagesdir>
321321
<idprefix />
322322
<idseparator>-</idseparator>
323-
<source-highlighter>coderay</source-highlighter>
323+
<source-highlighter>rouge</source-highlighter>
324324
<test-examples>../../test/java/com/rabbitmq/model/docs</test-examples>
325325
<build-directory>${project.build.directory}</build-directory>
326326
</attributes>

src/docs/asciidoc/usage.adoc

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ include::{test-examples}/Api.java[tag=connection-recovery-back-off]
244244
It is also possible to deactivate topology recovery if it is not appropriate for a given application.
245245
The application would usually register a connection <<lifecycle-listeners,lifecycle listener>> to know when the connection is recovered and recover its own state accordingly.
246246

247-
.Deactivate topology recovery
247+
.Deactivating topology recovery
248248
[source,java,indent=0]
249249
--------
250250
include::{test-examples}/Api.java[tag=connection-recovery-no-topology-recovery]
@@ -254,13 +254,84 @@ include::{test-examples}/Api.java[tag=connection-recovery-no-topology-recovery]
254254

255255
It is also possible to deactivate recovery altogether:
256256

257-
.Deactivate recovery
257+
.Deactivating recovery
258258
[source,java,indent=0]
259259
--------
260260
include::{test-examples}/Api.java[tag=connection-recovery-deactivate]
261261
--------
262262
<1> Deactivate recovery
263263

264+
=== Metrics Collection
265+
266+
The library provides the {javadoc-url}/com/rabbitmq/model/metrics/MetricsCollector.html[`MetricsCollector`] abstraction to collect metrics.
267+
A `MetricsCollector` instance is set at the environment level and is called in several places by the library.
268+
The underlying implementation can then collect, compute and expose metrics.
269+
270+
The library provides the `MicrometerMetricsCollector`, an implementation based on https://micrometer.io/[Micrometer].
271+
Micrometer is a façade for observability systems: the user can choose the Micrometer meter registry for their favorite system.
272+
273+
Here is how to set up the Micrometer metrics collector and configure it for Prometheus:
274+
275+
.Setting metrics collection with Prometheus
276+
[source,java,indent=0]
277+
--------
278+
include::{test-examples}/Api.java[tag=metrics-micrometer-prometheus]
279+
--------
280+
<1> Create the Micrometer meter registry for Prometheus
281+
<2> Create the metrics collector with the registry
282+
<3> Set the metrics collector in the environment builder
283+
<4> Create a connection, a Micrometer gauge is incremented
284+
285+
The following metrics are recorded:
286+
287+
* the number of open connections
288+
* the number of open publishers
289+
* the number of open consumers
290+
* the total number of published messages
291+
* the total number of accepted published messages
292+
* the total number of failed published messages
293+
* the total number of consumed messages
294+
* the total number of accepted consumed messages
295+
* the total number of requeued consumed messages
296+
* the total number of discarded consumed messages
297+
298+
Here is an example of the Prometheus output:
299+
300+
.Example of the Prometheus output
301+
[source,prometheus]
302+
------
303+
# HELP rabbitmq_amqp_connections
304+
# TYPE rabbitmq_amqp_connections gauge
305+
rabbitmq_amqp_connections 1.0
306+
# HELP rabbitmq_amqp_consumed_total
307+
# TYPE rabbitmq_amqp_consumed_total counter
308+
rabbitmq_amqp_consumed_total 3.0
309+
# HELP rabbitmq_amqp_consumed_accepted_total
310+
# TYPE rabbitmq_amqp_consumed_accepted_total counter
311+
rabbitmq_amqp_consumed_accepted_total 1.0
312+
# HELP rabbitmq_amqp_consumed_discarded_total
313+
# TYPE rabbitmq_amqp_consumed_discarded_total counter
314+
rabbitmq_amqp_consumed_discarded_total 1.0
315+
# HELP rabbitmq_amqp_consumed_requeued_total
316+
# TYPE rabbitmq_amqp_consumed_requeued_total counter
317+
rabbitmq_amqp_consumed_requeued_total 1.0
318+
# HELP rabbitmq_amqp_consumers
319+
# TYPE rabbitmq_amqp_consumers gauge
320+
rabbitmq_amqp_consumers 1.0
321+
# HELP rabbitmq_amqp_published_total
322+
# TYPE rabbitmq_amqp_published_total counter
323+
rabbitmq_amqp_published_total 2.0
324+
# HELP rabbitmq_amqp_published_accepted_total
325+
# TYPE rabbitmq_amqp_published_accepted_total counter
326+
rabbitmq_amqp_published_accepted_total 1.0
327+
# HELP rabbitmq_amqp_published_failed_total
328+
# TYPE rabbitmq_amqp_published_failed_total counter
329+
rabbitmq_amqp_published_failed_total 1.0
330+
# HELP rabbitmq_amqp_publishers
331+
# TYPE rabbitmq_amqp_publishers gauge
332+
rabbitmq_amqp_publishers 1.0
333+
------
334+
264335
=== Remote Procedure Call (RPC)
265336

266337
Remote procedure call with RabbitMQ consists in a client sending a request message and a server replying with a response message.

src/test/java/com/rabbitmq/model/docs/Api.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
import com.rabbitmq.model.*;
2121
import com.rabbitmq.model.amqp.AmqpEnvironmentBuilder;
22+
import com.rabbitmq.model.metrics.MetricsCollector;
23+
import com.rabbitmq.model.metrics.MicrometerMetricsCollector;
24+
import io.micrometer.prometheusmetrics.PrometheusConfig;
25+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
2226

2327
import java.nio.charset.StandardCharsets;
2428
import java.time.Duration;
@@ -301,4 +305,21 @@ void connectionRecoveryDeactivate() {
301305
// end::connection-recovery-deactivate[]
302306
}
303307

308+
void metricsCollectorMicrometerPrometheus() {
309+
String queue = null;
310+
// tag::metrics-micrometer-prometheus[]
311+
PrometheusMeterRegistry registry = new PrometheusMeterRegistry( // <1>
312+
PrometheusConfig.DEFAULT
313+
);
314+
MetricsCollector collector = new MicrometerMetricsCollector(registry); // <2>
315+
316+
Environment environment = new AmqpEnvironmentBuilder()
317+
.metricsCollector(collector) // <3>
318+
.build();
319+
320+
Connection connection = environment.connectionBuilder().build(); // <4>
321+
// end::metrics-micrometer-prometheus[]
322+
}
323+
324+
304325
}

src/test/java/com/rabbitmq/model/metrics/MicrometerMetricsCollectorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,9 @@ void prometheus() {
119119
collector.consumeDisposition(MetricsCollector.ConsumeDisposition.ACCEPTED);
120120
collector.consumeDisposition(MetricsCollector.ConsumeDisposition.REQUEUED);
121121
collector.consumeDisposition(MetricsCollector.ConsumeDisposition.DISCARDED);
122+
123+
assertThat(registry.scrape())
124+
.contains("# TYPE rabbitmq_amqp_connections gauge")
125+
.contains("rabbitmq_amqp_connections 1.0");
122126
}
123127
}

0 commit comments

Comments
 (0)