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..62c8537 100644 --- a/core/src/main/scala/prometheus4cats/internal/package.scala +++ b/core/src/main/scala/prometheus4cats/internal/package.scala @@ -230,11 +230,11 @@ class MetricDsl[F[_], A, L[_[_], _, _]] private[prometheus4cats] ( * @param labelNames * collection of labels name & function to convert `B` in to a label value pairs */ - def labels[B](labels: (Label.Name, B => String)*): LabelledMetricDsl[F, A, B, L] = { + def labels[B](labels: (Label.Name, B => Label.Value)*): LabelledMetricDsl[F, A, B, L] = { val labelNames = labels.toIndexedSeq.map(_._1) val labelValues = labels.toIndexedSeq.map(_._2) - new LabelledMetricDsl(makeMetric, labelNames, b => labelValues.map(_(b))) + new LabelledMetricDsl(makeMetric, labelNames, b => labelValues.map(_(b).value)) } } @@ -265,11 +265,11 @@ object MetricDsl { labelNames.toIndexedSeq ) - override def labels[B](labels: (Label.Name, B => String)*): LabelledMetricDsl.WithCallbacks[F, A, A0, B, L] = { + override def labels[B](labels: (Label.Name, B => Label.Value)*): LabelledMetricDsl.WithCallbacks[F, A, A0, B, L] = { val labelNames = labels.toIndexedSeq.map(_._1) val labelValues = labels.toIndexedSeq.map(_._2) - new LabelledMetricDsl.WithCallbacks(makeMetric, makeCallback, labelNames, b => labelValues.map(_(b))) + new LabelledMetricDsl.WithCallbacks(makeMetric, makeCallback, labelNames, b => labelValues.map(_(b).value)) } override def label[B]: FirstLabelApply.WithCallbacks[F, A, A0, B, L] = @@ -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/main/scala/prometheus4cats/internal/summary/SummaryDsl.scala b/core/src/main/scala/prometheus4cats/internal/summary/SummaryDsl.scala index 5a553a4..8e68158 100644 --- a/core/src/main/scala/prometheus4cats/internal/summary/SummaryDsl.scala +++ b/core/src/main/scala/prometheus4cats/internal/summary/SummaryDsl.scala @@ -62,7 +62,7 @@ object SummaryDsl { labelNames: Label.Name* ): BuildStep[F, Summary[F, A, Map[Label.Name, String]]] - def labels[B](labels: (Label.Name, B => String)*): LabelledMetricDsl[F, A, B, Summary] + def labels[B](labels: (Label.Name, B => Label.Value)*): LabelledMetricDsl[F, A, B, Summary] } private val defaultQuantiles: Seq[Summary.QuantileDefinition] = Seq.empty 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