diff --git a/core/src/main/scala/prometheus4cats/Label.scala b/core/src/main/scala/prometheus4cats/Label.scala index 7fe8bd4..295c844 100644 --- a/core/src/main/scala/prometheus4cats/Label.scala +++ b/core/src/main/scala/prometheus4cats/Label.scala @@ -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 @@ -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) + + } + } diff --git a/core/src/main/scala/prometheus4cats/internal/package.scala b/core/src/main/scala/prometheus4cats/internal/package.scala index 898d09c..15baf2f 100644 --- a/core/src/main/scala/prometheus4cats/internal/package.scala +++ b/core/src/main/scala/prometheus4cats/internal/package.scala @@ -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) ) } @@ -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) ) } @@ -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] @@ -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] diff --git a/core/src/test/scala/test/MetricsFactoryDslTest.scala b/core/src/test/scala/test/MetricsFactoryDslTest.scala index ae77ea7..6faca1c 100644 --- a/core/src/test/scala/test/MetricsFactoryDslTest.scala +++ b/core/src/test/scala/test/MetricsFactoryDslTest.scala @@ -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