Skip to content

Commit 0addec6

Browse files
committed
Fix bug in SslMeterBinder when binding without registered bundles
Before this commit the meter registries were derived from the created gauges. If the SslMeterBinder has been bound to a MeterRegistry without any bundles, then no gauges are created. If a SslBundle is then dynamically added, onBundleChange is called with the new bundle, but the list of meter registries is empty (because we have no gauges). The effect is that the newly registered bundle has no metrics. Closes gh-48180
1 parent 0b92b81 commit 0addec6

File tree

2 files changed

+12
-23
lines changed
  • spring-boot-project/spring-boot-actuator-autoconfigure/src

2 files changed

+12
-23
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/ssl/SslMeterBinder.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import java.time.Instant;
2222
import java.time.temporal.ChronoUnit;
2323
import java.util.ArrayList;
24-
import java.util.Collection;
2524
import java.util.Comparator;
26-
import java.util.HashSet;
2725
import java.util.List;
2826
import java.util.Map;
2927
import java.util.Set;
@@ -58,6 +56,8 @@ class SslMeterBinder implements MeterBinder {
5856

5957
private final BundleMetrics bundleMetrics = new BundleMetrics();
6058

59+
private final Set<MeterRegistry> boundMeterRegistries = ConcurrentHashMap.newKeySet();
60+
6161
SslMeterBinder(SslInfo sslInfo, SslBundles sslBundles) {
6262
this(sslInfo, sslBundles, Clock.systemDefaultZone());
6363
}
@@ -77,13 +77,14 @@ class SslMeterBinder implements MeterBinder {
7777
private void onBundleChange(String bundleName) {
7878
BundleInfo bundle = this.sslInfo.getBundle(bundleName);
7979
this.bundleMetrics.updateBundle(bundle);
80-
for (MeterRegistry meterRegistry : this.bundleMetrics.getMeterRegistries()) {
80+
for (MeterRegistry meterRegistry : this.boundMeterRegistries) {
8181
createOrUpdateBundleMetrics(meterRegistry, bundle);
8282
}
8383
}
8484

8585
@Override
8686
public void bindTo(MeterRegistry meterRegistry) {
87+
this.boundMeterRegistries.add(meterRegistry);
8788
for (BundleInfo bundle : this.sslInfo.getBundles()) {
8889
createOrUpdateBundleMetrics(meterRegistry, bundle);
8990
}
@@ -138,18 +139,6 @@ MultiGauge getGauge(BundleInfo bundleInfo, MeterRegistry meterRegistry) {
138139
return gauges.getGauge(meterRegistry);
139140
}
140141

141-
/**
142-
* Returns all meter registries.
143-
* @return all meter registries
144-
*/
145-
Collection<MeterRegistry> getMeterRegistries() {
146-
Set<MeterRegistry> result = new HashSet<>();
147-
for (Gauges metrics : this.gauges.values()) {
148-
result.addAll(metrics.getMeterRegistries());
149-
}
150-
return result;
151-
}
152-
153142
/**
154143
* Updates the given bundle.
155144
* @param bundle the updated bundle
@@ -184,14 +173,6 @@ Gauges withBundle(BundleInfo bundle) {
184173
return new Gauges(bundle, this.multiGauges);
185174
}
186175

187-
/**
188-
* Returns all meter registries.
189-
* @return all meter registries
190-
*/
191-
Set<MeterRegistry> getMeterRegistries() {
192-
return this.multiGauges.keySet();
193-
}
194-
195176
private MultiGauge createGauge(MeterRegistry meterRegistry) {
196177
return MultiGauge.builder(CHAIN_EXPIRY_METRIC_NAME)
197178
.baseUnit("seconds")

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/ssl/SslMeterBinderTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ void shouldWatchUpdatesForBundlesRegisteredAfterConstruction() {
8888
.hasDays(36889);
8989
}
9090

91+
@Test
92+
void shouldRegisterMetricsIfNoBundleExistsAtBindTime() {
93+
DefaultSslBundleRegistry sslBundleRegistry = new DefaultSslBundleRegistry();
94+
MeterRegistry meterRegistry = bindToRegistry(sslBundleRegistry);
95+
sslBundleRegistry.registerBundle("dummy", SslBundle.of(createSslStoreBundle("classpath:test.p12")));
96+
assertThat(meterRegistry.getMeters()).isNotEmpty();
97+
}
98+
9199
private long findExpiryGauge(MeterRegistry meterRegistry, String chain, String certificateSerialNumber) {
92100
return (long) meterRegistry.get("ssl.chain.expiry")
93101
.tag("bundle", "test-0")

0 commit comments

Comments
 (0)