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 7d3afc4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 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)

}

}
20 changes: 10 additions & 10 deletions core/src/main/scala/prometheus4cats/internal/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

}
Expand Down Expand Up @@ -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] =
Expand Down 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 7d3afc4

Please sign in to comment.