|
20 | 20 | import com.rabbitmq.client.MetricsCollector; |
21 | 21 | import io.micrometer.core.instrument.Counter; |
22 | 22 | import io.micrometer.core.instrument.MeterRegistry; |
| 23 | +import io.micrometer.core.instrument.Tag; |
| 24 | +import io.micrometer.core.instrument.Tags; |
23 | 25 |
|
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Collections; |
24 | 28 | import java.util.concurrent.atomic.AtomicLong; |
25 | 29 | import java.util.function.Function; |
26 | 30 |
|
@@ -62,7 +66,11 @@ public MicrometerMetricsCollector(MeterRegistry registry) { |
62 | 66 | } |
63 | 67 |
|
64 | 68 | public MicrometerMetricsCollector(final MeterRegistry registry, final String prefix) { |
65 | | - this(metric -> metric.create(registry, prefix)); |
| 69 | + this(metric -> metric.create(registry, prefix, new String[] {})); |
| 70 | + } |
| 71 | + |
| 72 | + public MicrometerMetricsCollector(final MeterRegistry registry, final String prefix, final String... tags) { |
| 73 | + this(metric -> metric.create(registry, prefix, tags)); |
66 | 74 | } |
67 | 75 |
|
68 | 76 | public MicrometerMetricsCollector(Function<Metrics, Object> metricsCreator) { |
@@ -141,42 +149,56 @@ public Counter getRejectedMessages() { |
141 | 149 | public enum Metrics { |
142 | 150 | CONNECTIONS { |
143 | 151 | @Override |
144 | | - Object create(MeterRegistry registry, String prefix) { |
145 | | - return registry.gauge(prefix + ".connections", new AtomicLong(0)); |
| 152 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 153 | + return registry.gauge(prefix + ".connections", tags(tags), new AtomicLong(0)); |
146 | 154 | } |
147 | 155 | }, |
148 | 156 | CHANNELS { |
149 | 157 | @Override |
150 | | - Object create(MeterRegistry registry, String prefix) { |
151 | | - return registry.gauge(prefix + ".channels", new AtomicLong(0)); |
| 158 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 159 | + return registry.gauge(prefix + ".channels", tags(tags), new AtomicLong(0)); |
152 | 160 | } |
153 | 161 | }, |
154 | 162 | PUBLISHED_MESSAGES { |
155 | 163 | @Override |
156 | | - Object create(MeterRegistry registry, String prefix) { |
157 | | - return registry.counter(prefix + ".published"); |
| 164 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 165 | + return registry.counter(prefix + ".published", tags); |
158 | 166 | } |
159 | 167 | }, |
160 | 168 | CONSUMED_MESSAGES { |
161 | 169 | @Override |
162 | | - Object create(MeterRegistry registry, String prefix) { |
163 | | - return registry.counter(prefix + ".consumed"); |
| 170 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 171 | + return registry.counter(prefix + ".consumed", tags); |
164 | 172 | } |
165 | 173 | }, |
166 | 174 | ACKNOWLEDGED_MESSAGES { |
167 | 175 | @Override |
168 | | - Object create(MeterRegistry registry, String prefix) { |
169 | | - return registry.counter(prefix + ".acknowledged"); |
| 176 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 177 | + return registry.counter(prefix + ".acknowledged", tags); |
170 | 178 | } |
171 | 179 | }, |
172 | 180 | REJECTED_MESSAGES { |
173 | 181 | @Override |
174 | | - Object create(MeterRegistry registry, String prefix) { |
175 | | - return registry.counter(prefix + ".rejected"); |
| 182 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 183 | + return registry.counter(prefix + ".rejected", tags); |
176 | 184 | } |
177 | 185 | }; |
178 | 186 |
|
179 | | - abstract Object create(MeterRegistry registry, String prefix); |
| 187 | + Object create(MeterRegistry registry, String prefix) { |
| 188 | + return this.create(registry, prefix, new String[] {}); |
| 189 | + } |
| 190 | + |
| 191 | + abstract Object create(MeterRegistry registry, String prefix, String... tags); |
| 192 | + |
| 193 | + private static Iterable<Tag> tags(String... tagStrings) { |
| 194 | + Collection<Tag> tags; |
| 195 | + if (tagStrings != null && tagStrings.length > 0) { |
| 196 | + tags = Tags.zip(tagStrings); |
| 197 | + } else { |
| 198 | + tags = Collections.emptyList(); |
| 199 | + } |
| 200 | + return tags; |
| 201 | + } |
180 | 202 | } |
181 | 203 |
|
182 | 204 | } |
0 commit comments