Skip to content

Commit

Permalink
Allow using labels with any type having a Show instance
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrohdezma committed Aug 18, 2023
1 parent 4aeab3f commit 0998f43
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
11 changes: 11 additions & 0 deletions core/src/main/scala/prometheus4cats/Label.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package prometheus4cats

import cats.Show
import cats.syntax.all._

import prometheus4cats.internal.LabelNameFromStringLiteral
import prometheus4cats.internal.Refined
import prometheus4cats.internal.Refined.Regex
Expand All @@ -37,4 +40,12 @@ object Label {

}

final class Value private (val value: String) extends AnyVal

object Value {

implicit def fromShow[A: Show](a: A): Value = new Value(a.show)

}

}
12 changes: 6 additions & 6 deletions core/src/main/scala/prometheus4cats/internal/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ class LabelledMetricDsl[F[_], A, T, L[_[_], _, _]] private[internal] (
new LabelsApply[F, A, T, B, L] {

override def apply[C](
labels: (Label.Name, B => String)*
labels: (Label.Name, B => Label.Value)*
)(implicit initLast: InitLast.Aux[T, B, C]): LabelledMetricDsl[F, A, C, L] = new LabelledMetricDsl(
makeMetric,
labelNames ++ labels.map(_._1),
c => f(initLast.init(c)) ++ labels.map(_._2(initLast.last(c)))
c => f(initLast.init(c)) ++ labels.map(_._2(initLast.last(c)).value)
)

}
Expand Down Expand Up @@ -498,13 +498,13 @@ object LabelledMetricDsl {
override def labels[B]: LabelsApply.WithCallbacks[F, A, A0, T, B, L] =
new LabelsApply.WithCallbacks[F, A, A0, T, B, L] {

override def apply[C](labels: (Label.Name, B => String)*)(implicit
override def apply[C](labels: (Label.Name, B => Label.Value)*)(implicit
initLast: InitLast.Aux[T, B, C]
): WithCallbacks[F, A, A0, C, L] = new WithCallbacks(
makeMetric,
makeCallback,
labelNames ++ labels.map(_._1),
c => f(initLast.init(c)) ++ labels.map(_._2(initLast.last(c)))
c => f(initLast.init(c)) ++ labels.map(_._2(initLast.last(c)).value)
)

}
Expand Down Expand Up @@ -584,7 +584,7 @@ object LabelApply {

abstract class LabelsApply[F[_], A, T, B, L[_[_], _, _]] {

def apply[C](labels: (Label.Name, B => String)*)(implicit
def apply[C](labels: (Label.Name, B => Label.Value)*)(implicit
initLast: InitLast.Aux[T, B, C]
): LabelledMetricDsl[F, A, C, L]

Expand All @@ -594,7 +594,7 @@ object LabelsApply {

abstract class WithCallbacks[F[_], A, A0, T, B, L[_[_], _, _]] extends LabelsApply[F, A, T, B, L] {

def apply[C](labels: (Label.Name, B => String)*)(implicit
def apply[C](labels: (Label.Name, B => Label.Value)*)(implicit
initLast: InitLast.Aux[T, B, C]
): LabelledMetricDsl.WithCallbacks[F, A, A0, C, L]

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/test/MetricsFactoryDslTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object MetricsFactoryDslTest {
longCounterBuilder.build
longCounterBuilder
.label[String]("label1")
.labels[(Int, BigInteger)](("label2", _._1.toString()), ("label3", _._2.toString()))
.labels[(Int, BigInteger)](("label2", _._1), ("label3", _._2.toString()))
.build
.map(_.inc(("dsfsf", (1, BigInteger.ONE))))
longCounterBuilder.unsafeLabels(Label.Name("label1"), Label.Name("label2")).build
Expand Down

0 comments on commit 0998f43

Please sign in to comment.