public abstract class SimpleCollector<Child,T extends SimpleCollector> extends Collector
Gauge
, Counter
and Summary
.
This class handles common initlization and label logic for the standard metrics. You should never need to subclass this class.
name
,
help
,
labelNames
,
namespace
and
subsystem
can be called to configure the Collector.
These return this
to allow calls to be chained.
Once configured, call create
(which is also called by register
).
The fullname of the metric is namespace_subsystem_name
, but only name
is required.
labelNames
specifies which (if any) labels the metrics will have, and
labels(java.lang.String...)
returns the Child of the metric that represents that particular set of labels.
Gauge
, Counter
and Summary
all offer convienence methods to avoid needing to call
labels(java.lang.String...)
for metrics with no labels.
remove(java.lang.String...)
and clear()
can be used to remove children.
Warning #1: Metrics that don't always export something are difficult to monitor, if you know in advance
what labels will be in use you should initilise them be calling labels(java.lang.String...)
.
This is done for you for metrics with no labels.
Warning #2: While labels are very powerful, avoid overly granular metric labels.
The combinatorial explosion of breaking out a metric in many dimensions can produce huge numberts
of timeseries, which will then take longer and more resources to process.
As a rule of thumb aim to keep the cardinality of metrics below ten, and limit where the
cardinality exceeds that value. For example rather than breaking out latency
by customer and endpoint in one metric, you might have two metrics with one breaking out
by each. If the cardinality is in the hundreds, you may wish to consider removing the breakout
by one of the dimensions altogether.
Modifier and Type | Class and Description |
---|---|
static class |
SimpleCollector.Builder<B extends SimpleCollector.Builder<B>>
Builders let you configure and then create collectors.
|
Collector.MetricFamilySamples, Collector.Type
Modifier and Type | Field and Description |
---|---|
protected ConcurrentMap<List<String>,Child> |
children |
protected String |
fullname |
protected String |
help |
protected List<String> |
labelNames |
protected Child |
noLabelsChild |
METRIC_LABEL_NAME_RE, METRIC_NAME_RE, MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND, RESERVED_METRIC_LABEL_NAME_RE
Modifier | Constructor and Description |
---|---|
protected |
SimpleCollector(SimpleCollector.Builder b) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all children.
|
protected void |
initializeNoLabelsChild()
Initialize the child with no labels.
|
Child |
labels(String... labelValues)
Return the Child with the given labels, creating it if needed.
|
protected abstract Child |
newChild()
Return a new child, workaround for Java generics limitations.
|
void |
remove(String... labelValues)
Remove the Child with the given labels.
|
<T extends Collector> |
setChild(Child child,
String... labelValues)
Replace the Child with the given labels.
|
checkMetricLabelName, checkMetricName, collect, doubleToGoString, register, register
protected final String fullname
protected final String help
protected final ConcurrentMap<List<String>,Child> children
protected Child noLabelsChild
protected SimpleCollector(SimpleCollector.Builder b)
public Child labels(String... labelValues)
Must be passed the same number of labels are were passed to labelNames
.
public void remove(String... labelValues)
Any references to the Child are invalidated.
public void clear()
Any references to any children are invalidated.
protected void initializeNoLabelsChild()
public <T extends Collector> T setChild(Child child, String... labelValues)
This is intended for advanced uses, in particular proxying metrics
from another monitoring system. This allows for callbacks for returning
values for Counter
and Gauge
without having to implement
a full Collector
.
An example with Gauge
:
Gauge.build().name("current_time").help("Current unixtime.").create()
.setChild(new Gauge.Child() {
public double get() {
return System.currentTimeMillis() / MILLISECONDS_PER_SECOND;
}
}).register();
Any references any previous Child with these labelValues are invalidated. A metric should be either all callbacks, or none.
protected abstract Child newChild()
Copyright © 2015. All rights reserved.