Skip to content

Instantly share code, notes, and snippets.

@ChrisBlom
Created May 24, 2022 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBlom/7759f61dfde3dd326f69e93b91e24124 to your computer and use it in GitHub Desktop.
Save ChrisBlom/7759f61dfde3dd326f69e93b91e24124 to your computer and use it in GitHub Desktop.
wrapper to create a easy to use timegauge
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tag
import java.time.Instant
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
import java.util.function.ToDoubleFunction
fun MeterRegistry.timeGaugeMillis(name: String, tags: Iterable<Tag>, initialValue: Long = 0): (Instant) -> Unit {
val a = AtomicLong(initialValue)
a.apply {
val deref = ToDoubleFunction<AtomicLong> { x -> x.get().toDouble() }
this@timeGaugeMillis.more().timeGauge(name, tags, this, TimeUnit.MILLISECONDS, deref)
}
return { t: Instant -> a.set(t.toEpochMilli()) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment