diff --git a/core/src/main/scala/prometheus4cats/internal/package.scala b/core/src/main/scala/prometheus4cats/internal/package.scala index 3c4a864..cd1d5bf 100644 --- a/core/src/main/scala/prometheus4cats/internal/package.scala +++ b/core/src/main/scala/prometheus4cats/internal/package.scala @@ -19,7 +19,7 @@ package prometheus4cats.internal import cats.data.NonEmptyList import cats.effect.kernel.{Clock, MonadCancelThrow, Resource} import cats.syntax.all._ -import cats.{FlatMap, Functor, Show} +import cats.{Contravariant, FlatMap, Functor, Show} import prometheus4cats.OutcomeRecorder.Status import prometheus4cats._ @@ -143,6 +143,15 @@ object BuildStep { implicit class LabelsContravariantSyntax[F[_], M[_]: LabelsContravariant, A](bs: BuildStep[F, M[A]]) { def contramapLabels[B](f: B => A): BuildStep[F, M[B]] = bs.map(LabelsContravariant[M].contramapLabels(_)(f)) } + + implicit def auxContravariant[F[_], M[_]: Contravariant]: Contravariant[Aux[F, M, *]] = + new Contravariant[Aux[F, M, *]] { + override def contramap[A, B](fa: Aux[F, M, A])(f: B => A): Aux[F, M, B] = fa.map(_.contramap(f)) + } + + implicit class ContravariantSyntax[F[_], M[_]: Contravariant, A](bs: BuildStep[F, M[A]]) { + def contramap[B](f: B => A): BuildStep[F, M[B]] = bs.map(_.contramap(f)) + } } sealed trait CallbackStep[F[_], A] { self => @@ -289,6 +298,8 @@ object MetricDsl { .map(_.contramapLabels[(Unit, OutcomeRecorder.Status)](_._2)) .map(OutcomeRecorder.fromCounter) ) + + def contramap[B](f: B => A): BuildStep[F, Counter[F, B, Unit]] = dsl.map(_.contramap(f)) } implicit class GaugeSyntax[F[_], A](dsl: MetricDsl[F, A, Gauge]) { @@ -299,6 +310,8 @@ object MetricDsl { .map(_.contramapLabels[(Unit, OutcomeRecorder.Status)](_._2)) .map(OutcomeRecorder.fromGauge(_)) ) + + def contramap[B](f: B => A): BuildStep[F, Gauge[F, B, Unit]] = dsl.map(_.contramap(f)) } } diff --git a/docs/interface/dsl.md b/docs/interface/dsl.md index f9edfe7..ac62f1d 100644 --- a/docs/interface/dsl.md +++ b/docs/interface/dsl.md @@ -140,7 +140,7 @@ unsafeLabelledCounter.build.evalMap(_.inc(3.0, labels)) #### Simple Metric ```scala mdoc:silent -val intCounter: Resource[IO, Counter[IO, Int]] = factory +val intCounter: Resource[IO, Counter[IO, Int, Unit]] = factory .counter("counter_total") .ofLong .help("Describe what this metric does") @@ -149,7 +149,7 @@ val intCounter: Resource[IO, Counter[IO, Int]] = factory ``` ```scala mdoc:silent -val shortCounter: Resource[IO, Counter[IO, Short]] = intCounter.map(_.contramap[Short](_.toInt)) +val shortCounter: Resource[IO, Counter[IO, Short, Unit]] = intCounter.map(_.contramap[Short](_.toInt)) ``` #### Labelled Metric diff --git a/docs/metrics/derived-metric-types.md b/docs/metrics/derived-metric-types.md index bb12c06..5e3d39a 100644 --- a/docs/metrics/derived-metric-types.md +++ b/docs/metrics/derived-metric-types.md @@ -32,7 +32,7 @@ operations. #### Obtaining from a `Histogram` ```scala mdoc:silent -val simpleTimerHistogram: Resource[IO, Timer.Aux[IO, Histogram]] = factory +val simpleTimerHistogram: Resource[IO, Timer.Aux[IO, Unit, Histogram]] = factory .histogram("time") .ofDouble .help("Records the how long an opertation took") @@ -55,7 +55,7 @@ val labelledTimerHistogram: Resource[IO, Timer.Aux[IO, String, Histogram]] = fac #### Obtaining from a `Summary` ```scala mdoc:silent -val simpleTimerSummary: Resource[IO, Timer.Aux[IO, Summary]] = factory +val simpleTimerSummary: Resource[IO, Timer.Aux[IO, Unit, Summary]] = factory .summary("time") .ofDouble .help("Records the how long an opertation took") @@ -76,7 +76,7 @@ val labelledTimerSummary: Resource[IO, Timer.Aux[IO, String, Summary]] = factory #### Obtaining from a `Gauge` ```scala mdoc:silent -val simpleTimerGauge: Resource[IO, Timer.Aux[IO, Gauge]] = factory +val simpleTimerGauge: Resource[IO, Timer.Aux[IO, Unit, Gauge]] = factory .gauge("time") .ofDouble .help("Records the how long an opertation took") @@ -102,7 +102,7 @@ system time. #### Obtaining from a `Gauge` ```scala mdoc:silent -val simpleCurrentTimeRecorderGauge: Resource[IO, CurrentTimeRecorder[IO]] = factory +val simpleCurrentTimeRecorderGauge: Resource[IO, CurrentTimeRecorder[IO, Unit]] = factory .gauge("current_time") .ofDouble .help("Records the how long an opertation took") @@ -142,7 +142,7 @@ To help disambiguate the difference in behaviour the `OutcomeRecorder` type will #### Obtaining from a `Counter` ```scala mdoc:silent -val simpleOutcomeCounter: Resource[IO, OutcomeRecorder.Aux[IO, Long, Counter]] = factory +val simpleOutcomeCounter: Resource[IO, OutcomeRecorder.Aux[IO, Long, Unit, Counter]] = factory .counter("outcome_total") .ofLong .help("Records the outcome of some operation") @@ -164,7 +164,7 @@ val labelledOutcomeCounter: #### Obtaining from a `Gauge` ```scala mdoc:silent -val simpleOutcomeGauge: Resource[IO, OutcomeRecorder.Aux[IO, Long, Gauge]] = factory +val simpleOutcomeGauge: Resource[IO, OutcomeRecorder.Aux[IO, Long, Unit, Gauge]] = factory .gauge("outcome") .ofLong .help("Records the outcome of some operation")