public final class DistinctCountSketch extends Object implements Meter
Id.The result is an estimate, not an exact count. With the fixed register count used here the relative standard error is approximately 13%. This is generally good enough for "how many distinct things this interval" style questions at a fixed and predictable publishing cost, independent of the true cardinality (whether it is 10 or 10 million).
Distinct count sketches are more expensive than a plain counter. Each instance publishes a fixed number of gauges (64). Be diligent about any additional dimensions added to a sketch and ensure they have a small bounded cardinality, the same way you would for a percentile timer.
| Modifier and Type | Class and Description |
|---|---|
static class |
DistinctCountSketch.Builder
Helper for getting instances of a DistinctCountSketch.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
REGISTERS
Number of HLL registers.
|
| Modifier and Type | Method and Description |
|---|---|
static IdBuilder<DistinctCountSketch.Builder> |
builder(Registry registry)
Return a builder for configuring and retrieving an instance of a distinct count sketch.
|
double |
cardinality()
Estimate the number of distinct values recorded into this sketch on the local node.
|
static double |
cardinality(double[] registers)
Estimate the number of distinct values from the per-register values of a merged sketch.
|
static DistinctCountSketch |
get(Registry registry,
Id id)
Creates a sketch that can be used for estimating the number of distinct values.
|
boolean |
hasExpired()
Returns true once every register that has been created has expired (and so the cached
sketch can be dropped from the registry state).
|
Id |
id()
Identifier for the sketch.
|
Iterable<Measurement> |
measure()
The sketch publishes its data as the per-register max-gauges, so it has no measurements
of its own.
|
void |
record(byte[] value)
Record a distinct value from its raw bytes.
|
void |
record(CharSequence value)
Record a distinct string value, such as a string id or ip address.
|
void |
record(long value)
Record a distinct
long value, such as a numeric id. |
public static final int REGISTERS
distinct=R## tag
must mean the same partition of the hash space everywhere or sketches from different
sources cannot be merged from the tag alone. There is deliberately no precision knob.
Exposed so that consumers reconstructing the sketch (such as the backend estimator) can
size the register array consistently.public static DistinctCountSketch get(Registry registry, Id id)
registry - Registry to use.id - Identifier for the metric being registered.public static IdBuilder<DistinctCountSketch.Builder> builder(Registry registry)
public Iterable<Measurement> measure()
hasExpired()).public boolean hasExpired()
This is only called by the registry's periodic state cleanup, not on the recording hot path, so the scan over the registers is inexpensive. It cannot delegate to a single representative register because registers expire independently: a register that receives no value for a while can expire while the sketch is still actively recording into others, so all created registers must be expired before the sketch is reclaimed.
hasExpired in interface Meterpublic void record(long value)
long value, such as a numeric id.public void record(CharSequence value)
public void record(byte[] value)
public double cardinality()
This only reflects what was recorded into this instance. The fleet-wide distinct
count is computed on the backend, which merges the registers across all sources (by
taking the per-register max) before applying the same estimator. The result is an
approximation with a relative standard error of roughly 1.04/sqrt(REGISTERS).
public static double cardinality(double[] registers)
0 for an unset register) for
each of the m registers; it may be any length, with a relative standard error of
roughly 1.04/sqrt(m).
This is the estimator applied at query time on the backend, after the registers have been merged across the requested space and time aggregation. It is exposed here so that the client and the backend share a single implementation. It uses the standard HyperLogLog estimator with linear counting for the small cardinality range.
registers - Per-register max rho values for the merged sketch.