SamplerGaugeCollector

class SamplerGaugeCollector(name: String, help: String, labelNames: List<String> = emptyList(), labelValues: List<String> = emptyList(), registry: PrometheusRegistry = PrometheusRegistry.defaultRegistry, data: () -> Double) : Collector

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

name

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.

help

the help/description text for the metric.

labelNames

the label names for the metric. Defaults to empty.

labelValues

the label values corresponding to labelNames. Defaults to empty.

registry

the registry to register with. Defaults to PrometheusRegistry.defaultRegistry.

data

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

Link copied to clipboard
constructor(name: String, help: String, labelNames: List<String> = emptyList(), labelValues: List<String> = emptyList(), registry: PrometheusRegistry = PrometheusRegistry.defaultRegistry, data: () -> Double)
constructor(name: String, help: String, data: () -> Double)

Creates an unlabelled gauge registered with PrometheusRegistry.defaultRegistry.

constructor(name: String, help: String, labelNames: List<String>, labelValues: List<String>, data: () -> Double)

Creates a gauge with the given labels, registered with PrometheusRegistry.defaultRegistry.

Properties

Link copied to clipboard
open val labelNames: Set<String?>??
Link copied to clipboard
open val metadata: MetricMetadata?
Link copied to clipboard
open override val metricFamilyDescriptor: MetricFamilyDescriptor?
Link copied to clipboard
open val metricType: MetricType?
Link copied to clipboard

Functions

Link copied to clipboard
open override fun collect(): GaugeSnapshot
open fun collect(scrapeRequest: PrometheusScrapeRequest?): MetricSnapshot?
open fun collect(includedNames: Predicate<String?>?): MetricSnapshot?
open fun collect(includedNames: Predicate<String?>?, scrapeRequest: PrometheusScrapeRequest?): MetricSnapshot?
Link copied to clipboard
open override fun getMetricFamilyDescriptor(): MetricFamilyDescriptor?