Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Histogram exemplar support #310

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

}

}
Loading