33import com .codahale .metrics .ConsoleReporter ;
44import com .codahale .metrics .Metric ;
55import com .codahale .metrics .MetricRegistry ;
6- import com .codahale .metrics .MetricSet ;
7- import com .codahale .metrics .jvm .GarbageCollectorMetricSet ;
8- import com .codahale .metrics .jvm .JvmAttributeGaugeSet ;
9- import com .codahale .metrics .jvm .MemoryUsageGaugeSet ;
10- import com .codahale .metrics .jvm .ThreadStatesGaugeSet ;
116import com .google .common .cache .Cache ;
127import com .typesafe .config .Config ;
138import io .github .mweirauch .micrometer .jvm .extras .ProcessMemoryMetrics ;
2520import io .micrometer .core .instrument .binder .jvm .ClassLoaderMetrics ;
2621import io .micrometer .core .instrument .binder .jvm .ExecutorServiceMetrics ;
2722import io .micrometer .core .instrument .binder .jvm .JvmGcMetrics ;
23+ import io .micrometer .core .instrument .binder .jvm .JvmHeapPressureMetrics ;
24+ import io .micrometer .core .instrument .binder .jvm .JvmInfoMetrics ;
2825import io .micrometer .core .instrument .binder .jvm .JvmMemoryMetrics ;
2926import io .micrometer .core .instrument .binder .jvm .JvmThreadMetrics ;
3027import io .micrometer .core .instrument .binder .logging .Log4j2Metrics ;
28+ import io .micrometer .core .instrument .binder .system .FileDescriptorMetrics ;
3129import io .micrometer .core .instrument .binder .system .ProcessorMetrics ;
3230import io .micrometer .core .instrument .binder .system .UptimeMetrics ;
3331import io .micrometer .core .instrument .composite .CompositeMeterRegistry ;
@@ -88,16 +86,10 @@ public class PlatformMetricsRegistry {
8886 private static final String METRICS_DEFAULT_TAGS_CONFIG_KEY = "defaultTags" ;
8987
9088 private static final MetricRegistry METRIC_REGISTRY = new MetricRegistry ();
91- private static ConsoleReporter consoleReporter ;
92- private static String metricsPrefix ;
9389 public static final List <String > DEFAULT_METRICS_REPORTERS = List .of ("prometheus" );
94- private static final Map <String , MetricSet > DEFAULT_METRIC_SET = new HashMap <>() {{
95- put ("gc" , new GarbageCollectorMetricSet ());
96- put ("jvm" , new JvmAttributeGaugeSet ());
97- put ("memory" , new MemoryUsageGaugeSet ());
98- put ("thread" , new ThreadStatesGaugeSet ());
9990
100- }};
91+ private static ConsoleReporter consoleReporter ;
92+ private static String metricsPrefix ;
10193 private static boolean isInit = false ;
10294
10395 /**
@@ -266,21 +258,27 @@ public synchronized static void initMetricsRegistry(String serviceName, Config c
266258 });
267259
268260 // Register different metrics with the registry.
261+
262+ // JVM metrics
263+ new JvmInfoMetrics ().bindTo (meterRegistry );
269264 new ClassLoaderMetrics ().bindTo (meterRegistry );
270- new JvmGcMetrics ().bindTo (meterRegistry );
271- new ProcessorMetrics ().bindTo (meterRegistry );
272265 new JvmThreadMetrics ().bindTo (meterRegistry );
273266 new JvmMemoryMetrics ().bindTo (meterRegistry );
267+ new JvmGcMetrics ().bindTo (meterRegistry );
268+ new JvmHeapPressureMetrics ().bindTo (meterRegistry );
269+
270+ // process metrics - micrometer builtin
274271 new UptimeMetrics ().bindTo (meterRegistry );
272+ new ProcessorMetrics ().bindTo (meterRegistry );
273+ new FileDescriptorMetrics ().bindTo (meterRegistry );
274+
275+ // logging metrics
275276 new Log4j2Metrics ().bindTo (meterRegistry );
276277
278+ // process metrics - third party
277279 new ProcessMemoryMetrics ().bindTo (meterRegistry );
278280 new ProcessThreadMetrics ().bindTo (meterRegistry );
279281
280- for (String key : DEFAULT_METRIC_SET .keySet ()) {
281- METRIC_REGISTRY
282- .registerAll (String .format ("%s.%s" , metricsPrefix , key ), DEFAULT_METRIC_SET .get (key ));
283- }
284282 isInit = true ;
285283 }
286284
@@ -324,14 +322,27 @@ public static Timer registerTimer(String name, Map<String, String> tags) {
324322 * default tags also will be reported with the metrics.
325323 *
326324 * See https://micrometer.io/docs/concepts#_timers for more details on the Timer.
325+ *
326+ * Defaults the timer range from 1 second to 60 seconds (both inclusive)
327327 */
328328 public static Timer registerTimer (String name , Map <String , String > tags , boolean histogram ) {
329+ return registerTimer (name , tags , histogram , Duration .ofSeconds (1 ), Duration .ofSeconds (60 ));
330+ }
331+
332+ /**
333+ * Registers a Timer with the given name with the service's metric registry and reports it
334+ * periodically to the configured reporters. Apart from the given tags, the reporting service's
335+ * default tags also will be reported with the metrics.
336+ *
337+ * See https://micrometer.io/docs/concepts#_timers for more details on the Timer.
338+ */
339+ public static Timer registerTimer (String name , Map <String , String > tags , boolean histogram , Duration minExpectedValue , Duration maxExpectedValue ) {
329340 Timer .Builder builder = Timer .builder (name )
330- .publishPercentiles (0.5 , 0.95 , 0.99 )
331- .tags (toIterable (tags ));
341+ .publishPercentiles (0.5 , 0.75 , 0.90 , 0.95 , 0.99 )
342+ .tags (toIterable (tags ));
332343
333344 if (histogram ) {
334- builder = builder .publishPercentileHistogram ();
345+ builder = builder .minimumExpectedValue ( minExpectedValue ). maximumExpectedValue ( maxExpectedValue ). publishPercentileHistogram ();
335346 }
336347 return builder .register (meterRegistry );
337348 }
@@ -356,8 +367,7 @@ public static <T extends Number> T registerGauge(String name, Map<String, String
356367 * <p>
357368 * See https://micrometer.io/docs/concepts#_distribution_summaries for more details.
358369 */
359- public static DistributionSummary registerDistributionSummary (String name ,
360- Map <String , String > tags ) {
370+ public static DistributionSummary registerDistributionSummary (String name , Map <String , String > tags ) {
361371 return registerDistributionSummary (name , tags , false );
362372 }
363373
@@ -371,13 +381,26 @@ public static DistributionSummary registerDistributionSummary(String name,
371381 * For more details - https://micrometer.io/docs/concepts#_distribution_summaries,
372382 * https://micrometer.io/docs/concepts#_histograms_and_percentiles
373383 */
374- public static DistributionSummary registerDistributionSummary (String name ,
375- Map <String , String > tags , boolean histogram ) {
384+ public static DistributionSummary registerDistributionSummary (String name , Map <String , String > tags , boolean histogram ) {
385+ return registerDistributionSummary (name , tags , histogram , null , null );
386+ }
387+
388+ /**
389+ * Registers a DistributionSummary for the given name with the service's metric registry and
390+ * reports it periodically to the configured reporters Apart from the provided tags, the reporting
391+ * service's default tags also will be reported with the metrics.
392+ * <p>
393+ * Param histogram – Determines whether percentile histograms should be published.
394+ * <p>
395+ * For more details - https://micrometer.io/docs/concepts#_distribution_summaries,
396+ * https://micrometer.io/docs/concepts#_histograms_and_percentiles
397+ */
398+ public static DistributionSummary registerDistributionSummary (String name , Map <String , String > tags , boolean histogram , Double minExpectedValue , Double maxExpectedValue ) {
376399 DistributionSummary .Builder builder = DistributionSummary .builder (name )
377- .publishPercentiles (0.5 , 0.95 , 0.99 )
378- .tags (toIterable (tags ));
400+ .publishPercentiles (0.5 , 0.75 , 0.90 , 0.95 , 0.99 )
401+ .tags (toIterable (tags ));
379402 if (histogram ) {
380- builder = builder .publishPercentileHistogram ();
403+ builder = builder .minimumExpectedValue ( minExpectedValue ). maximumExpectedValue ( maxExpectedValue ). publishPercentileHistogram ();
381404 }
382405 return builder .register (meterRegistry );
383406 }
0 commit comments