@ -14,16 +14,19 @@ package tech.pegasys.pantheon.metrics.prometheus;
import static java.util.Arrays.asList ;
import static java.util.Collections.singleton ;
import static tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem.NO_OP_COLLECTOR ;
import tech.pegasys.pantheon.metrics.LabelledMetric ;
import tech.pegasys.pantheon.metrics.MetricCategory ;
import tech.pegasys.pantheon.metrics.MetricsSystem ;
import tech.pegasys.pantheon.metrics.Observation ;
import tech.pegasys.pantheon.metrics.OperationTimer ;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem ;
import java.util.ArrayList ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.EnumSet ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.ConcurrentHashMap ;
@ -55,20 +58,31 @@ public class PrometheusMetricsSystem implements MetricsSystem {
cachedTimers = new ConcurrentHashMap < > ( ) ;
private final Map < String , Collector > cachedGauges = new ConcurrentHashMap < > ( ) ;
private final EnumSet < MetricCategory > enabledCategories = EnumSet . allOf ( MetricCategory . class ) ;
PrometheusMetricsSystem ( ) { }
public static MetricsSystem init ( ) {
public static MetricsSystem init ( final MetricsConfiguration metricsConfiguration ) {
if ( ! metricsConfiguration . isEnabled ( ) ) {
return new NoOpMetricsSystem ( ) ;
}
final PrometheusMetricsSystem metricsSystem = new PrometheusMetricsSystem ( ) ;
metricsSystem . collectors . put (
MetricCategory . PROCESS , singleton ( new StandardExports ( ) . register ( metricsSystem . registry ) ) ) ;
metricsSystem . collectors . put (
MetricCategory . JVM ,
asList (
new MemoryPoolsExports ( ) . register ( metricsSystem . registry ) ,
new BufferPoolsExports ( ) . register ( metricsSystem . registry ) ,
new GarbageCollectorExports ( ) . register ( metricsSystem . registry ) ,
new ThreadExports ( ) . register ( metricsSystem . registry ) ,
new ClassLoadingExports ( ) . register ( metricsSystem . registry ) ) ) ;
metricsSystem . enabledCategories . retainAll ( metricsConfiguration . getMetricCategories ( ) ) ;
if ( metricsSystem . enabledCategories . contains ( MetricCategory . PROCESS ) ) {
metricsSystem . collectors . put (
MetricCategory . PROCESS ,
singleton ( new StandardExports ( ) . register ( metricsSystem . registry ) ) ) ;
}
if ( metricsSystem . enabledCategories . contains ( MetricCategory . JVM ) ) {
metricsSystem . collectors . put (
MetricCategory . JVM ,
asList (
new MemoryPoolsExports ( ) . register ( metricsSystem . registry ) ,
new BufferPoolsExports ( ) . register ( metricsSystem . registry ) ,
new GarbageCollectorExports ( ) . register ( metricsSystem . registry ) ,
new ThreadExports ( ) . register ( metricsSystem . registry ) ,
new ClassLoadingExports ( ) . register ( metricsSystem . registry ) ) ) ;
}
return metricsSystem ;
}
@ -82,9 +96,13 @@ public class PrometheusMetricsSystem implements MetricsSystem {
return cachedCounters . computeIfAbsent (
metricName ,
( k ) - > {
final Counter counter = Counter . build ( metricName , help ) . labelNames ( labelNames ) . create ( ) ;
addCollector ( category , counter ) ;
return new PrometheusCounter ( counter ) ;
if ( enabledCategories . contains ( category ) ) {
final Counter counter = Counter . build ( metricName , help ) . labelNames ( labelNames ) . create ( ) ;
addCollector ( category , counter ) ;
return new PrometheusCounter ( counter ) ;
} else {
return NoOpMetricsSystem . NO_OP_LABELLED_COUNTER ;
}
} ) ;
}
@ -98,18 +116,22 @@ public class PrometheusMetricsSystem implements MetricsSystem {
return cachedTimers . computeIfAbsent (
metricName ,
( k ) - > {
final Summary summary =
Summary . build ( metricName , help )
. quantile ( 0 . 2 , 0 . 02 )
. quantile ( 0 . 5 , 0 . 05 )
. quantile ( 0 . 8 , 0 . 02 )
. quantile ( 0 . 95 , 0 . 005 )
. quantile ( 0 . 99 , 0 . 001 )
. quantile ( 1 . 0 , 0 )
. labelNames ( labelNames )
. create ( ) ;
addCollector ( category , summary ) ;
return new PrometheusTimer ( summary ) ;
if ( enabledCategories . contains ( category ) ) {
final Summary summary =
Summary . build ( metricName , help )
. quantile ( 0 . 2 , 0 . 02 )
. quantile ( 0 . 5 , 0 . 05 )
. quantile ( 0 . 8 , 0 . 02 )
. quantile ( 0 . 95 , 0 . 005 )
. quantile ( 0 . 99 , 0 . 001 )
. quantile ( 1 . 0 , 0 )
. labelNames ( labelNames )
. create ( ) ;
addCollector ( category , summary ) ;
return new PrometheusTimer ( summary ) ;
} else {
return NoOpMetricsSystem . NO_OP_LABELLED_TIMER ;
}
} ) ;
}
@ -123,9 +145,13 @@ public class PrometheusMetricsSystem implements MetricsSystem {
cachedGauges . computeIfAbsent (
metricName ,
( k ) - > {
Collector collector = new CurrentValueCollector ( metricName , help , valueSupplier ) ;
addCollector ( category , collector ) ;
return collector ;
if ( enabledCategories . contains ( category ) ) {
final Collector collector = new CurrentValueCollector ( metricName , help , valueSupplier ) ;
addCollector ( category , collector ) ;
return collector ;
} else {
return NO_OP_COLLECTOR ;
}
} ) ;
}