Skip to content

Commit 0ca3a8d

Browse files
committed
Change micrometer properties to use adapters
Update configuration property classes used with micrometer so that they no longer directly implement `Config` interfaces. Properties are now adapted to Config implementations independently. See spring-projectsgh-9970
1 parent 75f07b5 commit 0ca3a8d

22 files changed

+1168
-170
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2017 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.export;
18+
19+
import java.util.function.Function;
20+
21+
import org.springframework.util.Assert;
22+
23+
/**
24+
* Base class for properties to config adapters.
25+
*
26+
* @param <T> The properties type
27+
* @param <C> The config type
28+
* @author Phillip Webb
29+
* @since 2.0.0
30+
*/
31+
public class PropertiesConfigAdapter<T, C> {
32+
33+
private T properties;
34+
35+
private C defaults;
36+
37+
/**
38+
* Create a new {@link PropertiesConfigAdapter} instance.
39+
* @param properties the source properties
40+
* @param defaults a config implementation providing default values
41+
*/
42+
public PropertiesConfigAdapter(T properties, C defaults) {
43+
Assert.notNull(properties, "Properties must not be null");
44+
Assert.notNull(defaults, "Defaults must not be null");
45+
this.properties = properties;
46+
this.defaults = defaults;
47+
}
48+
49+
/**
50+
* Get the value from the properties or use a fallback from the {@code defaults}.
51+
* @param getter the getter for the properties
52+
* @param fallback the fallback method from the {@code defaults}
53+
* @param <V> the value type
54+
* @return the property or fallback value
55+
*/
56+
protected final <V> V get(Function<T, V> getter, Function<C, V> fallback) {
57+
V value = getter.apply(this.properties);
58+
return (value != null ? value : fallback.apply(this.defaults));
59+
}
60+
61+
}

spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/RegistryProperties.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/StepRegistryProperties.java

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,93 @@
1818

1919
import java.time.Duration;
2020

21-
import io.micrometer.core.instrument.spectator.step.StepRegistryConfig;
22-
2321
/**
24-
* Specialization of {@link RegistryProperties} for configuring a metrics registry that
25-
* pushes aggregated metrics on a regular interval.
22+
* Base class for properties that configure a metrics registry that pushes aggregated
23+
* metrics on a regular interval.
2624
*
2725
* @author Jon Schneider
2826
* @author Andy Wilkinson
2927
* @since 2.0.0
3028
*/
31-
public abstract class StepRegistryProperties extends RegistryProperties
32-
implements StepRegistryConfig {
29+
public abstract class StepRegistryProperties {
30+
31+
/**
32+
* The step size (reporting frequency) to use.
33+
*/
34+
private Duration step = Duration.ofMinutes(1);
35+
36+
/**
37+
* Enable publishing to the backend.
38+
*/
39+
private Boolean enabled = true;
40+
41+
/**
42+
* The connection timeout for requests to the backend.
43+
*/
44+
private Duration connectTimeout;
45+
46+
/**
47+
* The read timeout for requests to the backend.
48+
*/
49+
private Duration readTimeout;
50+
51+
/**
52+
* The number of threads to use with the metrics publishing scheduler.
53+
*/
54+
private Integer numThreads;
55+
56+
/**
57+
* The number of measurements per request to use for the backend. If more measurements
58+
* are found, then multiple requests will be made.
59+
*/
60+
private Integer batchSize;
61+
62+
public Duration getStep() {
63+
return this.step;
64+
}
3365

3466
public void setStep(Duration step) {
35-
set("step", step);
67+
this.step = step;
68+
}
69+
70+
public Boolean getEnabled() {
71+
return this.enabled;
3672
}
3773

3874
public void setEnabled(Boolean enabled) {
39-
set("enabled", enabled);
75+
this.enabled = enabled;
4076
}
4177

42-
public void setBatchSize(Integer batchSize) {
43-
set("batchSize", batchSize);
78+
public Duration getConnectTimeout() {
79+
return this.connectTimeout;
4480
}
4581

4682
public void setConnectTimeout(Duration connectTimeout) {
47-
set("connectTimeout", connectTimeout);
83+
this.connectTimeout = connectTimeout;
84+
}
85+
86+
public Duration getReadTimeout() {
87+
return this.readTimeout;
4888
}
4989

5090
public void setReadTimeout(Duration readTimeout) {
51-
set("readTimeout", readTimeout);
91+
this.readTimeout = readTimeout;
92+
}
93+
94+
public Integer getNumThreads() {
95+
return this.numThreads;
5296
}
5397

5498
public void setNumThreads(Integer numThreads) {
55-
set("numThreads", numThreads);
99+
this.numThreads = numThreads;
100+
}
101+
102+
public Integer getBatchSize() {
103+
return this.batchSize;
104+
}
105+
106+
public void setBatchSize(Integer batchSize) {
107+
this.batchSize = batchSize;
56108
}
57109

58110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2017 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.export;
18+
19+
import java.time.Duration;
20+
21+
import io.micrometer.core.instrument.spectator.step.StepRegistryConfig;
22+
23+
/**
24+
* Base class for {@link StepRegistryProperties} to {@link StepRegistryConfig} adapters.
25+
*
26+
* @param <T> The properties type
27+
* @param <C> The config type
28+
* @author Jon Schneider
29+
* @author Phillip Webb
30+
* @since 2.0.0
31+
*/
32+
public abstract class StepRegistryPropertiesConfigAdapter<T extends StepRegistryProperties, C extends StepRegistryConfig>
33+
extends PropertiesConfigAdapter<T, C> implements StepRegistryConfig {
34+
35+
public StepRegistryPropertiesConfigAdapter(T properties, C defaults) {
36+
super(properties, defaults);
37+
}
38+
39+
@Override
40+
public String prefix() {
41+
return null;
42+
}
43+
44+
@Override
45+
public String get(String k) {
46+
return null;
47+
}
48+
49+
@Override
50+
public Duration step() {
51+
return get(T::getStep, C::step);
52+
}
53+
54+
@Override
55+
public boolean enabled() {
56+
return get(T::getEnabled, C::enabled);
57+
}
58+
59+
@Override
60+
public Duration connectTimeout() {
61+
return get(T::getConnectTimeout, C::connectTimeout);
62+
}
63+
64+
@Override
65+
public Duration readTimeout() {
66+
return get(T::getReadTimeout, C::readTimeout);
67+
}
68+
69+
@Override
70+
public int numThreads() {
71+
return get(T::getNumThreads, C::numThreads);
72+
}
73+
74+
@Override
75+
public int batchSize() {
76+
return get(T::getBatchSize, C::batchSize);
77+
}
78+
79+
}

spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasExportConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@
4343
@EnableConfigurationProperties(AtlasProperties.class)
4444
public class AtlasExportConfiguration {
4545

46+
@Bean
47+
@ConditionalOnMissingBean(AtlasConfig.class)
48+
public AtlasConfig atlasConfig(AtlasProperties atlasProperties) {
49+
return new AtlasPropertiesConfigAdapter(atlasProperties);
50+
}
51+
4652
@Bean
4753
@ConditionalOnProperty(value = "spring.metrics.atlas.enabled", matchIfMissing = true)
48-
public MetricsExporter atlasExporter(AtlasConfig config, Clock clock) {
49-
return () -> new AtlasMeterRegistry(config, clock);
54+
public MetricsExporter atlasExporter(AtlasConfig atlasConfig, Clock clock) {
55+
return () -> new AtlasMeterRegistry(atlasConfig, clock);
5056
}
5157

5258
@Bean

0 commit comments

Comments
 (0)