Skip to content

Commit 3b1ecf0

Browse files
authored
Add Histogram exemplar support (#310)
1 parent 4a2c61e commit 3b1ecf0

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

core/src/main/scala/io/chrisdavenport/epimetheus/Histogram.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cats.effect._
55
import cats.implicits._
66
import io.prometheus.metrics.core.datapoints.DistributionDataPoint
77
import io.prometheus.metrics.core.metrics.{Histogram => JHistogram}
8+
import io.prometheus.metrics.model.snapshots.Labels
89

910
import scala.annotation.tailrec
1011
import scala.concurrent.duration._
@@ -24,6 +25,11 @@ sealed abstract class Histogram[F[_]] {
2425
*/
2526
def observe(d: Double): F[Unit]
2627

28+
def observeWithExemplar(d: Double, exemplarLabels: Map[String, String]): F[Unit]
29+
30+
def observeWithExemplar(d: Double, exemplarLabels: (String, String)*): F[Unit] =
31+
observeWithExemplar(d, exemplarLabels.toMap)
32+
2733
def mapK[G[_]](fk: F ~> G): Histogram[G] =
2834
new Histogram.MapKHistogram[F, G](this, fk)
2935

@@ -257,6 +263,12 @@ object Histogram {
257263
) extends Histogram[F] {
258264
def observe(d: Double): F[Unit] = Sync[F].delay(underlying.observe(d))
259265

266+
def observeWithExemplar(d: Double, exemplarLabels: Map[String, String]): F[Unit] = {
267+
val labels = exemplarLabels.toList.flatMap { case (k, v) => List(k, v) }
268+
val jLabels = Labels.of(labels:_*)
269+
Sync[F].delay(underlying.observeWithExemplar(d, jLabels))
270+
}
271+
260272
override private[epimetheus] def asJava: F[JHistogram] = underlying.pure[F]
261273
}
262274

@@ -265,6 +277,12 @@ object Histogram {
265277
) extends Histogram[F] {
266278
def observe(d: Double): F[Unit] = Sync[F].delay(underlying.observe(d))
267279

280+
def observeWithExemplar(d: Double, exemplarLabels: Map[String, String]): F[Unit] = {
281+
val labels = exemplarLabels.toList.flatMap { case (k, v) => List(k, v) }
282+
val jLabels = Labels.of(labels:_*)
283+
Sync[F].delay(underlying.observeWithExemplar(d, jLabels))
284+
}
285+
268286
override private[epimetheus] def asJava: F[JHistogram] =
269287
ApplicativeThrow[F].raiseError(
270288
new IllegalArgumentException(
@@ -277,8 +295,12 @@ object Histogram {
277295
private[Histogram] val base: Histogram[F],
278296
fk: F ~> G
279297
) extends Histogram[G] {
298+
280299
def observe(d: Double): G[Unit] = fk(base.observe(d))
281300

301+
override def observeWithExemplar(d: Double, exemplarLabels: Map[String,String]): G[Unit] =
302+
fk(base.observeWithExemplar(d, exemplarLabels))
303+
282304
override private[epimetheus] def asJava: G[JHistogram] = fk(base.asJava)
283305
}
284306

core/src/test/scala/io/chrisdavenport/epimetheus/HistogramSpec.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,35 @@ class HistogramSpec extends munit.CatsEffectSuite {
2020

2121
test.attempt.map(_.isRight).assert
2222
}
23+
24+
test("Histogram: observe with exemplars (map)") {
25+
val setup = for {
26+
cr <- PrometheusRegistry.build[IO]
27+
h <- Histogram.noLabels(cr, Name("boo"), "Boo ")
28+
} yield h
29+
30+
val test = for {
31+
h <- setup
32+
_ <- h.observeWithExemplar(0.1, Map("exemplar1" -> "value1"))
33+
} yield ()
34+
35+
test.attempt.map(_.isRight).assert
36+
37+
}
38+
39+
test("Histogram: observe with exemplars (tuples)") {
40+
val setup = for {
41+
cr <- PrometheusRegistry.build[IO]
42+
h <- Histogram.noLabels(cr, Name("boo"), "Boo ")
43+
} yield h
44+
45+
val test = for {
46+
h <- setup
47+
_ <- h.observeWithExemplar(0.1, "exemplar1" -> "value1")
48+
} yield ()
49+
50+
test.attempt.map(_.isRight).assert
51+
52+
}
53+
2354
}

0 commit comments

Comments
 (0)