Package com.ocient.metrics
Class MetricsRegistry
java.lang.Object
com.ocient.metrics.MetricsRegistry
public class MetricsRegistry extends Object
Global Registry for all performance counters.
In the example below I've added both INCREMENTAL_VALUE and INSTANTANEOUS_VALUE performance counters for a queue implementation with add() and poll() methods. The perf counters are used to track:
- Cumulative number of elements processed by the queue (Countertype.INCREMENTAL)
- Cumulative number of bytes processed by the queue (Countertype.INCREMENTAL)
- Cumulative number of elements currently in the queue (Countertype.INSTANTANEOUS)
- Cumulative number of bytes currently consumed by the queue (Countertype.INSTANTANEOUS)
private static class ExampleQueue {
PerfCounter m_numElementsProcessed;
PerfCounter m_numBytesProcessed;
PerfCounter m_numElements;
PerfCounter m_numBytesUsed;
ExampleQueue(final int processingGroup, final int processingCore) {
// Generate paths for all of the counters
final List<NamedTag> tags = Arrays.asList(
NamedTag.id(Tag.PROCESSING_GROUP, processingGroup),
NamedTag.id(Tag.PROCESSING_CORE, processingCore));
final Path nepPath = Path.create("extractor.exampleQueue.numElementsProcessed", tags);
final Path nbpPath = Path.create("extractor.exampleQueue.numBytesProcessed", tags);
final Path cnbPath = Path.create("extractor.exampleQueue.numElements", tags);
final Path cnePath = Path.create("extractor.exampleQueue.numBytesUsed", tags);
// Maintain references to these counters to avoid subsequent map lookups
this.m_numElementsProcessed = MetricsRegistry.instance().getCounter(nepPath, ThreadingModel.NO_CONTENTION, DataType.LONG, CounterType.INCREMENTAL_VALUE, Units.UNITLESS);
this.m_numBytesProcessed = MetricsRegistry.instance().getCounter(nbpPath, ThreadingModel.NO_CONTENTION, DataType.LONG, CounterType.INCREMENTAL_VALUE, Units.BYTES);
this.m_numElements = MetricsRegistry.instance().getCounter(cnbPath, ThreadingModel.NO_CONTENTION, DataType.LONG, CounterType.INSTANTANEOUS_VALUE, Units.UNITLESS);
this.m_numBytesUsed = MetricsRegistry.instance().getCounter(cnePath, ThreadingModel.NO_CONTENTION, DataType.LONG, CounterType.INSTANTANEOUS_VALUE, Units.BYTES);
}
void shutdown() {
MetricsRegistry.instance().removeCounter(this.m_numElementsProcessed.path());
MetricsRegistry.instance().removeCounter(this.m_numBytesProcessed.path());
MetricsRegistry.instance().removeCounter(this.m_numElements.path());
MetricsRegistry.instance().removeCounter(this.m_numBytesUsed.path());
}
void add(final Object o) {
//... store o in memory
this.m_numElements.incValue(1);
this.m_numBytesUsed.incValue(getBytes(o));
}
Object poll() {
//... poll object
this.m_numElements.decValue(1);
this.m_numBytesUsed.decValue(getBytes(o));
this.m_numElementsProcessed.incValue(1);
this.m_numBytesProcessed.incValue(getBytes(o));
//... return polled object
}
}
-
Method Summary
Modifier and Type Method Description voidclear()Removes allPerfCounters from the registryOptional<Metric<?>>find(Metric.MetricPath path)Finds the singleMetricwith the exact given path.<R, A> Rfind(Predicate<Metric<?>> predicate, Collector<Metric<?>,A,R> collector)Finds the group ofMetrics that match the given predicate.PerfCountergetCounter(Metric.MetricPath path, PerfCounter.ThreadingModel threadingModel, Metric.DataType dataType, Metric.CounterType counterType, Metric.Units units)Fetches aPerfCounteror creates one if no such counter exists.static MetricsRegistryinstance()voidregisterGauge(Gauge gauge)Adds theGaugeto the global registrybooleanremoveGauge(Gauge gauge)Removes theGaugefrom the global registrybooleanremoveIf(Predicate<Metric<?>> predicate)Removes a counter with the givenMetric.MetricPath.booleanremoveMetricsByBase(String basePath)Removes the set of metrics with the given base path.Stream<Metric<?>>stream()
-
Method Details
-
instance
- Returns:
- the global
PerfCounterregistry
-
getCounter
public PerfCounter getCounter(Metric.MetricPath path, PerfCounter.ThreadingModel threadingModel, Metric.DataType dataType, Metric.CounterType counterType, Metric.Units units)Fetches aPerfCounteror creates one if no such counter exists.For example, to retrieve the counter for "extractor.examplePath" tagged with PROCESSING_GROUP=0:
final Path path = Path.create("extractor.examplePath", Arrays.asList(NamedTag.id(Tag.PROCESSING_GROUP, 0)); PerfCounter counter = PerfRegistry.instance().getCounter(path, ThreadingModel.NO_CONTENTION, DataType.LONG, CounterType.INCREMENTAL_VALUE, Units.UNITLESS);- Parameters:
path- the path for the counter, should be unique to this counterthreadingModel- the threading model under which the counter will be mutateddataType- the data typecounterType- the counter typeunits- the units of the value- Returns:
- the
PerfCounter
-
registerGauge
Adds theGaugeto the global registry- Parameters:
gauge- the gauge
-
removeGauge
Removes theGaugefrom the global registry- Parameters:
gauge- the gauge- Returns:
trueif the gauge was removed
-
stream
- Returns:
- a
Streamof registered metrics
-
find
Finds the group ofMetrics that match the given predicate.- Parameters:
predicate- a non-interfering, stateless predicate to apply to each element to determine if it should be includedcollector- a path to the desired metric- Returns:
- the
Metricif it exists, otherwiseOptional.empty()
-
find
Finds the singleMetricwith the exact given path.- Parameters:
path- a path to the desired metric- Returns:
- the
Metricif it exists, otherwiseOptional.empty()
-
removeIf
Removes a counter with the givenMetric.MetricPath.- Parameters:
predicate- a non-interfering, stateless predicate to apply to each element to determine if it should be removed- Returns:
trueif aMetricwas removed
-
removeMetricsByBase
Removes the set of metrics with the given base path.- Parameters:
basePath- the base path of the metrics- Returns:
trueif aPerfCounterwas removed
-
clear
public void clear()Removes allPerfCounters from the registry
-