-
Notifications
You must be signed in to change notification settings - Fork 962
/
MeterRegistry.java
1081 lines (968 loc) · 44.3 KB
/
MeterRegistry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument;
import io.micrometer.core.annotation.Incubating;
import io.micrometer.core.instrument.Meter.Id;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.config.MeterFilterReply;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.pause.NoPauseDetector;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.instrument.noop.*;
import io.micrometer.core.instrument.search.MeterNotFoundException;
import io.micrometer.core.instrument.search.RequiredSearch;
import io.micrometer.core.instrument.search.Search;
import io.micrometer.core.instrument.util.TimeUtils;
import io.micrometer.core.lang.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.*;
import java.util.stream.Collectors;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
/**
* Creates and manages your application's set of meters. Exporters use the meter registry
* to iterate over the set of meters instrumenting your application, and then further
* iterate over each meter's metrics, generally resulting in a time series in the metrics
* backend for each combination of metrics and dimensions.
* <p>
* MeterRegistry may be used in a reactive context. As such, implementations must not
* negatively impact the calling thread, e.g. it should respond immediately by avoiding IO
* call, deep stack recursion or any coordination.
* <p>
* If you register meters having the same ID multiple times, the first registration only
* will work and the subsequent registrations will be ignored.
*
* @author Jon Schneider
* @author Johnny Lim
*/
public abstract class MeterRegistry {
// @formatter:off
private static final EnumMap<TimeUnit, String> BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values())
.collect(
Collectors.toMap(
Function.identity(),
(timeUnit) -> timeUnit.toString().toLowerCase(),
(k, v) -> { throw new IllegalStateException("Duplicate keys should not exist."); },
() -> new EnumMap<>(TimeUnit.class)
)
);
// @formatter:on
protected final Clock clock;
private final Object meterMapLock = new Object();
private volatile MeterFilter[] filters = new MeterFilter[0];
private final List<Consumer<Meter>> meterAddedListeners = new CopyOnWriteArrayList<>();
private final List<Consumer<Meter>> meterRemovedListeners = new CopyOnWriteArrayList<>();
private final List<BiConsumer<Meter.Id, String>> meterRegistrationFailedListeners = new CopyOnWriteArrayList<>();
private final Config config = new Config();
private final More more = new More();
// Even though writes are guarded by meterMapLock, iterators across value space are
// supported
// Hence, we use CHM to support that iteration without ConcurrentModificationException
// risk
private final Map<Id, Meter> meterMap = new ConcurrentHashMap<>();
/**
* Map of meter id whose associated meter contains synthetic counterparts to those
* synthetic ids. We maintain these associations so that when we remove a meter with
* synthetics, they can removed as well.
*/
// Guarded by meterMapLock for both reads and writes
private final Map<Id, Set<Id>> syntheticAssociations = new HashMap<>();
private final AtomicBoolean closed = new AtomicBoolean();
private PauseDetector pauseDetector = new NoPauseDetector();
/**
* We'll use snake case as a general-purpose default for registries because it is the
* most likely to result in a portable name. Camel casing is also perfectly
* acceptable. '-' and '.' separators can pose problems for some monitoring systems.
* '-' is interpreted as metric subtraction in some (including Prometheus), and '.' is
* used to flatten tags into hierarchical names when shipping metrics to hierarchical
* backends such as Graphite.
*/
private NamingConvention namingConvention = NamingConvention.snakeCase;
protected MeterRegistry(Clock clock) {
requireNonNull(clock);
this.clock = clock;
}
/**
* Build a new gauge to be added to the registry. This is guaranteed to only be called
* if the gauge doesn't already exist.
* @param id The id that uniquely identifies the gauge.
* @param obj State object used to compute a value.
* @param valueFunction Function that is applied on the value for the number.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return A new gauge.
*/
protected abstract <T> Gauge newGauge(Meter.Id id, @Nullable T obj, ToDoubleFunction<T> valueFunction);
/**
* Build a new counter to be added to the registry. This is guaranteed to only be
* called if the counter doesn't already exist.
* @param id The id that uniquely identifies the counter.
* @return A new counter.
*/
protected abstract Counter newCounter(Meter.Id id);
/**
* Build a new long task timer to be added to the registry. This is guaranteed to only
* be called if the long task timer doesn't already exist.
* @param id The id that uniquely identifies the long task timer.
* @return A new long task timer.
* @deprecated Implement
* {@link #newLongTaskTimer(Meter.Id, DistributionStatisticConfig)} instead.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
protected LongTaskTimer newLongTaskTimer(Meter.Id id) {
throw new UnsupportedOperationException("MeterRegistry implementations may still override this, but it is only "
+ "invoked by the overloaded form of newLongTaskTimer for backwards compatibility.");
}
/**
* Build a new long task timer to be added to the registry. This is guaranteed to only
* be called if the long task timer doesn't already exist.
* @param id The id that uniquely identifies the long task timer.
* @param distributionStatisticConfig Configuration for published distribution
* statistics.
* @return A new long task timer.
* @since 1.5.0
*/
protected LongTaskTimer newLongTaskTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) {
return newLongTaskTimer(id); // default implementation for backwards compatibility
}
/**
* Build a new timer to be added to the registry. This is guaranteed to only be called
* if the timer doesn't already exist.
* @param id The id that uniquely identifies the timer.
* @param distributionStatisticConfig Configuration for published distribution
* statistics.
* @param pauseDetector The pause detector to use for coordinated omission
* compensation.
* @return A new timer.
*/
protected abstract Timer newTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig,
PauseDetector pauseDetector);
/**
* Build a new distribution summary to be added to the registry. This is guaranteed to
* only be called if the distribution summary doesn't already exist.
* @param id The id that uniquely identifies the distribution summary.
* @param distributionStatisticConfig Configuration for published distribution
* statistics.
* @param scale Multiply every recorded sample by this factor.
* @return A new distribution summary.
*/
protected abstract DistributionSummary newDistributionSummary(Meter.Id id,
DistributionStatisticConfig distributionStatisticConfig, double scale);
/**
* Build a new custom meter to be added to the registry. This is guaranteed to only be
* called if the custom meter doesn't already exist.
* @param id The id that uniquely identifies the custom meter.
* @param type What kind of meter this is.
* @param measurements A set of measurements describing how to sample this meter.
* @return A new custom meter.
*/
protected abstract Meter newMeter(Meter.Id id, Meter.Type type, Iterable<Measurement> measurements);
/**
* Build a new time gauge to be added to the registry. This is guaranteed to only be
* called if the time gauge doesn't already exist.
* @param id The id that uniquely identifies the time gauge.
* @param obj The state object from which the value function derives a measurement.
* @param valueFunctionUnit The base unit of time returned by the value function.
* @param valueFunction A function returning a time value that can go up or down.
* @param <T> The type of the object upon which the value function derives a
* measurement.
* @return A new time gauge.
*/
protected <T> TimeGauge newTimeGauge(Meter.Id id, @Nullable T obj, TimeUnit valueFunctionUnit,
ToDoubleFunction<T> valueFunction) {
Meter.Id withUnit = id.withBaseUnit(getBaseTimeUnitStr());
Gauge gauge = newGauge(withUnit, obj,
obj2 -> TimeUtils.convert(valueFunction.applyAsDouble(obj2), valueFunctionUnit, getBaseTimeUnit()));
return new TimeGauge() {
@Override
public Id getId() {
return withUnit;
}
@Override
public double value() {
return gauge.value();
}
@Override
public TimeUnit baseTimeUnit() {
return getBaseTimeUnit();
}
};
}
/**
* Build a new function timer to be added to the registry. This is guaranteed to only
* be called if the function timer doesn't already exist.
* @param id The id that uniquely identifies the function timer.
* @param obj The state object from which the count and total functions derive
* measurements.
* @param countFunction A monotonically increasing count function.
* @param totalTimeFunction A monotonically increasing total time function.
* @param totalTimeFunctionUnit The base unit of time of the totals returned by the
* total time function.
* @param <T> The type of the object upon which the value functions derives their
* measurements.
* @return A new function timer.
*/
protected abstract <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction,
ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit);
/**
* Build a new function counter to be added to the registry. This is guaranteed to
* only be called if the function counter doesn't already exist.
* @param id The id that uniquely identifies the function counter.
* @param obj The state object from which the count function derives a measurement.
* @param countFunction A monotonically increasing count function.
* @param <T> The type of the object upon which the value function derives a
* measurement.
* @return A new function counter.
*/
protected abstract <T> FunctionCounter newFunctionCounter(Id id, T obj, ToDoubleFunction<T> countFunction);
protected List<Tag> getConventionTags(Meter.Id id) {
return id.getConventionTags(config().namingConvention());
}
protected String getConventionName(Meter.Id id) {
return id.getConventionName(config().namingConvention());
}
/**
* @return the registry's base TimeUnit. Must not be null.
*/
protected abstract TimeUnit getBaseTimeUnit();
/**
* Every custom registry implementation should define a default histogram expiry at a
* minimum: <pre>
* DistributionStatisticConfig.builder()
* .expiry(defaultStep)
* .build()
* .merge(DistributionStatisticConfig.DEFAULT);
* </pre>
* @return The default distribution statistics config.
*/
protected abstract DistributionStatisticConfig defaultHistogramConfig();
private String getBaseTimeUnitStr() {
return BASE_TIME_UNIT_STRING_CACHE.get(getBaseTimeUnit());
}
/**
* Only used by {@link Counter#builder(String)}.
* @param id The identifier for this counter.
* @return A new or existing counter.
*/
Counter counter(Meter.Id id) {
return registerMeterIfNecessary(Counter.class, id, this::newCounter, NoopCounter::new);
}
/**
* Only used by {@link Gauge#builder(String, Object, ToDoubleFunction)}.
* @param id The identifier for this gauge.
* @param obj State object used to compute a value.
* @param valueFunction Function that is applied on the value for the number.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return A new or existing gauge.
*/
<T> Gauge gauge(Meter.Id id, @Nullable T obj, ToDoubleFunction<T> valueFunction) {
return registerMeterIfNecessary(Gauge.class, id, id2 -> newGauge(id2, obj, valueFunction), NoopGauge::new);
}
/**
* Only used by {@link Timer#builder(String)}.
* @param id The identifier for this timer.
* @param distributionStatisticConfig Configuration that governs how distribution
* statistics are computed.
* @return A new or existing timer.
*/
Timer timer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig,
PauseDetector pauseDetectorOverride) {
return registerMeterIfNecessary(Timer.class, id, distributionStatisticConfig, (id2, filteredConfig) -> {
Meter.Id withUnit = id2.withBaseUnit(getBaseTimeUnitStr());
return newTimer(withUnit, filteredConfig.merge(defaultHistogramConfig()), pauseDetectorOverride);
}, NoopTimer::new);
}
/**
* Only used by {@link DistributionSummary#builder(String)}.
* @param id The identifier for this distribution summary.
* @param distributionStatisticConfig Configuration that governs how distribution
* statistics are computed.
* @return A new or existing distribution summary.
*/
DistributionSummary summary(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double scale) {
return registerMeterIfNecessary(DistributionSummary.class, id, distributionStatisticConfig, (id2,
filteredConfig) -> newDistributionSummary(id2, filteredConfig.merge(defaultHistogramConfig()), scale),
NoopDistributionSummary::new);
}
/**
* Register a custom meter type.
* @param id Id of the meter being registered.
* @param type Meter type, which may be used by naming conventions to normalize the
* name.
* @param measurements A sequence of measurements describing how to sample the meter.
* @return The meter.
*/
Meter register(Meter.Id id, Meter.Type type, Iterable<Measurement> measurements) {
return registerMeterIfNecessary(Meter.class, id, id2 -> newMeter(id2, type, measurements), NoopMeter::new);
}
/**
* @return The set of registered meters.
*/
public List<Meter> getMeters() {
return Collections.unmodifiableList(new ArrayList<>(meterMap.values()));
}
/**
* Iterate over each meter in the registry.
* @param consumer Consumer of each meter during iteration.
*/
public void forEachMeter(Consumer<? super Meter> consumer) {
meterMap.values().forEach(consumer);
}
/**
* @return A configuration object used to change the behavior of this registry.
*/
public Config config() {
return config;
}
/**
* Initiate a search beginning with a metric name. If constraints added in the search
* are not satisfied, the search will return {@code null}.
* @param name The meter name to locate.
* @return A new search.
*/
public Search find(String name) {
return Search.in(this).name(name);
}
/**
* Initiate a search beginning with a metric name. All constraints added in the search
* must be satisfied or an {@link MeterNotFoundException} is thrown.
* @param name The meter name to locate.
* @return A new search.
*/
public RequiredSearch get(String name) {
return RequiredSearch.in(this).name(name);
}
/**
* Tracks a monotonically increasing value.
* @param name The base metric name
* @param tags Sequence of dimensions for breaking down the name.
* @return A new or existing counter.
*/
public Counter counter(String name, Iterable<Tag> tags) {
return Counter.builder(name).tags(tags).register(this);
}
/**
* Tracks a monotonically increasing value.
* @param name The base metric name
* @param tags MUST be an even number of arguments representing key/value pairs of
* tags.
* @return A new or existing counter.
*/
public Counter counter(String name, String... tags) {
return counter(name, Tags.of(tags));
}
/**
* Measures the distribution of samples.
* @param name The base metric name
* @param tags Sequence of dimensions for breaking down the name.
* @return A new or existing distribution summary.
*/
public DistributionSummary summary(String name, Iterable<Tag> tags) {
return DistributionSummary.builder(name).tags(tags).register(this);
}
/**
* Measures the distribution of samples.
* @param name The base metric name
* @param tags MUST be an even number of arguments representing key/value pairs of
* tags.
* @return A new or existing distribution summary.
*/
public DistributionSummary summary(String name, String... tags) {
return summary(name, Tags.of(tags));
}
/**
* Measures the time taken for short tasks and the count of these tasks.
* @param name The base metric name
* @param tags Sequence of dimensions for breaking down the name.
* @return A new or existing timer.
*/
public Timer timer(String name, Iterable<Tag> tags) {
return Timer.builder(name).tags(tags).register(this);
}
/**
* Measures the time taken for short tasks and the count of these tasks.
* @param name The base metric name
* @param tags MUST be an even number of arguments representing key/value pairs of
* tags.
* @return A new or existing timer.
*/
public Timer timer(String name, String... tags) {
return timer(name, Tags.of(tags));
}
/**
* Access to less frequently used meter types and patterns.
* @return Access to additional meter types and patterns.
*/
public More more() {
return more;
}
/**
* Register a gauge that reports the value of the object after the function
* {@code valueFunction} is applied. The registration will keep a weak reference to
* the object so it will not prevent garbage collection. Applying
* {@code valueFunction} on the object should be thread safe.
* @param name Name of the gauge being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param stateObject State object used to compute a value.
* @param valueFunction Function that produces an instantaneous gauge value from the
* state object.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return The state object that was passed in so the registration can be done as part
* of an assignment statement.
*/
@Nullable
public <T> T gauge(String name, Iterable<Tag> tags, @Nullable T stateObject, ToDoubleFunction<T> valueFunction) {
Gauge.builder(name, stateObject, valueFunction).tags(tags).register(this);
return stateObject;
}
/**
* Register a gauge that reports the value of the {@link Number}.
* @param name Name of the gauge being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param number Thread-safe implementation of {@link Number} used to access the
* value.
* @param <T> The type of the number from which the gauge value is extracted.
* @return The number that was passed in so the registration can be done as part of an
* assignment statement.
*/
@Nullable
public <T extends Number> T gauge(String name, Iterable<Tag> tags, T number) {
return gauge(name, tags, number, Number::doubleValue);
}
/**
* Register a gauge that reports the value of the {@link Number}.
* @param name Name of the gauge being registered.
* @param number Thread-safe implementation of {@link Number} used to access the
* value.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return The number that was passed in so the registration can be done as part of an
* assignment statement.
*/
@Nullable
public <T extends Number> T gauge(String name, T number) {
return gauge(name, emptyList(), number);
}
/**
* Register a gauge that reports the value of the object.
* @param name Name of the gauge being registered.
* @param stateObject State object used to compute a value.
* @param valueFunction Function that produces an instantaneous gauge value from the
* state object.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return The state object that was passed in so the registration can be done as part
* of an assignment statement.
*/
@Nullable
public <T> T gauge(String name, T stateObject, ToDoubleFunction<T> valueFunction) {
return gauge(name, emptyList(), stateObject, valueFunction);
}
/**
* Register a gauge that reports the size of the {@link Collection}. The registration
* will keep a weak reference to the collection so it will not prevent garbage
* collection. The collection implementation used should be thread safe. Note that
* calling {@link Collection#size()} can be expensive for some collection
* implementations and should be considered before registering.
* @param name Name of the gauge being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param collection Thread-safe implementation of {@link Collection} used to access
* the value.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return The Collection that was passed in so the registration can be done as part
* of an assignment statement.
*/
@Nullable
public <T extends Collection<?>> T gaugeCollectionSize(String name, Iterable<Tag> tags, T collection) {
return gauge(name, tags, collection, Collection::size);
}
/**
* Register a gauge that reports the size of the {@link Map}. The registration will
* keep a weak reference to the collection so it will not prevent garbage collection.
* The collection implementation used should be thread safe. Note that calling
* {@link Map#size()} can be expensive for some collection implementations and should
* be considered before registering.
* @param name Name of the gauge being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param map Thread-safe implementation of {@link Map} used to access the value.
* @param <T> The type of the state object from which the gauge value is extracted.
* @return The Map that was passed in so the registration can be done as part of an
* assignment statement.
*/
@Nullable
public <T extends Map<?, ?>> T gaugeMapSize(String name, Iterable<Tag> tags, T map) {
return gauge(name, tags, map, Map::size);
}
private <M extends Meter> M registerMeterIfNecessary(Class<M> meterClass, Meter.Id id,
Function<Meter.Id, M> builder, Function<Meter.Id, M> noopBuilder) {
return registerMeterIfNecessary(meterClass, id, null, (id2, conf) -> builder.apply(id2), noopBuilder);
}
private <M extends Meter> M registerMeterIfNecessary(Class<M> meterClass, Meter.Id id,
@Nullable DistributionStatisticConfig config, BiFunction<Meter.Id, DistributionStatisticConfig, M> builder,
Function<Meter.Id, M> noopBuilder) {
Id mappedId = getMappedId(id);
Meter m = getOrCreateMeter(config, builder, id, mappedId, noopBuilder);
if (!meterClass.isInstance(m)) {
throw new IllegalArgumentException(
format("There is already a registered meter of a different type (%s vs. %s) with the same name: %s",
m.getClass().getSimpleName(), meterClass.getSimpleName(), id.getName()));
}
return meterClass.cast(m);
}
private Id getMappedId(Id id) {
if (id.syntheticAssociation() != null) {
return id;
}
Id mappedId = id;
for (MeterFilter filter : filters) {
mappedId = filter.map(mappedId);
}
return mappedId;
}
private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
BiFunction<Id, /* Nullable Generic */ DistributionStatisticConfig, ? extends Meter> builder, Id originalId,
Id mappedId, Function<Meter.Id, ? extends Meter> noopBuilder) {
Meter m = meterMap.get(mappedId);
if (m == null) {
if (isClosed()) {
return noopBuilder.apply(mappedId);
}
synchronized (meterMapLock) {
m = meterMap.get(mappedId);
if (m == null) {
if (!accept(mappedId)) {
return noopBuilder.apply(mappedId);
}
if (config != null) {
for (MeterFilter filter : filters) {
DistributionStatisticConfig filteredConfig = filter.configure(mappedId, config);
if (filteredConfig != null) {
config = filteredConfig;
}
}
}
m = builder.apply(mappedId, config);
Id synAssoc = mappedId.syntheticAssociation();
if (synAssoc != null) {
Set<Id> associations = syntheticAssociations.computeIfAbsent(synAssoc, k -> new HashSet<>());
associations.add(mappedId);
}
for (Consumer<Meter> onAdd : meterAddedListeners) {
onAdd.accept(m);
}
meterMap.put(mappedId, m);
}
}
}
return m;
}
private boolean accept(Meter.Id id) {
for (MeterFilter filter : filters) {
MeterFilterReply reply = filter.accept(id);
if (reply == MeterFilterReply.DENY) {
return false;
}
else if (reply == MeterFilterReply.ACCEPT) {
return true;
}
}
return true;
}
/**
* Remove a {@link Meter} from this {@link MeterRegistry registry}. This is expected
* to be a {@link Meter} with the same {@link Id} returned when registering a meter -
* which will have {@link MeterFilter}s applied to it.
* @param meter The meter to remove
* @return The removed meter, or null if the provided meter is not currently
* registered.
* @since 1.1.0
*/
@Incubating(since = "1.1.0")
@Nullable
public Meter remove(Meter meter) {
return remove(meter.getId());
}
/**
* Remove a {@link Meter} from this {@link MeterRegistry registry} based on its
* {@link Id} before applying this registry's {@link MeterFilter}s to the given
* {@link Id}.
* @param preFilterId the id of the meter to remove
* @return The removed meter, or null if the meter is not found
* @since 1.3.16
*/
@Incubating(since = "1.3.16")
@Nullable
public Meter removeByPreFilterId(Meter.Id preFilterId) {
return remove(getMappedId(preFilterId));
}
/**
* Remove a {@link Meter} from this {@link MeterRegistry registry} based the given
* {@link Id} as-is. The registry's {@link MeterFilter}s will not be applied to it.
* You can use the {@link Id} of the {@link Meter} returned when registering a meter,
* since that will have {@link MeterFilter}s already applied to it.
* @param mappedId The id of the meter to remove
* @return The removed meter, or null if no meter matched the provided id.
* @since 1.1.0
*/
@Incubating(since = "1.1.0")
@Nullable
public Meter remove(Meter.Id mappedId) {
Meter m = meterMap.get(mappedId);
if (m != null) {
synchronized (meterMapLock) {
m = meterMap.remove(mappedId);
if (m != null) {
Set<Id> synthetics = syntheticAssociations.remove(mappedId);
if (synthetics != null) {
for (Id synthetic : synthetics) {
remove(synthetic);
}
}
for (Consumer<Meter> onRemove : meterRemovedListeners) {
onRemove.accept(m);
}
return m;
}
}
}
return null;
}
/**
* Clear all meters.
* @since 1.2.0
*/
@Incubating(since = "1.2.0")
public void clear() {
meterMap.keySet().forEach(this::remove);
}
/**
* Access to configuration options for this registry.
*/
public class Config {
/**
* Append a list of common tags to apply to all metrics reported to the monitoring
* system.
* @param tags Tags to add to every metric.
* @return This configuration instance.
*/
public Config commonTags(Iterable<Tag> tags) {
return meterFilter(MeterFilter.commonTags(tags));
}
/**
* Append a list of common tags to apply to all metrics reported to the monitoring
* system. Must be an even number of arguments representing key/value pairs of
* tags.
* @param tags MUST be an even number of arguments representing key/value pairs of
* tags.
* @return This configuration instance.
*/
public Config commonTags(String... tags) {
return commonTags(Tags.of(tags));
}
/**
* Add a meter filter to the registry. Filters are applied in the order in which
* they are added.
* @param filter The filter to add to the registry.
* @return This configuration instance.
*/
public synchronized Config meterFilter(MeterFilter filter) {
MeterFilter[] newFilters = new MeterFilter[filters.length + 1];
System.arraycopy(filters, 0, newFilters, 0, filters.length);
newFilters[filters.length] = filter;
filters = newFilters;
return this;
}
/**
* Register an event listener for each meter added to the registry.
* @param meterAddedListener a meter-added event listener to be added
* @return This configuration instance.
*/
public Config onMeterAdded(Consumer<Meter> meterAddedListener) {
meterAddedListeners.add(meterAddedListener);
return this;
}
/**
* Register an event listener for each meter removed from the registry.
* @param meterRemovedListener a meter-removed event listener to be added
* @return This configuration instance.
* @since 1.1.0
*/
@Incubating(since = "1.1.0")
public Config onMeterRemoved(Consumer<Meter> meterRemovedListener) {
meterRemovedListeners.add(meterRemovedListener);
return this;
}
/**
* Register an event listener for meter registration failures.
* @param meterRegistrationFailedListener An event listener for meter registration
* failures
* @return This configuration instance
* @since 1.6.0
*/
@Incubating(since = "1.6.0")
public Config onMeterRegistrationFailed(BiConsumer<Id, String> meterRegistrationFailedListener) {
meterRegistrationFailedListeners.add(meterRegistrationFailedListener);
return this;
}
/**
* Use the provided naming convention, overriding the default for your monitoring
* system.
* @param convention The naming convention to use.
* @return This configuration instance.
*/
public Config namingConvention(NamingConvention convention) {
namingConvention = convention;
return this;
}
/**
* @return The naming convention currently in use on this registry.
*/
public NamingConvention namingConvention() {
return namingConvention;
}
/**
* @return The clock used to measure durations of timers and long task timers (and
* sometimes influences publishing behavior).
*/
public Clock clock() {
return clock;
}
/**
* Sets the default pause detector to use for all timers in this registry.
* @param detector The pause detector to use.
* @return This configuration instance.
* @see io.micrometer.core.instrument.distribution.pause.NoPauseDetector
* @see io.micrometer.core.instrument.distribution.pause.ClockDriftPauseDetector
*/
public Config pauseDetector(PauseDetector detector) {
pauseDetector = detector;
return this;
}
MeterFilter[] getFilters() {
return filters;
}
/**
* Merges the provided configuration with this one.
* @param config configuration to merge
* @return this configuration with merged elements from the provided configuration
* @since 1.9.18
*/
public Config merge(Config config) {
for (MeterFilter filter : config.getFilters()) {
meterFilter(filter);
}
// TODO: What else should we merge?
return this;
}
/**
* @return The pause detector that is currently in effect.
*/
public PauseDetector pauseDetector() {
return pauseDetector;
}
}
/**
* Additional, less commonly used meter types.
*/
public class More {
/**
* Measures the time taken for long tasks.
* @param name Name of the long task timer being registered.
* @param tags MUST be an even number of arguments representing key/value pairs of
* tags.
* @return A new or existing long task timer.
*/
public LongTaskTimer longTaskTimer(String name, String... tags) {
return longTaskTimer(name, Tags.of(tags));
}
/**
* Measures the time taken for long tasks.
* @param name Name of the long task timer being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @return A new or existing long task timer.
*/
public LongTaskTimer longTaskTimer(String name, Iterable<Tag> tags) {
return LongTaskTimer.builder(name).tags(tags).register(MeterRegistry.this);
}
/**
* Only used by {@link LongTaskTimer#builder(String)}.
* @param id The identifier for this long task timer.
* @return A new or existing long task timer.
*/
LongTaskTimer longTaskTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) {
return registerMeterIfNecessary(LongTaskTimer.class, id, distributionStatisticConfig,
(id2, filteredConfig) -> {
Meter.Id withUnit = id2.withBaseUnit(getBaseTimeUnitStr());
return newLongTaskTimer(withUnit, filteredConfig.merge(defaultHistogramConfig()));
}, NoopLongTaskTimer::new);
}
/**
* Tracks a monotonically increasing value, automatically incrementing the counter
* whenever the value is observed.
* @param name Name of the counter being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param obj State object used to compute a value.
* @param countFunction Function that produces a monotonically increasing counter
* value from the state object.
* @param <T> The type of the state object from which the counter value is
* extracted.
* @return A new or existing function counter.
*/
public <T> FunctionCounter counter(String name, Iterable<Tag> tags, T obj, ToDoubleFunction<T> countFunction) {
return FunctionCounter.builder(name, obj, countFunction).tags(tags).register(MeterRegistry.this);
}
/**
* Tracks a number, maintaining a weak reference on it.
* @param name Name of the counter being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param number A monotonically increasing number to track.
* @param <T> The type of the state object from which the counter value is
* extracted.
* @return A new or existing function counter.
*/
public <T extends Number> FunctionCounter counter(String name, Iterable<Tag> tags, T number) {
return FunctionCounter.builder(name, number, Number::doubleValue).tags(tags).register(MeterRegistry.this);
}
/**
* Tracks a number, maintaining a weak reference on it.
* @param id The identifier for this function counter.
* @param obj State object used to compute a value.
* @param countFunction Function that produces a monotonically increasing counter
* value from the state object.
* @param <T> The type of the state object from which the counter value is
* extracted.
* @return A new or existing function counter.
*/
<T> FunctionCounter counter(Meter.Id id, T obj, ToDoubleFunction<T> countFunction) {
return registerMeterIfNecessary(FunctionCounter.class, id,
id2 -> newFunctionCounter(id2, obj, countFunction), NoopFunctionCounter::new);
}
/**
* A timer that tracks monotonically increasing functions for count and totalTime.
* @param name Name of the timer being registered.
* @param tags Sequence of dimensions for breaking down the name.
* @param obj State object used to compute a value.
* @param countFunction Function that produces a monotonically increasing counter
* value from the state object.
* @param totalTimeFunction Function that produces a monotonically increasing
* total time value from the state object.
* @param totalTimeFunctionUnit The base unit of time produced by the total time
* function.
* @param <T> The type of the state object from which the function values are
* extracted.
* @return A new or existing function timer.
*/
public <T> FunctionTimer timer(String name, Iterable<Tag> tags, T obj, ToLongFunction<T> countFunction,
ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit) {
return FunctionTimer.builder(name, obj, countFunction, totalTimeFunction, totalTimeFunctionUnit)
.tags(tags)
.register(MeterRegistry.this);
}
/**
* A timer that tracks monotonically increasing functions for count and totalTime.
* @param id The identifier for this function timer.
* @param obj State object used to compute a value.
* @param countFunction Function that produces a monotonically increasing counter
* value from the state object.
* @param totalTimeFunction Function that produces a monotonically increasing
* total time value from the state object.
* @param totalTimeFunctionUnit The base unit of time produced by the total time
* function.
* @param <T> The type of the state object from which the function values are
* extracted.F
* @return A new or existing function timer.
*/
<T> FunctionTimer timer(Meter.Id id, T obj, ToLongFunction<T> countFunction,
ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit) {
return registerMeterIfNecessary(FunctionTimer.class, id, id2 -> {