Skip to content

Commit 64dae5e

Browse files
committed
Merge branch '1.5.x'
2 parents 43fb99a + 035e27b commit 64dae5e

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/PublicMetricsAutoConfiguration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

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

19+
import java.util.Collection;
1920
import java.util.List;
21+
import java.util.Map;
2022

2123
import javax.servlet.Servlet;
2224
import javax.sql.DataSource;
@@ -132,8 +134,10 @@ static class CacheStatisticsConfiguration {
132134
@Bean
133135
@ConditionalOnMissingBean
134136
@ConditionalOnBean(CacheStatisticsProvider.class)
135-
public CachePublicMetrics cachePublicMetrics() {
136-
return new CachePublicMetrics();
137+
public CachePublicMetrics cachePublicMetrics(
138+
Map<String, CacheManager> cacheManagers,
139+
Collection<CacheStatisticsProvider<?>> statisticsProviders) {
140+
return new CachePublicMetrics(cacheManagers, statisticsProviders);
137141
}
138142

139143
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.boot.actuate.cache.CacheStatistics;
2625
import org.springframework.boot.actuate.cache.CacheStatisticsProvider;
2726
import org.springframework.boot.actuate.metrics.Metric;
@@ -39,11 +38,15 @@
3938
*/
4039
public class CachePublicMetrics implements PublicMetrics {
4140

42-
@Autowired
43-
private Map<String, CacheManager> cacheManagers;
41+
private final Map<String, CacheManager> cacheManagers;
4442

45-
@Autowired
46-
private Collection<CacheStatisticsProvider<?>> statisticsProviders;
43+
private final Collection<CacheStatisticsProvider<?>> statisticsProviders;
44+
45+
public CachePublicMetrics(Map<String, CacheManager> cacheManagers,
46+
Collection<CacheStatisticsProvider<?>> statisticsProviders) {
47+
this.cacheManagers = cacheManagers;
48+
this.statisticsProviders = statisticsProviders;
49+
}
4750

4851
@Override
4952
public Collection<Metric<?>> metrics() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.endpoint;
18+
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import org.springframework.boot.actuate.cache.CacheStatisticsProvider;
28+
import org.springframework.boot.actuate.cache.CaffeineCacheStatisticsProvider;
29+
import org.springframework.boot.actuate.cache.ConcurrentMapCacheStatisticsProvider;
30+
import org.springframework.boot.actuate.metrics.Metric;
31+
import org.springframework.cache.CacheManager;
32+
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.entry;
36+
37+
/**
38+
* Tests for {@link CachePublicMetrics}
39+
*
40+
* @author Stephane Nicoll
41+
*/
42+
public class CachePublicMetricsTests {
43+
44+
private Map<String, CacheManager> cacheManagers = new HashMap<String, CacheManager>();
45+
46+
@Before
47+
public void setup() {
48+
this.cacheManagers.put("cacheManager",
49+
new ConcurrentMapCacheManager("foo", "bar"));
50+
}
51+
52+
@Test
53+
public void cacheMetricsWithMatchingProvider() {
54+
CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers,
55+
providers(new ConcurrentMapCacheStatisticsProvider()));
56+
Map<String, Number> metrics = metrics(cpm);
57+
assertThat(metrics).containsOnly(entry("cache.foo.size", 0L),
58+
entry("cache.bar.size", 0L));
59+
}
60+
61+
@Test
62+
public void cacheMetricsWithNoMatchingProvider() {
63+
CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers,
64+
providers(new CaffeineCacheStatisticsProvider()));
65+
Map<String, Number> metrics = metrics(cpm);
66+
assertThat(metrics).isEmpty();
67+
}
68+
69+
@Test
70+
public void cacheMetricsWithMultipleCacheManagers() {
71+
this.cacheManagers.put("anotherCacheManager",
72+
new ConcurrentMapCacheManager("foo"));
73+
CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers,
74+
providers(new ConcurrentMapCacheStatisticsProvider()));
75+
Map<String, Number> metrics = metrics(cpm);
76+
assertThat(metrics).containsOnly(entry("cache.cacheManager_foo.size", 0L),
77+
entry("cache.bar.size", 0L),
78+
entry("cache.anotherCacheManager_foo.size", 0L));
79+
}
80+
81+
82+
private Map<String, Number> metrics(CachePublicMetrics cpm) {
83+
Collection<Metric<?>> metrics = cpm.metrics();
84+
assertThat(metrics).isNotNull();
85+
Map<String, Number> result = new HashMap<String, Number>();
86+
for (Metric<?> metric : metrics) {
87+
result.put(metric.getName(), metric.getValue());
88+
}
89+
return result;
90+
}
91+
92+
private Collection<CacheStatisticsProvider<?>> providers(
93+
CacheStatisticsProvider<?>... providers) {
94+
return Arrays.asList(providers);
95+
}
96+
97+
}

0 commit comments

Comments
 (0)