Skip to main content
A Counter instrument that records values of type A. The Counter is monotonic, meaning the aggregated value is nominally increasing.

Type Signature

trait Counter[F[_], A]
Type Parameters:
  • F[_] - the higher-kinded type of a polymorphic effect
  • A - the type of values to record. Must have a MeasurementValue instance. Long and Double are supported out of the box.

Creating a Counter

Counters are created using the Meter API:
val meter: Meter[IO] = ???

val counter: IO[Counter[IO, Long]] =
  meter.counter[Long]("request.count")
    .withUnit("requests")
    .withDescription("Total number of requests")
    .create

Backend Methods

add

Records a value with a set of attributes.
value
A
required
The value to increment the counter with. Must be non-negative.
attributes
immutable.Iterable[Attribute[_]]
required
The set of attributes to associate with the value.
Returns: F[Unit] Example:
counter.flatMap { c =>
  c.add(
    5L,
    Attribute(AttributeKey.string("http.method"), "GET")
  )
}

inc

Increments the counter by one.
attributes
immutable.Iterable[Attribute[_]]
required
The set of attributes to associate with the value.
Returns: F[Unit] Example:
counter.flatMap { c =>
  c.inc(Attribute(AttributeKey.string("http.method"), "GET"))
}

Builder Methods

withUnit

Sets the unit of measure for this counter.
unit
String
required
The measurement unit. Must be 63 or fewer ASCII characters.
Returns: Counter.Builder[F, A] Reference: Instrument Unit

withDescription

Sets the description for this counter.
description
String
required
The description of the counter.
Returns: Counter.Builder[F, A] Reference: Instrument Description

create

Creates a Counter with the configured unit and description. Returns: F[Counter[F, A]]

See Also

Build docs developers (and LLMs) love