17
17
package org .springframework .boot .actuate .metrics ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Collection ;
20
21
import java .util .Collections ;
21
22
import java .util .HashMap ;
22
23
import java .util .HashSet ;
@@ -81,15 +82,15 @@ private String getName(Meter meter) {
81
82
public MetricResponse metric (@ Selector String requiredMetricName ,
82
83
@ Nullable List <String > tag ) {
83
84
List <Tag > tags = parseTags (tag );
84
- List <Meter > meters = new ArrayList <>();
85
- collectMeters ( meters , this . registry , requiredMetricName , tags );
85
+ Collection <Meter > meters = findFirstMatchingMeters ( this . registry ,
86
+ requiredMetricName , tags );
86
87
if (meters .isEmpty ()) {
87
88
return null ;
88
89
}
89
90
Map <Statistic , Double > samples = getSamples (meters );
90
91
Map <String , Set <String >> availableTags = getAvailableTags (meters );
91
92
tags .forEach ((t ) -> availableTags .remove (t .getKey ()));
92
- Meter .Id meterId = meters .get ( 0 ).getId ();
93
+ Meter .Id meterId = meters .iterator (). next ( ).getId ();
93
94
return new MetricResponse (requiredMetricName , meterId .getDescription (),
94
95
meterId .getBaseUnit (), asList (samples , Sample ::new ),
95
96
asList (availableTags , AvailableTag ::new ));
@@ -112,18 +113,23 @@ private Tag parseTag(String tag) {
112
113
return Tag .of (parts [0 ], parts [1 ]);
113
114
}
114
115
115
- private void collectMeters ( List <Meter > meters , MeterRegistry registry , String name ,
116
+ private Collection <Meter > findFirstMatchingMeters ( MeterRegistry registry , String name ,
116
117
Iterable <Tag > tags ) {
117
118
if (registry instanceof CompositeMeterRegistry ) {
118
- ((CompositeMeterRegistry ) registry ).getRegistries ()
119
- .forEach ((member ) -> collectMeters (meters , member , name , tags ));
120
- }
121
- else {
122
- meters .addAll (registry .find (name ).tags (tags ).meters ());
119
+ return findFirstMatchingMeters ((CompositeMeterRegistry ) registry , name , tags );
123
120
}
121
+ return registry .find (name ).tags (tags ).meters ();
122
+ }
123
+
124
+ private Collection <Meter > findFirstMatchingMeters (CompositeMeterRegistry composite ,
125
+ String name , Iterable <Tag > tags ) {
126
+ return composite .getRegistries ().stream ()
127
+ .map ((registry ) -> findFirstMatchingMeters (registry , name , tags ))
128
+ .filter ((matching ) -> !matching .isEmpty ()).findFirst ()
129
+ .orElse (Collections .emptyList ());
124
130
}
125
131
126
- private Map <Statistic , Double > getSamples (List <Meter > meters ) {
132
+ private Map <Statistic , Double > getSamples (Collection <Meter > meters ) {
127
133
Map <Statistic , Double > samples = new LinkedHashMap <>();
128
134
meters .forEach ((meter ) -> mergeMeasurements (samples , meter ));
129
135
return samples ;
@@ -138,7 +144,7 @@ private BiFunction<Double, Double, Double> mergeFunction(Statistic statistic) {
138
144
return Statistic .MAX .equals (statistic ) ? Double ::max : Double ::sum ;
139
145
}
140
146
141
- private Map <String , Set <String >> getAvailableTags (List <Meter > meters ) {
147
+ private Map <String , Set <String >> getAvailableTags (Collection <Meter > meters ) {
142
148
Map <String , Set <String >> availableTags = new HashMap <>();
143
149
meters .forEach ((meter ) -> mergeAvailableTags (availableTags , meter ));
144
150
return availableTags ;
0 commit comments