Skip to content

Commit

Permalink
Add Histogram exemplar support (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcardell authored Feb 1, 2024
1 parent 4a2c61e commit 3b1ecf0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/main/scala/io/chrisdavenport/epimetheus/Histogram.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.effect._
import cats.implicits._
import io.prometheus.metrics.core.datapoints.DistributionDataPoint
import io.prometheus.metrics.core.metrics.{Histogram => JHistogram}
import io.prometheus.metrics.model.snapshots.Labels

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

def observeWithExemplar(d: Double, exemplarLabels: Map[String, String]): F[Unit]

def observeWithExemplar(d: Double, exemplarLabels: (String, String)*): F[Unit] =
observeWithExemplar(d, exemplarLabels.toMap)

def mapK[G[_]](fk: F ~> G): Histogram[G] =
new Histogram.MapKHistogram[F, G](this, fk)

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

def observeWithExemplar(d: Double, exemplarLabels: Map[String, String]): F[Unit] = {
val labels = exemplarLabels.toList.flatMap { case (k, v) => List(k, v) }
val jLabels = Labels.of(labels:_*)
Sync[F].delay(underlying.observeWithExemplar(d, jLabels))
}

override private[epimetheus] def asJava: F[JHistogram] = underlying.pure[F]
}

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

def observeWithExemplar(d: Double, exemplarLabels: Map[String, String]): F[Unit] = {
val labels = exemplarLabels.toList.flatMap { case (k, v) => List(k, v) }
val jLabels = Labels.of(labels:_*)
Sync[F].delay(underlying.observeWithExemplar(d, jLabels))
}

override private[epimetheus] def asJava: F[JHistogram] =
ApplicativeThrow[F].raiseError(
new IllegalArgumentException(
Expand All @@ -277,8 +295,12 @@ object Histogram {
private[Histogram] val base: Histogram[F],
fk: F ~> G
) extends Histogram[G] {

def observe(d: Double): G[Unit] = fk(base.observe(d))

override def observeWithExemplar(d: Double, exemplarLabels: Map[String,String]): G[Unit] =
fk(base.observeWithExemplar(d, exemplarLabels))

override private[epimetheus] def asJava: G[JHistogram] = fk(base.asJava)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,35 @@ class HistogramSpec extends munit.CatsEffectSuite {

test.attempt.map(_.isRight).assert
}

test("Histogram: observe with exemplars (map)") {
val setup = for {
cr <- PrometheusRegistry.build[IO]
h <- Histogram.noLabels(cr, Name("boo"), "Boo ")
} yield h

val test = for {
h <- setup
_ <- h.observeWithExemplar(0.1, Map("exemplar1" -> "value1"))
} yield ()

test.attempt.map(_.isRight).assert

}

test("Histogram: observe with exemplars (tuples)") {
val setup = for {
cr <- PrometheusRegistry.build[IO]
h <- Histogram.noLabels(cr, Name("boo"), "Boo ")
} yield h

val test = for {
h <- setup
_ <- h.observeWithExemplar(0.1, "exemplar1" -> "value1")
} yield ()

test.attempt.map(_.isRight).assert

}

}

0 comments on commit 3b1ecf0

Please sign in to comment.