Skip to content

Commit 035e27b

Browse files
committed
Merge branch '1.4.x' into 1.5.x
2 parents 652a5e7 + 8e5bf4b commit 035e27b

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
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;
@@ -135,8 +137,10 @@ static class CacheStatisticsConfiguration {
135137
@Bean
136138
@ConditionalOnMissingBean
137139
@ConditionalOnBean(CacheStatisticsProvider.class)
138-
public CachePublicMetrics cachePublicMetrics() {
139-
return new CachePublicMetrics();
140+
public CachePublicMetrics cachePublicMetrics(
141+
Map<String, CacheManager> cacheManagers,
142+
Collection<CacheStatisticsProvider<?>> statisticsProviders) {
143+
return new CachePublicMetrics(cacheManagers, statisticsProviders);
140144
}
141145

142146
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -45,6 +45,16 @@ public class CachePublicMetrics implements PublicMetrics {
4545
@Autowired
4646
private Collection<CacheStatisticsProvider<?>> statisticsProviders;
4747

48+
@Deprecated
49+
public CachePublicMetrics() {
50+
}
51+
52+
public CachePublicMetrics(Map<String, CacheManager> cacheManagers,
53+
Collection<CacheStatisticsProvider<?>> statisticsProviders) {
54+
this.cacheManagers = cacheManagers;
55+
this.statisticsProviders = statisticsProviders;
56+
}
57+
4858
@Override
4959
public Collection<Metric<?>> metrics() {
5060
Collection<Metric<?>> metrics = new HashSet<Metric<?>>();
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)