Skip to content

Commit 5ea8bb0

Browse files
committed
Recommend the use of a MeterBinder when a metric depends on a bean
Closes gh-19557
1 parent 16aa8fb commit 5ea8bb0

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

+10-9
Original file line numberDiff line numberDiff line change
@@ -1309,14 +1309,7 @@ You can apply customizations to particular registry implementations by being mor
13091309
}
13101310
----
13111311

1312-
With that setup in place you can inject `MeterRegistry` in your components and register metrics:
1313-
1314-
[source,java,indent=0]
1315-
----
1316-
include::{code-examples}/actuate/metrics/SampleBean.java[tag=example]
1317-
----
1318-
1319-
Spring Boot also <<production-ready-metrics-meter,configures built-in instrumentation>> (i.e. `MeterBinder` implementations) that you can control via configuration or dedicated annotation markers.
1312+
Spring Boot also <<production-ready-metrics-meter,configures built-in instrumentation>> that you can control via configuration or dedicated annotation markers.
13201313

13211314

13221315

@@ -1916,8 +1909,16 @@ To register custom metrics, inject `MeterRegistry` into your component, as shown
19161909
include::{code-examples}/actuate/metrics/MetricsMeterRegistryInjectionExample.java[tag=component]
19171910
----
19181911

1919-
If you find that you repeatedly instrument a suite of metrics across components or applications, you may encapsulate this suite in a `MeterBinder` implementation.
1912+
If you metrics depend on other beans, it is recommend that you use a `MeterBinder` to register them, as shown in the following example:
1913+
1914+
[source,java,indent=0]
1915+
----
1916+
include::{code-examples}/actuate/metrics/SampleMeterBinderConfiguration.java[tag=example]
1917+
----
1918+
1919+
Using a `MeterBinder` ensures that the correct dependency relationships are set up and that the bean is available when the metric's value is retrieved.
19201920
By default, metrics from all `MeterBinder` beans will be automatically bound to the Spring-managed `MeterRegistry`.
1921+
A `MeterBinder` implementation can also be useful if you find that you repeatedly instrument a suite of metrics across components or applications..
19211922

19221923

19231924

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuate/metrics/SampleBean.java renamed to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuate/metrics/SampleMeterBinderConfiguration.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,30 +16,31 @@
1616

1717
package org.springframework.boot.docs.actuate.metrics;
1818

19-
import io.micrometer.core.instrument.Counter;
20-
import io.micrometer.core.instrument.MeterRegistry;
19+
import io.micrometer.core.instrument.Gauge;
20+
import io.micrometer.core.instrument.binder.MeterBinder;
2121

22-
import org.springframework.stereotype.Component;
22+
import org.springframework.context.annotation.Bean;
2323

2424
/**
25-
* Example to show manual usage of {@link MeterRegistry}.
25+
* Example to show configuration of a custom {@link MeterBinder}.
2626
*
27-
* @author Stephane Nicoll
27+
* @author Andy Wilkinson
2828
*/
29-
// tag::example[]
30-
@Component
31-
public class SampleBean {
29+
public class SampleMeterBinderConfiguration {
3230

33-
private final Counter counter;
34-
35-
public SampleBean(MeterRegistry registry) {
36-
this.counter = registry.counter("received.messages");
31+
// tag::example[]
32+
@Bean
33+
MeterBinder queueSize(Queue queue) {
34+
return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
3735
}
36+
// end::example[]
37+
38+
static class Queue {
39+
40+
int size() {
41+
return 5;
42+
}
3843

39-
public void handleMessage(String message) {
40-
this.counter.increment();
41-
// handle message implementation
4244
}
4345

4446
}
45-
// end::example[]

0 commit comments

Comments
 (0)