|
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 |
|
26 | 30 | import static com.rabbitmq.client.impl.MicrometerMetricsCollector.Metrics.ACKNOWLEDGED_MESSAGES; |
@@ -61,10 +65,14 @@ public MicrometerMetricsCollector(MeterRegistry registry) { |
61 | 65 | } |
62 | 66 |
|
63 | 67 | public MicrometerMetricsCollector(final MeterRegistry registry, final String prefix) { |
| 68 | + this(registry, prefix, new String[] {}); |
| 69 | + } |
| 70 | + |
| 71 | + public MicrometerMetricsCollector(final MeterRegistry registry, final String prefix, final String ... tags) { |
64 | 72 | this(new MetricsCreator() { |
65 | 73 | @Override |
66 | 74 | public Object create(Metrics metric) { |
67 | | - return metric.create(registry, prefix); |
| 75 | + return metric.create(registry, prefix, tags); |
68 | 76 | } |
69 | 77 | }); |
70 | 78 | } |
@@ -145,42 +153,56 @@ public Counter getRejectedMessages() { |
145 | 153 | public enum Metrics { |
146 | 154 | CONNECTIONS { |
147 | 155 | @Override |
148 | | - Object create(MeterRegistry registry, String prefix) { |
149 | | - return registry.gauge(prefix + ".connections", new AtomicLong(0)); |
| 156 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 157 | + return registry.gauge(prefix + ".connections", tags(tags), new AtomicLong(0)); |
150 | 158 | } |
151 | 159 | }, |
152 | 160 | CHANNELS { |
153 | 161 | @Override |
154 | | - Object create(MeterRegistry registry, String prefix) { |
155 | | - return registry.gauge(prefix + ".channels", new AtomicLong(0)); |
| 162 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 163 | + return registry.gauge(prefix + ".channels", tags(tags), new AtomicLong(0)); |
156 | 164 | } |
157 | 165 | }, |
158 | 166 | PUBLISHED_MESSAGES { |
159 | 167 | @Override |
160 | | - Object create(MeterRegistry registry, String prefix) { |
161 | | - return registry.counter(prefix + ".published"); |
| 168 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 169 | + return registry.counter(prefix + ".published", tags); |
162 | 170 | } |
163 | 171 | }, |
164 | 172 | CONSUMED_MESSAGES { |
165 | 173 | @Override |
166 | | - Object create(MeterRegistry registry, String prefix) { |
167 | | - return registry.counter(prefix + ".consumed"); |
| 174 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 175 | + return registry.counter(prefix + ".consumed", tags); |
168 | 176 | } |
169 | 177 | }, |
170 | 178 | ACKNOWLEDGED_MESSAGES { |
171 | 179 | @Override |
172 | | - Object create(MeterRegistry registry, String prefix) { |
173 | | - return registry.counter(prefix + ".acknowledged"); |
| 180 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 181 | + return registry.counter(prefix + ".acknowledged", tags); |
174 | 182 | } |
175 | 183 | }, |
176 | 184 | REJECTED_MESSAGES { |
177 | 185 | @Override |
178 | | - Object create(MeterRegistry registry, String prefix) { |
179 | | - return registry.counter(prefix + ".rejected"); |
| 186 | + Object create(MeterRegistry registry, String prefix, String... tags) { |
| 187 | + return registry.counter(prefix + ".rejected", tags); |
180 | 188 | } |
181 | 189 | }; |
182 | 190 |
|
183 | | - abstract Object create(MeterRegistry registry, String prefix); |
| 191 | + Object create(MeterRegistry registry, String prefix) { |
| 192 | + return this.create(registry, prefix, new String[] {}); |
| 193 | + } |
| 194 | + |
| 195 | + abstract Object create(MeterRegistry registry, String prefix, String... tags); |
| 196 | + |
| 197 | + private static Iterable<Tag> tags(String... tagStrings) { |
| 198 | + Collection<Tag> tags; |
| 199 | + if (tagStrings != null && tagStrings.length > 0) { |
| 200 | + tags = Tags.zip(tagStrings); |
| 201 | + } else { |
| 202 | + tags = Collections.emptyList(); |
| 203 | + } |
| 204 | + return tags; |
| 205 | + } |
184 | 206 | } |
185 | 207 |
|
186 | 208 | public interface MetricsCreator { |
|
0 commit comments