Counter

A Counter is a metric that only increases (monotonically), typically used for counting occurrences.

This implementation supports optional creation of a _created time series and allows for use with and without labels.

Parameters

fullName

The full name of the metric.

help

Help text describing what the counter measures.

labelNames

List of label names for this counter.

unit

The unit of the metric (e.g., "meters", "seconds").

includeCreatedSeries

If true, also emits a _created series per label set.

Types

Link copied to clipboard
inner class Child

Represents a labeled child of the counter metric.

Properties

Link copied to clipboard

Full metric name (including any suffix or unit).

Link copied to clipboard

Description of the metric, explaining what it measures.

Link copied to clipboard
Link copied to clipboard

List of label names associated with this metric (e.g., "method", "status").

Link copied to clipboard
open override val name: String

The base name of the metric (without unit suffix).

Link copied to clipboard
open override val type: Collector.Type

The metric type (e.g., counter, gauge).

Link copied to clipboard

Unit of the metric (e.g., "seconds", "bytes").

Functions

Link copied to clipboard
fun clear()

Removes all labeled child instances and resets the no-label child (if applicable).

Link copied to clipboard

Collects the current metric samples for this counter.

Link copied to clipboard
suspend fun <T> Counter.countExceptions(vararg exceptionTypes: KClass<out Throwable>, block: () -> T): T?

Executes the given block and increments the counter if an exception of the specified types is thrown.

Link copied to clipboard
fun get(): Double

Retrieves the current value of the counter.

Link copied to clipboard

Returns the simple class name of the Collector instance.

Link copied to clipboard
suspend fun inc(): Unit?

Increments the counter by 1.

suspend fun inc(amount: Double): Unit?

Increments the counter by the specified amount.

Link copied to clipboard
fun labels(vararg labelValues: String): Counter.Child

Retrieves or creates a Child associated with the given label values.

Link copied to clipboard
open override fun newChild(): Counter.Child

Creates a new child instance. Called internally when a new label set is first seen.

Link copied to clipboard
suspend fun register(registry: CollectorRegistry = CollectorRegistry.defaultRegistry): Collector

Registers this collector in the given CollectorRegistry.

Link copied to clipboard
fun remove(vararg labelValues: String)

Removes the child instance associated with the given label values.