|
18 | 18 |
|
19 | 19 | import java.util.Collection;
|
20 | 20 | import java.util.Collections;
|
| 21 | +import java.util.HashMap; |
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.Map;
|
23 | 24 | import java.util.stream.Collectors;
|
24 | 25 | import java.util.stream.Stream;
|
25 |
| -import java.util.stream.StreamSupport; |
26 | 26 |
|
27 | 27 | import io.micrometer.core.instrument.Measurement;
|
28 | 28 | import io.micrometer.core.instrument.Meter;
|
29 | 29 | import io.micrometer.core.instrument.MeterRegistry;
|
30 |
| -import io.micrometer.core.instrument.NamingConvention; |
31 | 30 | import io.micrometer.core.instrument.Statistic;
|
32 |
| -import io.micrometer.core.instrument.util.HierarchicalNameMapper; |
| 31 | +import io.micrometer.core.instrument.Tag; |
33 | 32 |
|
34 | 33 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
35 | 34 | import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
36 | 35 | import org.springframework.boot.actuate.endpoint.annotation.Selector;
|
| 36 | +import org.springframework.lang.Nullable; |
| 37 | +import org.springframework.util.Assert; |
37 | 38 |
|
38 | 39 | /**
|
39 | 40 | * An {@link Endpoint} for exposing the metrics held by a {@link MeterRegistry}.
|
@@ -61,57 +62,133 @@ private String getMeterIdName(Meter meter) {
|
61 | 62 | }
|
62 | 63 |
|
63 | 64 | @ReadOperation
|
64 |
| - public Map<String, Collection<MeasurementSample>> metric( |
65 |
| - @Selector String requiredMetricName) { |
66 |
| - return this.registry.find(requiredMetricName).meters().stream() |
67 |
| - .collect(Collectors.toMap(this::getHierarchicalName, this::getSamples)); |
68 |
| - } |
69 |
| - |
70 |
| - private List<MeasurementSample> getSamples(Meter meter) { |
71 |
| - return stream(meter.measure()).map(this::getSample).collect(Collectors.toList()); |
72 |
| - } |
| 65 | + public Response metric(@Selector String requiredMetricName, |
| 66 | + @Nullable List<String> tag) { |
| 67 | + Assert.isTrue(tag == null || tag.stream().allMatch((t) -> t.contains(":")), |
| 68 | + "Each tag parameter must be in the form key:value"); |
| 69 | + List<Tag> tags = parseTags(tag); |
| 70 | + Collection<Meter> meters = this.registry.find(requiredMetricName).tags(tags) |
| 71 | + .meters(); |
| 72 | + if (meters.isEmpty()) { |
| 73 | + return null; |
| 74 | + } |
73 | 75 |
|
74 |
| - private MeasurementSample getSample(Measurement measurement) { |
75 |
| - return new MeasurementSample(measurement.getStatistic(), measurement.getValue()); |
76 |
| - } |
| 76 | + Map<Statistic, Double> samples = new HashMap<>(); |
| 77 | + Map<String, List<String>> availableTags = new HashMap<>(); |
| 78 | + |
| 79 | + for (Meter meter : meters) { |
| 80 | + for (Measurement ms : meter.measure()) { |
| 81 | + samples.merge(ms.getStatistic(), ms.getValue(), Double::sum); |
| 82 | + } |
| 83 | + for (Tag availableTag : meter.getId().getTags()) { |
| 84 | + availableTags.merge(availableTag.getKey(), |
| 85 | + Collections.singletonList(availableTag.getValue()), |
| 86 | + (t1, t2) -> Stream.concat(t1.stream(), t2.stream()) |
| 87 | + .collect(Collectors.toList())); |
| 88 | + } |
| 89 | + } |
77 | 90 |
|
78 |
| - private String getHierarchicalName(Meter meter) { |
79 |
| - return HierarchicalNameMapper.DEFAULT.toHierarchicalName(meter.getId(), |
80 |
| - NamingConvention.camelCase); |
| 91 | + tags.forEach((t) -> availableTags.remove(t.getKey())); |
| 92 | + |
| 93 | + return new Response(requiredMetricName, |
| 94 | + samples.entrySet().stream() |
| 95 | + .map((sample) -> new Response.Sample(sample.getKey(), |
| 96 | + sample.getValue())) |
| 97 | + .collect( |
| 98 | + Collectors.toList()), |
| 99 | + availableTags.entrySet().stream() |
| 100 | + .map((tagValues) -> new Response.AvailableTag(tagValues.getKey(), |
| 101 | + tagValues.getValue())) |
| 102 | + .collect(Collectors.toList())); |
81 | 103 | }
|
82 | 104 |
|
83 |
| - private <T> Stream<T> stream(Iterable<T> measure) { |
84 |
| - return StreamSupport.stream(measure.spliterator(), false); |
| 105 | + private List<Tag> parseTags(List<String> tags) { |
| 106 | + return tags == null ? Collections.emptyList() : tags.stream().map((t) -> { |
| 107 | + String[] tagParts = t.split(":", 2); |
| 108 | + return Tag.of(tagParts[0], tagParts[1]); |
| 109 | + }).collect(Collectors.toList()); |
85 | 110 | }
|
86 | 111 |
|
87 | 112 | /**
|
88 |
| - * A measurement sample combining a {@link Statistic statistic} and a value. |
| 113 | + * Response payload. |
89 | 114 | */
|
90 |
| - static class MeasurementSample { |
| 115 | + static class Response { |
91 | 116 |
|
92 |
| - private final Statistic statistic; |
| 117 | + private final String name; |
93 | 118 |
|
94 |
| - private final Double value; |
| 119 | + private final List<Sample> measurements; |
95 | 120 |
|
96 |
| - MeasurementSample(Statistic statistic, Double value) { |
97 |
| - this.statistic = statistic; |
98 |
| - this.value = value; |
| 121 | + private final List<AvailableTag> availableTags; |
| 122 | + |
| 123 | + Response(String name, List<Sample> measurements, |
| 124 | + List<AvailableTag> availableTags) { |
| 125 | + this.name = name; |
| 126 | + this.measurements = measurements; |
| 127 | + this.availableTags = availableTags; |
99 | 128 | }
|
100 | 129 |
|
101 |
| - public Statistic getStatistic() { |
102 |
| - return this.statistic; |
| 130 | + public String getName() { |
| 131 | + return this.name; |
103 | 132 | }
|
104 | 133 |
|
105 |
| - public Double getValue() { |
106 |
| - return this.value; |
| 134 | + public List<Sample> getMeasurements() { |
| 135 | + return this.measurements; |
107 | 136 | }
|
108 | 137 |
|
109 |
| - @Override |
110 |
| - public String toString() { |
111 |
| - return "MeasurementSample{" + "statistic=" + this.statistic + ", value=" |
112 |
| - + this.value + '}'; |
| 138 | + public List<AvailableTag> getAvailableTags() { |
| 139 | + return this.availableTags; |
113 | 140 | }
|
114 | 141 |
|
115 |
| - } |
| 142 | + /** |
| 143 | + * A set of tags for further dimensional drilldown and their potential values. |
| 144 | + */ |
| 145 | + static class AvailableTag { |
| 146 | + |
| 147 | + private final String tag; |
| 148 | + |
| 149 | + private final List<String> values; |
| 150 | + |
| 151 | + AvailableTag(String tag, List<String> values) { |
| 152 | + this.tag = tag; |
| 153 | + this.values = values; |
| 154 | + } |
116 | 155 |
|
| 156 | + public String getTag() { |
| 157 | + return this.tag; |
| 158 | + } |
| 159 | + |
| 160 | + public List<String> getValues() { |
| 161 | + return this.values; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * A measurement sample combining a {@link Statistic statistic} and a value. |
| 167 | + */ |
| 168 | + static class Sample { |
| 169 | + |
| 170 | + private final Statistic statistic; |
| 171 | + |
| 172 | + private final Double value; |
| 173 | + |
| 174 | + Sample(Statistic statistic, Double value) { |
| 175 | + this.statistic = statistic; |
| 176 | + this.value = value; |
| 177 | + } |
| 178 | + |
| 179 | + public Statistic getStatistic() { |
| 180 | + return this.statistic; |
| 181 | + } |
| 182 | + |
| 183 | + public Double getValue() { |
| 184 | + return this.value; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public String toString() { |
| 189 | + return "MeasurementSample{" + "statistic=" + this.statistic + ", value=" |
| 190 | + + this.value + '}'; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
117 | 194 | }
|
0 commit comments