1212import com .typesafe .config .Config ;
1313import io .github .mweirauch .micrometer .jvm .extras .ProcessMemoryMetrics ;
1414import io .github .mweirauch .micrometer .jvm .extras .ProcessThreadMetrics ;
15+ import io .micrometer .common .util .StringUtils ;
1516import io .micrometer .core .instrument .Clock ;
1617import io .micrometer .core .instrument .Counter ;
1718import io .micrometer .core .instrument .DistributionSummary ;
3334import io .micrometer .core .instrument .logging .LoggingMeterRegistry ;
3435import io .micrometer .core .instrument .logging .LoggingRegistryConfig ;
3536import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
36- import io .micrometer .core .instrument .util .StringUtils ;
3737import io .micrometer .core .lang .NonNull ;
3838import io .micrometer .core .lang .Nullable ;
3939import io .micrometer .prometheus .PrometheusConfig ;
4242import io .prometheus .client .dropwizard .DropwizardExports ;
4343import io .prometheus .client .exporter .PushGateway ;
4444import java .time .Duration ;
45+ import java .util .ArrayList ;
4546import java .util .HashMap ;
4647import java .util .HashSet ;
4748import java .util .List ;
@@ -98,20 +99,19 @@ public class PlatformMetricsRegistry {
9899
99100 }};
100101 private static boolean isInit = false ;
101- private static final Set <Tag > DEFAULT_TAGS = new HashSet <>();
102102
103103 /**
104104 * Main MetricMeter registry, with which all the metrics should be registered. We use
105105 * a {@link CompositeMeterRegistry} here so that we can even registry multiple registries
106106 * like Prometheus and Logging registries, if needed.
107107 */
108- private static final CompositeMeterRegistry METER_REGISTRY = new CompositeMeterRegistry ();
108+ private static CompositeMeterRegistry meterRegistry = new CompositeMeterRegistry ();
109109
110110 private static void initPrometheusReporter (int reportInterval ) {
111111 LOGGER .info ("Trying to init PrometheusReporter" );
112112
113113 // Add Prometheus registry to the composite registry.
114- METER_REGISTRY .add (new PrometheusMeterRegistry (new PrometheusConfig () {
114+ meterRegistry .add (new PrometheusMeterRegistry (new PrometheusConfig () {
115115 @ Override
116116 @ NonNull
117117 public Duration step () {
@@ -138,7 +138,7 @@ private static void initConsoleMetricsReporter(final int reportIntervalSec) {
138138 private static void initLoggingMetricsReporter (int reportIntervalSec ) {
139139 LOGGER .info ("Initializing the logging metric reporter." );
140140
141- METER_REGISTRY .add (new LoggingMeterRegistry (new LoggingRegistryConfig () {
141+ meterRegistry .add (new LoggingMeterRegistry (new LoggingRegistryConfig () {
142142 @ Override
143143 @ NonNull
144144 public Duration step () {
@@ -156,7 +156,7 @@ public String get(String key) {
156156 private static void initTestingMetricsReporter () {
157157 LOGGER .info ("Initializing the testing metric reporter." );
158158
159- METER_REGISTRY .add (new SimpleMeterRegistry ());
159+ meterRegistry .add (new SimpleMeterRegistry ());
160160 }
161161
162162 private static void initPrometheusPushGatewayReporter (String serviceName ,
@@ -170,7 +170,7 @@ private static void initPrometheusPushGatewayReporter(String serviceName,
170170 throw new IllegalArgumentException ("pushUrlAddress configuration is not specified." );
171171 }
172172
173- METER_REGISTRY .add (new PrometheusPushMeterRegistry (
173+ meterRegistry .add (new PrometheusPushMeterRegistry (
174174 new PrometheusPushRegistryConfig () {
175175 @ Override
176176 public String jobName () {
@@ -261,19 +261,21 @@ public synchronized static void initMetricsRegistry(String serviceName, Config c
261261 }
262262
263263 LOGGER .info ("Setting default tags for all metrics to: {}" , defaultTags );
264- defaultTags .forEach ((key , value ) -> DEFAULT_TAGS .add (new ImmutableTag (key , value )));
264+ defaultTags .forEach ((key , value ) -> {
265+ meterRegistry .config ().commonTags (List .of ((new ImmutableTag (key , value ))));
266+ });
265267
266268 // Register different metrics with the registry.
267- new ClassLoaderMetrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
268- new JvmGcMetrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
269- new ProcessorMetrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
270- new JvmThreadMetrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
271- new JvmMemoryMetrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
272- new UptimeMetrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
273- new Log4j2Metrics (DEFAULT_TAGS ).bindTo (METER_REGISTRY );
269+ new ClassLoaderMetrics ().bindTo (meterRegistry );
270+ new JvmGcMetrics ().bindTo (meterRegistry );
271+ new ProcessorMetrics ().bindTo (meterRegistry );
272+ new JvmThreadMetrics ().bindTo (meterRegistry );
273+ new JvmMemoryMetrics ().bindTo (meterRegistry );
274+ new UptimeMetrics ().bindTo (meterRegistry );
275+ new Log4j2Metrics ().bindTo (meterRegistry );
274276
275- new ProcessMemoryMetrics ().bindTo (METER_REGISTRY );
276- new ProcessThreadMetrics ().bindTo (METER_REGISTRY );
277+ new ProcessMemoryMetrics ().bindTo (meterRegistry );
278+ new ProcessThreadMetrics ().bindTo (meterRegistry );
277279
278280 for (String key : DEFAULT_METRIC_SET .keySet ()) {
279281 METRIC_REGISTRY
@@ -302,7 +304,7 @@ public static void register(String metricName, Metric metric) {
302304 * See https://micrometer.io/docs/concepts#_counters for more details on the Counter.
303305 */
304306 public static Counter registerCounter (String name , Map <String , String > tags ) {
305- return METER_REGISTRY .counter (name , addDefaultTags (tags ));
307+ return meterRegistry .counter (name , toIterable (tags ));
306308 }
307309
308310 /**
@@ -326,11 +328,12 @@ public static Timer registerTimer(String name, Map<String, String> tags) {
326328 public static Timer registerTimer (String name , Map <String , String > tags , boolean histogram ) {
327329 Timer .Builder builder = Timer .builder (name )
328330 .publishPercentiles (0.5 , 0.95 , 0.99 )
329- .tags (addDefaultTags (tags ));
331+ .tags (toIterable (tags ));
332+
330333 if (histogram ) {
331334 builder = builder .publishPercentileHistogram ();
332335 }
333- return builder .register (METER_REGISTRY );
336+ return builder .register (meterRegistry );
334337 }
335338
336339 /**
@@ -341,7 +344,7 @@ public static Timer registerTimer(String name, Map<String, String> tags, boolean
341344 * See https://micrometer.io/docs/concepts#_gauges for more details on the Gauges.
342345 */
343346 public static <T extends Number > T registerGauge (String name , Map <String , String > tags , T number ) {
344- Gauge .builder (name , number , Number ::doubleValue ).tags (addDefaultTags (tags )).strongReference (true ).register (METER_REGISTRY );
347+ Gauge .builder (name , number , Number ::doubleValue ).tags (toIterable (tags )).strongReference (true ).register (meterRegistry );
345348 return number ;
346349 }
347350
@@ -372,19 +375,19 @@ public static DistributionSummary registerDistributionSummary(String name,
372375 Map <String , String > tags , boolean histogram ) {
373376 DistributionSummary .Builder builder = DistributionSummary .builder (name )
374377 .publishPercentiles (0.5 , 0.95 , 0.99 )
375- .tags (addDefaultTags (tags ));
378+ .tags (toIterable (tags ));
376379 if (histogram ) {
377380 builder = builder .publishPercentileHistogram ();
378381 }
379- return builder .register (METER_REGISTRY );
382+ return builder .register (meterRegistry );
380383 }
381384
382385 /**
383386 * Registers metrics for GuavaCaches using micrometer's GuavaCacheMetrics under the given
384387 * cacheName for the given guavaCache
385388 */
386389 public static <K , V > void registerCache (String cacheName , Cache <K , V > guavaCache , Map <String , String > tags ) {
387- GuavaCacheMetrics .monitor (METER_REGISTRY , guavaCache , cacheName , addDefaultTags (tags ));
390+ GuavaCacheMetrics .monitor (meterRegistry , guavaCache , cacheName , toIterable (tags ));
388391
389392 }
390393
@@ -397,16 +400,16 @@ public static <K, V> void registerCache(String cacheName, Cache<K, V> guavaCache
397400 */
398401 public static void monitorExecutorService (String name , ExecutorService executorService ,
399402 @ Nullable Map <String , String > tags ) {
400- new ExecutorServiceMetrics (executorService , name , addDefaultTags (tags )).bindTo (METER_REGISTRY );
403+ new ExecutorServiceMetrics (executorService , name , toIterable (tags )).bindTo (meterRegistry );
401404 }
402405
403- private static Iterable <Tag > addDefaultTags (Map <String , String > tags ) {
404- if (tags == null || tags .isEmpty ()) {
405- return DEFAULT_TAGS ;
406+ private static Iterable <Tag > toIterable (Map <String , String > tags ) {
407+ List <Tag > newTags = new ArrayList <>();
408+
409+ if (tags != null ) {
410+ tags .forEach ((k , v ) -> newTags .add (new ImmutableTag (k , v )));
406411 }
407412
408- Set <Tag > newTags = new HashSet <>(DEFAULT_TAGS );
409- tags .forEach ((k , v ) -> newTags .add (new ImmutableTag (k , v )));
410413 return newTags ;
411414 }
412415
@@ -415,22 +418,22 @@ public static MetricRegistry getMetricRegistry() {
415418 }
416419
417420 public static MeterRegistry getMeterRegistry () {
418- return METER_REGISTRY ;
421+ return meterRegistry ;
419422 }
420423
421424 public static synchronized void stop () {
422425 stopConsoleMetricsReporter ();
423426 METRIC_REGISTRY .getNames ().forEach (METRIC_REGISTRY ::remove );
424427
425- DEFAULT_TAGS .clear ();
426428 /* For each meter registry in this composite, it will call the close function */
427- METER_REGISTRY .getRegistries ().forEach (MeterRegistry ::close );
428- METER_REGISTRY .forEachMeter (METER_REGISTRY ::remove );
429- METER_REGISTRY .getRegistries ().forEach (MeterRegistry ::clear );
430- Set <MeterRegistry > registries = new HashSet <>(METER_REGISTRY .getRegistries ());
431- registries .forEach (METER_REGISTRY ::remove );
429+ meterRegistry .getRegistries ().forEach (MeterRegistry ::close );
430+ meterRegistry .forEachMeter (meterRegistry ::remove );
431+ meterRegistry .getRegistries ().forEach (MeterRegistry ::clear );
432+ Set <MeterRegistry > registries = new HashSet <>(meterRegistry .getRegistries ());
433+ registries .forEach (meterRegistry ::remove );
432434 registries .clear ();
433435 CollectorRegistry .defaultRegistry .clear ();
436+ meterRegistry = new CompositeMeterRegistry ();
434437 isInit = false ;
435438 }
436439
0 commit comments