Skip to content

Commit 1fce462

Browse files
Jon Schneiderphilwebb
authored andcommitted
Apply MeterRegistryCustomizer to composites
Update `MeterRegistryConfigurer` to also apply customizers to composite meter registries. Prior to this commit composites were skipped due to the incorrect assumption that did not contain their own state. Closes gh-12762
1 parent d49a102 commit 1fce462

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ class MeterRegistryConfigurer {
5959
}
6060

6161
void configure(MeterRegistry registry) {
62-
if (registry instanceof CompositeMeterRegistry) {
63-
return;
64-
}
6562
// Customizers must be applied before binders, as they may add custom
6663
// tags or alter timer or summary configuration.
6764
customize(registry);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.metrics;
18+
19+
import io.micrometer.core.instrument.MeterRegistry;
20+
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
21+
import org.junit.Test;
22+
23+
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration;
24+
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration;
25+
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
27+
28+
public class MeterRegistryConfigurerIntegrationTests {
29+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
30+
.with(MetricsRun.limitedTo(AtlasMetricsExportAutoConfiguration.class,
31+
PrometheusMetricsExportAutoConfiguration.class));
32+
33+
@Test
34+
public void binderMetricsAreSearchableFromTheComposite() {
35+
this.contextRunner
36+
.run((context) -> {
37+
CompositeMeterRegistry composite = context.getBean(CompositeMeterRegistry.class);
38+
composite.get("jvm.memory.used").gauge();
39+
40+
for (MeterRegistry registry : context.getBeansOfType(MeterRegistry.class).values()) {
41+
registry.get("jvm.memory.used").gauge();
42+
}
43+
});
44+
}
45+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurerTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import static org.mockito.BDDMockito.given;
3636
import static org.mockito.Mockito.inOrder;
3737
import static org.mockito.Mockito.verify;
38-
import static org.mockito.Mockito.verifyZeroInteractions;
3938

4039
/**
4140
* Tests for {@link MeterRegistryConfigurer}.
@@ -73,13 +72,13 @@ public void setup() {
7372
}
7473

7574
@Test
76-
public void configureWhenCompositeShouldSkip() {
77-
this.binders.add(this.mockBinder);
75+
public void configureWhenCompositeShouldApplyCustomizer() {
7876
this.customizers.add(this.mockCustomizer);
7977
MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(this.binders,
8078
this.filters, this.customizers, false);
81-
configurer.configure(new CompositeMeterRegistry());
82-
verifyZeroInteractions(this.mockBinder, this.mockCustomizer);
79+
CompositeMeterRegistry composite = new CompositeMeterRegistry();
80+
configurer.configure(composite);
81+
verify(this.mockCustomizer).customize(composite);
8382
}
8483

8584
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryCustomizerTests.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

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

19+
import io.micrometer.atlas.AtlasMeterRegistry;
1920
import io.micrometer.core.instrument.MeterRegistry;
20-
import io.micrometer.core.instrument.MeterRegistry.Config;
21+
import io.micrometer.prometheus.PrometheusMeterRegistry;
2122
import org.junit.Test;
2223

24+
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration;
25+
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration;
2326
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
2427
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2528
import org.springframework.context.annotation.Bean;
@@ -36,16 +39,16 @@
3639
public class MeterRegistryCustomizerTests {
3740

3841
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
39-
.with(MetricsRun.simple());
42+
.with(MetricsRun.limitedTo(AtlasMetricsExportAutoConfiguration.class,
43+
PrometheusMetricsExportAutoConfiguration.class));
4044

4145
@Test
4246
public void commonTagsAreAppliedToAutoConfiguredBinders() {
4347
this.contextRunner
4448
.withUserConfiguration(MeterRegistryCustomizerConfiguration.class)
4549
.run((context) -> {
4650
MeterRegistry registry = context.getBean(MeterRegistry.class);
47-
assertThat(registry.get("jvm.memory.used").tags("region", "us-east-1")
48-
.gauge()).isNotNull();
51+
registry.get("jvm.memory.used").tags("region", "us-east-1").gauge();
4952
});
5053
}
5154

@@ -55,9 +58,21 @@ public void commonTagsAreAppliedBeforeRegistryIsInjectableElsewhere() {
5558
.withUserConfiguration(MeterRegistryCustomizerConfiguration.class)
5659
.run((context) -> {
5760
MeterRegistry registry = context.getBean(MeterRegistry.class);
58-
assertThat(
59-
registry.get("my.thing").tags("region", "us-east-1").gauge())
60-
.isNotNull();
61+
registry.get("my.thing").tags("region", "us-east-1").gauge();
62+
});
63+
}
64+
65+
@Test
66+
public void customizersCanBeAppliedToSpecificRegistryTypes() {
67+
this.contextRunner
68+
.withUserConfiguration(MeterRegistryCustomizerConfiguration.class)
69+
.run((context) -> {
70+
MeterRegistry prometheus = context.getBean(PrometheusMeterRegistry.class);
71+
prometheus.get("jvm.memory.used").tags("job", "myjob").gauge();
72+
73+
MeterRegistry atlas = context.getBean(AtlasMeterRegistry.class);
74+
assertThat(atlas.find("jvm.memory.used").tags("job", "myjob")
75+
.gauge()).isNull();
6176
});
6277
}
6378

@@ -66,10 +81,12 @@ static class MeterRegistryCustomizerConfiguration {
6681

6782
@Bean
6883
public MeterRegistryCustomizer<MeterRegistry> commonTags() {
69-
return (registry) -> {
70-
Config config = registry.config();
71-
config.commonTags("region", "us-east-1");
72-
};
84+
return (registry) -> registry.config().commonTags("region", "us-east-1");
85+
}
86+
87+
@Bean
88+
public MeterRegistryCustomizer<PrometheusMeterRegistry> prometehusOnlyCommonTags() {
89+
return (registry) -> registry.config().commonTags("job", "myjob");
7390
}
7491

7592
@Bean

0 commit comments

Comments
 (0)