17
17
package org .springframework .boot .actuate .metrics ;
18
18
19
19
import java .util .Collections ;
20
- import java .util .List ;
21
- import java .util .Map ;
22
20
import java .util .Optional ;
23
21
import java .util .stream .Stream ;
24
22
25
23
import io .micrometer .core .instrument .MeterRegistry ;
26
24
import io .micrometer .core .instrument .Statistic ;
25
+ import io .micrometer .core .instrument .composite .CompositeMeterRegistry ;
27
26
import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
28
27
import org .junit .Test ;
29
28
@@ -43,28 +42,41 @@ public class MetricsEndpointTests {
43
42
44
43
@ Test
45
44
public void listNamesHandlesEmptyListOfMeters () {
46
- Map <String , List <String >> result = this .endpoint .listNames ();
47
- assertThat (result ).containsOnlyKeys ("names" );
48
- assertThat (result .get ("names" )).isEmpty ();
45
+ MetricsEndpoint .ListNamesResponse result = this .endpoint .listNames ();
46
+ assertThat (result .getNames ()).isEmpty ();
49
47
}
50
48
51
49
@ Test
52
50
public void listNamesProducesListOfUniqueMeterNames () {
53
51
this .registry .counter ("com.example.foo" );
54
52
this .registry .counter ("com.example.bar" );
55
53
this .registry .counter ("com.example.foo" );
56
- Map <String , List <String >> result = this .endpoint .listNames ();
57
- assertThat (result ).containsOnlyKeys ("names" );
58
- assertThat (result .get ("names" )).containsOnlyOnce ("com.example.foo" ,
54
+ MetricsEndpoint .ListNamesResponse result = this .endpoint .listNames ();
55
+ assertThat (result .getNames ()).containsOnlyOnce ("com.example.foo" ,
59
56
"com.example.bar" );
60
57
}
61
58
59
+ @ Test
60
+ public void listNamesRecursesOverCompositeRegistries () {
61
+ CompositeMeterRegistry composite = new CompositeMeterRegistry ();
62
+ SimpleMeterRegistry reg1 = new SimpleMeterRegistry ();
63
+ SimpleMeterRegistry reg2 = new SimpleMeterRegistry ();
64
+ composite .add (reg1 );
65
+ composite .add (reg2 );
66
+
67
+ reg1 .counter ("counter1" ).increment ();
68
+ reg2 .counter ("counter2" ).increment ();
69
+
70
+ MetricsEndpoint endpoint = new MetricsEndpoint (composite );
71
+ assertThat (endpoint .listNames ().getNames ()).containsOnly ("counter1" , "counter2" );
72
+ }
73
+
62
74
@ Test
63
75
public void metricValuesAreTheSumOfAllTimeSeriesMatchingTags () {
64
76
this .registry .counter ("cache" , "result" , "hit" , "host" , "1" ).increment (2 );
65
77
this .registry .counter ("cache" , "result" , "miss" , "host" , "1" ).increment (2 );
66
78
this .registry .counter ("cache" , "result" , "hit" , "host" , "2" ).increment (2 );
67
- MetricsEndpoint .Response response = this .endpoint .metric ("cache" ,
79
+ MetricsEndpoint .MetricResponse response = this .endpoint .metric ("cache" ,
68
80
Collections .emptyList ());
69
81
assertThat (response .getName ()).isEqualTo ("cache" );
70
82
assertThat (availableTagKeys (response )).containsExactly ("result" , "host" );
@@ -77,29 +89,45 @@ public void metricValuesAreTheSumOfAllTimeSeriesMatchingTags() {
77
89
@ Test
78
90
public void metricWithSpaceInTagValue () {
79
91
this .registry .counter ("counter" , "key" , "a space" ).increment (2 );
80
- MetricsEndpoint .Response response = this .endpoint .metric ("counter" ,
92
+ MetricsEndpoint .MetricResponse response = this .endpoint .metric ("counter" ,
81
93
Collections .singletonList ("key:a space" ));
82
94
assertThat (response .getName ()).isEqualTo ("counter" );
83
95
assertThat (availableTagKeys (response )).isEmpty ();
84
96
assertThat (getCount (response )).hasValue (2.0 );
85
97
}
86
98
99
+ @ Test
100
+ public void metricPresentInOneRegistryOfACompositeAndNotAnother () {
101
+ CompositeMeterRegistry composite = new CompositeMeterRegistry ();
102
+ SimpleMeterRegistry reg1 = new SimpleMeterRegistry ();
103
+ SimpleMeterRegistry reg2 = new SimpleMeterRegistry ();
104
+ composite .add (reg1 );
105
+ composite .add (reg2 );
106
+
107
+ reg1 .counter ("counter1" ).increment ();
108
+ reg2 .counter ("counter2" ).increment ();
109
+
110
+ MetricsEndpoint endpoint = new MetricsEndpoint (composite );
111
+ assertThat (endpoint .metric ("counter1" , Collections .emptyList ())).isNotNull ();
112
+ assertThat (endpoint .metric ("counter2" , Collections .emptyList ())).isNotNull ();
113
+ }
114
+
87
115
@ Test
88
116
public void nonExistentMetric () {
89
- MetricsEndpoint .Response response = this .endpoint .metric ("does.not.exist" ,
117
+ MetricsEndpoint .MetricResponse response = this .endpoint .metric ("does.not.exist" ,
90
118
Collections .emptyList ());
91
119
assertThat (response ).isNull ();
92
120
}
93
121
94
- private Optional <Double > getCount (MetricsEndpoint .Response response ) {
122
+ private Optional <Double > getCount (MetricsEndpoint .MetricResponse response ) {
95
123
return response .getMeasurements ().stream ()
96
124
.filter ((ms ) -> ms .getStatistic ().equals (Statistic .Count )).findAny ()
97
- .map (MetricsEndpoint .Response .Sample ::getValue );
125
+ .map (MetricsEndpoint .MetricResponse .Sample ::getValue );
98
126
}
99
127
100
- private Stream <String > availableTagKeys (MetricsEndpoint .Response response ) {
128
+ private Stream <String > availableTagKeys (MetricsEndpoint .MetricResponse response ) {
101
129
return response .getAvailableTags ().stream ()
102
- .map (MetricsEndpoint .Response .AvailableTag ::getTag );
130
+ .map (MetricsEndpoint .MetricResponse .AvailableTag ::getTag );
103
131
}
104
132
105
133
}
0 commit comments