SamplerGaugeCollector
A Prometheus Collector that exposes a gauge metric whose value is obtained by invoking a sampling function on each collection cycle.
The collector registers itself with registry upon construction and can later be removed with registry.unregister(collector). Collection is delegated to an internal, unregistered GaugeWithCallback. Registration reads only the metric's descriptor (name, type, help and label names), never its value, so the sampler first runs on the first scrape, not during construction.
Each collector exposes exactly one series, with labelNames and labelValues as constant labels. The registry rejects a second collector with the same name and the same label names, so one metric cannot get a series per label value this way. (It does accept the same name with other label names, provided the help text matches.)
Parameters
the metric name. As in the Prometheus Java client 1.x, any non-empty UTF-8 string is accepted; the exposition formats escape characters outside the legacy [a-zA-Z_:][a-zA-Z0-9_:]* set.
the help/description text for the metric.
the label names for the metric. Defaults to empty.
the label values corresponding to labelNames. Defaults to empty.
the registry to register with. Defaults to PrometheusRegistry.defaultRegistry.
a lambda that returns the current gauge value as a Double. If it throws, the failure is logged at WARN and the gauge reports NaN for that scrape instead of failing the whole scrape.
Throws
if labelNames and labelValues differ in size, if name is empty or a label name is not a valid Prometheus label name (for example one starting with __), if a label name is repeated, or if registry already holds a metric whose exposed names collide with this one.
Constructors
Creates an unlabelled gauge registered with PrometheusRegistry.defaultRegistry.
Creates a gauge with the given labels, registered with PrometheusRegistry.defaultRegistry.