From 1242f98f2a28cae5c3a2e0826b63b63f8977b9e6 Mon Sep 17 00:00:00 2001 From: Chris Jansen Date: Tue, 1 Aug 2023 20:30:09 +0100 Subject: [PATCH] Target 2.0 with bin-incompat changes --- build.sbt | 2 +- .../main/scala/prometheus4cats/Counter.scala | 13 +- .../scala/prometheus4cats/Histogram.scala | 7 - .../scala/prometheus4cats/MetricFactory.scala | 119 +- .../prometheus4cats/MetricRegistry.scala | 1102 +++++++---------- .../util/DoubleMetricRegistry.scala | 93 +- .../test/ExternalPackageMetricRegistry.scala | 63 + .../javasimpleclient/JavaMetricRegistry.scala | 302 ++--- .../testing/TestingMetricRegistry.scala | 2 +- 9 files changed, 676 insertions(+), 1027 deletions(-) diff --git a/build.sbt b/build.sbt index 1f3157d8..2dc8046c 100644 --- a/build.sbt +++ b/build.sbt @@ -11,7 +11,7 @@ import laika.rewrite.link._ import org.typelevel.sbt.site.TypelevelProject // https://typelevel.org/sbt-typelevel/faq.html#what-is-a-base-version-anyway -ThisBuild / tlBaseVersion := "1.0" // your current series x.y +ThisBuild / tlBaseVersion := "2.0" // your current series x.y ThisBuild / organization := "com.permutive" ThisBuild / organizationName := "Permutive" diff --git a/core/src/main/scala/prometheus4cats/Counter.scala b/core/src/main/scala/prometheus4cats/Counter.scala index 5c33a3e8..057c8cfc 100644 --- a/core/src/main/scala/prometheus4cats/Counter.scala +++ b/core/src/main/scala/prometheus4cats/Counter.scala @@ -104,18 +104,7 @@ object Counter { def make[F[_]: FlatMap: prometheus4cats.Exemplar, A](_inc: (A, Option[prometheus4cats.Exemplar.Labels]) => F[Unit])( implicit A: Numeric[A] - ): Exemplar[F, A] = - make(A.one, _inc) - - private[prometheus4cats] def fromCounter[F[_], A](counter: Counter[F, A]) = new Exemplar[F, A] { - override def inc: F[Unit] = counter.inc - - override def inc(n: A): F[Unit] = counter.inc(n) - - override def incWithExemplar: F[Unit] = inc - - override def incWithExemplar(n: A): F[Unit] = inc(n) - } + ): Counter.Exemplar[F, A] = make(A.one, _inc) def noop[F[_]: Applicative, A]: Exemplar[F, A] = new Exemplar[F, A] { override def inc: F[Unit] = Applicative[F].unit diff --git a/core/src/main/scala/prometheus4cats/Histogram.scala b/core/src/main/scala/prometheus4cats/Histogram.scala index 10c1afd4..559049c5 100644 --- a/core/src/main/scala/prometheus4cats/Histogram.scala +++ b/core/src/main/scala/prometheus4cats/Histogram.scala @@ -112,13 +112,6 @@ object Histogram { override def observeWithExemplar(n: A): F[Unit] = prometheus4cats.Exemplar[F].get.flatMap(_observe(n, _)) } - private[prometheus4cats] def fromHistogram[F[_], A](histogram: Histogram[F, A]): Exemplar[F, A] = - new Exemplar[F, A] { - override def observe(n: A): F[Unit] = histogram.observe(n) - - override def observeWithExemplar(n: A): F[Unit] = histogram.observe(n) - } - def noop[F[_]: Applicative, A]: Exemplar[F, A] = new Exemplar[F, A] { override def observe(n: A): F[Unit] = Applicative[F].unit diff --git a/core/src/main/scala/prometheus4cats/MetricFactory.scala b/core/src/main/scala/prometheus4cats/MetricFactory.scala index 698a81e4..1171dc41 100644 --- a/core/src/main/scala/prometheus4cats/MetricFactory.scala +++ b/core/src/main/scala/prometheus4cats/MetricFactory.scala @@ -30,17 +30,12 @@ sealed abstract class MetricFactory[F[_]]( val commonLabels: CommonLabels ) { - protected def exemplarRegistry: Option[MetricRegistry.WithExemplars[F]] = metricRegistry match { - case exemplars: MetricRegistry.WithExemplars[F] => Some(exemplars) - case _ => None - } - /** Given a natural transformation from `F` to `G`, transforms this [[MetricFactory]] from effect `F` to effect `G`. * The G constraint can also be satisfied by requiring a Functor[G]. */ def mapK[G[_]](fk: F ~> G)(implicit F: MonadCancel[F, _], G: MonadCancel[G, _]): MetricFactory[G] = new MetricFactory[G]( - exemplarRegistry.fold(metricRegistry.mapK(fk))(_.mapK(fk)), + metricRegistry.mapK(fk), prefix, commonLabels ) {} @@ -138,39 +133,23 @@ sealed abstract class MetricFactory[F[_]]( new TypeStep[ExemplarCounterDsl[MetricDsl, *]]( new HelpStep(help => new MetricDsl( - exemplarRegistry.fold[Resource[F, Counter.Exemplar[F, Long]]]( - metricRegistry - .createAndRegisterLongCounter(prefix, name, help, commonLabels) - .map(Counter.Exemplar.fromCounter) - )(_.createAndRegisterLongExemplarCounter(prefix, name, help, commonLabels)), + metricRegistry.createAndRegisterLongExemplarCounter(prefix, name, help, commonLabels), new LabelledMetricPartiallyApplied[F, Long, Counter.Labelled.Exemplar] { override def apply[B]( labels: IndexedSeq[Label.Name] )(f: B => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Long, B]] = - exemplarRegistry.fold[Resource[F, Counter.Labelled.Exemplar[F, Long, B]]]( - metricRegistry - .createAndRegisterLabelledLongCounter(prefix, name, help, commonLabels, labels)(f) - .map(Counter.Labelled.Exemplar.fromCounter) - )(_.createAndRegisterLabelledLongExemplarCounter(prefix, name, help, commonLabels, labels)(f)) + metricRegistry.createAndRegisterLabelledLongExemplarCounter(prefix, name, help, commonLabels, labels)(f) } ) ), new HelpStep(help => new MetricDsl( - exemplarRegistry.fold[Resource[F, Counter.Exemplar[F, Double]]]( - metricRegistry - .createAndRegisterDoubleCounter(prefix, name, help, commonLabels) - .map(Counter.Exemplar.fromCounter) - )(_.createAndRegisterDoubleExemplarCounter(prefix, name, help, commonLabels)), + metricRegistry.createAndRegisterDoubleExemplarCounter(prefix, name, help, commonLabels), new LabelledMetricPartiallyApplied[F, Double, Counter.Labelled.Exemplar] { override def apply[B]( labels: IndexedSeq[Label.Name] )(f: B => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Double, B]] = - exemplarRegistry.fold[Resource[F, Counter.Labelled.Exemplar[F, Double, B]]]( - metricRegistry - .createAndRegisterLabelledDoubleCounter(prefix, name, help, commonLabels, labels)(f) - .map(Counter.Labelled.Exemplar.fromCounter) - )(_.createAndRegisterLabelledDoubleExemplarCounter(prefix, name, help, commonLabels, labels)(f)) + metricRegistry.createAndRegisterLabelledDoubleExemplarCounter(prefix, name, help, commonLabels, labels)(f) } ) ) @@ -245,22 +224,13 @@ sealed abstract class MetricFactory[F[_]]( new HelpStep(help => new BucketDsl[MetricDsl[F, Long, Histogram.Exemplar, Histogram.Labelled.Exemplar], Long](buckets => new MetricDsl( - exemplarRegistry.fold[Resource[F, Histogram.Exemplar[F, Long]]]( - metricRegistry - .createAndRegisterLongHistogram(prefix, name, help, commonLabels, buckets) - .map(Histogram.Exemplar.fromHistogram) - )(_.createAndRegisterLongExemplarHistogram(prefix, name, help, commonLabels, buckets)), + metricRegistry.createAndRegisterLongExemplarHistogram(prefix, name, help, commonLabels, buckets), new LabelledMetricPartiallyApplied[F, Long, Histogram.Labelled.Exemplar] { override def apply[B]( labels: IndexedSeq[Label.Name] )(f: B => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Long, B]] = - exemplarRegistry.fold[Resource[F, Histogram.Labelled.Exemplar[F, Long, B]]]( - metricRegistry - .createAndRegisterLabelledLongHistogram(prefix, name, help, commonLabels, labels, buckets)(f) - .map(Histogram.Labelled.Exemplar.fromHistogram) - )( - _.createAndRegisterLabelledLongExemplarHistogram(prefix, name, help, commonLabels, labels, buckets)(f) - ) + metricRegistry + .createAndRegisterLabelledLongExemplarHistogram(prefix, name, help, commonLabels, labels, buckets)(f) } ) ) @@ -268,24 +238,19 @@ sealed abstract class MetricFactory[F[_]]( new HelpStep(help => new BucketDsl[MetricDsl[F, Double, Histogram.Exemplar, Histogram.Labelled.Exemplar], Double](buckets => new MetricDsl( - exemplarRegistry.fold[Resource[F, Histogram.Exemplar[F, Double]]]( - metricRegistry - .createAndRegisterDoubleHistogram(prefix, name, help, commonLabels, buckets) - .map(Histogram.Exemplar.fromHistogram) - )(_.createAndRegisterDoubleExemplarHistogram(prefix, name, help, commonLabels, buckets)), + metricRegistry.createAndRegisterDoubleExemplarHistogram(prefix, name, help, commonLabels, buckets), new LabelledMetricPartiallyApplied[F, Double, Histogram.Labelled.Exemplar] { override def apply[B]( labels: IndexedSeq[Label.Name] )(f: B => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, B]] = - exemplarRegistry.fold[Resource[F, Histogram.Labelled.Exemplar[F, Double, B]]]( - metricRegistry - .createAndRegisterLabelledDoubleHistogram(prefix, name, help, commonLabels, labels, buckets)(f) - .map(Histogram.Labelled.Exemplar.fromHistogram) - )( - _.createAndRegisterLabelledDoubleExemplarHistogram(prefix, name, help, commonLabels, labels, buckets)( - f - ) - ) + metricRegistry.createAndRegisterLabelledDoubleExemplarHistogram( + prefix, + name, + help, + commonLabels, + labels, + buckets + )(f) } ) ) @@ -756,23 +721,9 @@ object MetricFactory { * @return * a new [[MetricFactory]] instance */ - @deprecated( - "please provide a MetricRegistry.WithExemplars[F] instead, otherwise any exemplar labels you set won't be displayed", - "1.1.0" - ) def build[F[_]](metricRegistry: MetricRegistry[F]): MetricFactory[F] = new MetricFactory[F](metricRegistry, prefix, commonLabels) {} - /** Build a [[MetricFactory]] from a [[MetricRegistry]] - * - * @param metricRegistry - * [[MetricRegistry.WithExemplars]] with which to register new metrics created by the built [[MetricFactory]] - * @return - * a new [[MetricFactory]] instance - */ - def build[F[_]](metricRegistry: MetricRegistry.WithExemplars[F]): MetricFactory[F] = - new MetricFactory[F](metricRegistry, prefix, commonLabels) {} - /** Build a [[MetricFactory]] from a [[MetricRegistry]] and separate [[CallbackRegistry]] * * @param metricRegistry @@ -782,31 +733,12 @@ object MetricFactory { * @return * a new [[MetricFactory.WithCallbacks]] instance */ - @deprecated( - "please provide a MetricRegistry.WithExemplars[F] instead, otherwise any exemplar labels you set won't be displayed", - "1.1.0" - ) def build[F[_]]( metricRegistry: MetricRegistry[F], callbackRegistry: CallbackRegistry[F] ): MetricFactory.WithCallbacks[F] = new MetricFactory.WithCallbacks[F](metricRegistry, callbackRegistry, prefix, commonLabels) {} - /** Build a [[MetricFactory]] from a [[MetricRegistry.WithExemplars]] and separate [[CallbackRegistry]] - * - * @param metricRegistry - * [[MetricRegistry.WithExemplars]] with which to register new metrics created by the built [[MetricFactory]] - * @param callbackRegistry - * [[CallbackRegistry]] with which to register new metrics created by the built [[MetricFactory]] - * @return - * a new [[MetricFactory.WithCallbacks]] instance - */ - def build[F[_]]( - metricRegistry: MetricRegistry.WithExemplars[F], - callbackRegistry: CallbackRegistry[F] - ): MetricFactory.WithCallbacks[F] = - new MetricFactory.WithCallbacks[F](metricRegistry, callbackRegistry, prefix, commonLabels) {} - /** Build a [[MetricFactory]] from a [[MetricRegistry with CallbackRegistry]] * * @param metricRegistry @@ -815,26 +747,9 @@ object MetricFactory { * @return * a new [[MetricFactory.WithCallbacks]] instance */ - @deprecated( - "please provide a MetricRegistry.WithExemplars[F] instead, otherwise any exemplar labels you set won't be displayed", - "1.1.0" - ) def build[F[_]](metricRegistry: MetricRegistry[F] with CallbackRegistry[F]): MetricFactory.WithCallbacks[F] = new MetricFactory.WithCallbacks[F](metricRegistry, metricRegistry, prefix, commonLabels) {} - /** Build a [[MetricFactory]] from a [[MetricRegistry.WithExemplars with CallbackRegistry]] - * - * @param metricRegistry - * [[[MetricRegistry.WithExemplars with CallbackRegistry]] with which to register new metrics and callbacks - * created by the built [[MetricFactory]] - * @return - * a new [[MetricFactory.WithCallbacks]] instance - */ - def build[F[_]]( - metricRegistry: MetricRegistry.WithExemplars[F] with CallbackRegistry[F] - ): MetricFactory.WithCallbacks[F] = - new MetricFactory.WithCallbacks[F](metricRegistry, metricRegistry, prefix, commonLabels) {} - /** Build a [[MetricFactory]] from an existing [[MetricFactory]] and [[CallbackRegistry]] * * @param metricFactory diff --git a/core/src/main/scala/prometheus4cats/MetricRegistry.scala b/core/src/main/scala/prometheus4cats/MetricRegistry.scala index eb7567cf..deedc988 100644 --- a/core/src/main/scala/prometheus4cats/MetricRegistry.scala +++ b/core/src/main/scala/prometheus4cats/MetricRegistry.scala @@ -480,693 +480,324 @@ trait MetricRegistry[F[_]] { help: Metric.Help ): Resource[F, Info[F, Map[Label.Name, String]]] + /** Create and register an exemplar counter that records [[scala.Double]] values against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Counter.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @return + * a [[Counter.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterLongExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[F, Counter.Exemplar[F, Long]] + + /** Create and register an exemplar counter that records [[scala.Long]] values against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Counter.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @return + * a [[Counter.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterDoubleExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[F, Counter.Exemplar[F, Double]] + + /** Create and register a labelled exemplar counter that records [[scala.Double]] values against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Counter.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @param labelNames + * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with + * @param f + * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must be + * paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s + * @return + * a [[Counter.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterLabelledDoubleExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Double, A]] + + /** Create and register a labelled exemplar counter that records [[scala.Long]] values against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Counter.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @param labelNames + * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with + * @param f + * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must be + * paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s + * @return + * a [[Counter.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterLabelledLongExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Long, A]] + + /** Create and register an exemplar histogram that records [[scala.Double]] values against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Histogram.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @param buckets + * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram + * @return + * a [[Histogram.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterDoubleExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + buckets: NonEmptySeq[Double] + ): Resource[F, Histogram.Exemplar[F, Double]] + + /** Create and register an exemplar histogram that records [[scala.Long]] values against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Histogram.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @param buckets + * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram + * @return + * a [[Histogram.Labelled]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterLongExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + buckets: NonEmptySeq[Long] + ): Resource[F, Histogram.Exemplar[F, Long]] + + /** Create and register a labelled exemplar histogram against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Histogram.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @param labelNames + * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with + * @param buckets + * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram + * @param f + * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must be + * paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s + * @return + * a [[Histogram.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterLabelledDoubleExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Double] + )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, A]] + + /** Create and register a labelled exemplar histogram against a metrics registry + * + * @param prefix + * optional [[Metric.Prefix]] to be prepended to the metric name + * @param name + * [[Histogram.Name]] metric name + * @param help + * [[Metric.Help]] string to describe the metric + * @param commonLabels + * [[Metric.CommonLabels]] map of common labels to be added to the metric + * @param labelNames + * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with + * @param buckets + * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram + * @param f + * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must be + * paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s + * @return + * a [[Histogram.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it + */ + def createAndRegisterLabelledLongExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Long] + )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Long, A]] + def mapK[G[_]](fk: F ~> G)(implicit F: MonadCancel[F, _], G: MonadCancel[G, _]): MetricRegistry[G] = MetricRegistry.mapK(this, fk) } object MetricRegistry { - trait WithExemplars[F[_]] extends MetricRegistry[F] { - - /** Create and register an exemplar counter that records [[scala.Double]] values against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Counter.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @return - * a [[Counter.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterLongExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels - ): Resource[F, Counter.Exemplar[F, Long]] - - /** Create and register an exemplar counter that records [[scala.Long]] values against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Counter.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @return - * a [[Counter.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterDoubleExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels - ): Resource[F, Counter.Exemplar[F, Double]] - - /** Create and register a labelled exemplar counter that records [[scala.Double]] values against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Counter.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @param labelNames - * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with - * @param f - * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must - * be paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s - * @return - * a [[Counter.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterLabelledDoubleExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Double, A]] - - /** Create and register a labelled exemplar counter that records [[scala.Long]] values against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Counter.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @param labelNames - * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with - * @param f - * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must - * be paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s - * @return - * a [[Counter.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterLabelledLongExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Long, A]] - - /** Create and register an exemplar histogram that records [[scala.Double]] values against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Histogram.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @param buckets - * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram - * @return - * a [[Histogram.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterDoubleExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - buckets: NonEmptySeq[Double] - ): Resource[F, Histogram.Exemplar[F, Double]] - - /** Create and register an exemplar histogram that records [[scala.Long]] values against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Histogram.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @param buckets - * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram - * @return - * a [[Histogram.Labelled]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterLongExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - buckets: NonEmptySeq[Long] - ): Resource[F, Histogram.Exemplar[F, Long]] - - /** Create and register a labelled exemplar histogram against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Histogram.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @param labelNames - * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with - * @param buckets - * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram - * @param f - * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must - * be paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s - * @return - * a [[Histogram.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterLabelledDoubleExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Double] - )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, A]] - - /** Create and register a labelled exemplar histogram against a metrics registry - * - * @param prefix - * optional [[Metric.Prefix]] to be prepended to the metric name - * @param name - * [[Histogram.Name]] metric name - * @param help - * [[Metric.Help]] string to describe the metric - * @param commonLabels - * [[Metric.CommonLabels]] map of common labels to be added to the metric - * @param labelNames - * an [[scala.IndexedSeq]] of [[Label.Name]]s to annotate the metric with - * @param buckets - * a [[cats.data.NonEmptySeq]] of [[scala.Double]]s representing bucket values for the histogram - * @param f - * a function from `A` to an [[scala.IndexedSeq]] of [[java.lang.String]] that provides label values, which must - * be paired with their corresponding name in the [[scala.IndexedSeq]] of [[Label.Name]]s - * @return - * a [[Histogram.Labelled.Exemplar]] wrapped in whatever side effect that was performed in registering it - */ - def createAndRegisterLabelledLongExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Long] - )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Long, A]] - - override def mapK[G[_]]( - fk: F ~> G - )(implicit F: MonadCancel[F, _], G: MonadCancel[G, _]): MetricRegistry.WithExemplars[G] = - WithExemplars.mapK(this, fk) - } - - object WithExemplars { - def noop[F[_]](implicit F: Applicative[F]): MetricRegistry.WithExemplars[F] = - new DoubleMetricRegistry.WithExemplars[F] { - - override def createAndRegisterDoubleExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels - ): Resource[F, Counter.Exemplar[F, Double]] = Resource.pure(Counter.Exemplar.noop) - - override def createAndRegisterDoubleCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[F, Counter[F, Double]] = Resource.pure(Counter.noop) - - override def createAndRegisterLabelledDoubleCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled[F, Double, A]] = - Resource.pure(Counter.Labelled.noop) - - override def createAndRegisterLabelledDoubleExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Labelled.Exemplar[F, Double, A]] = - Resource.pure(Counter.Labelled.Exemplar.noop) - - override def createAndRegisterDoubleGauge( - prefix: Option[Metric.Prefix], - name: Gauge.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[F, Gauge[F, Double]] = - Resource.pure(Gauge.noop) - - override def createAndRegisterLabelledDoubleGauge[A]( - prefix: Option[Metric.Prefix], - name: Gauge.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Gauge.Labelled[F, Double, A]] = - Resource.pure(Gauge.Labelled.noop) - - override def createAndRegisterDoubleHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - buckets: NonEmptySeq[Double] - ): Resource[F, Histogram[F, Double]] = Resource.pure(Histogram.noop) - - override def createAndRegisterDoubleExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - buckets: NonEmptySeq[Double] - ): Resource[F, Histogram.Exemplar[F, Double]] = Resource.pure(Histogram.Exemplar.noop) - - override def createAndRegisterLabelledDoubleHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Double] - )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled[F, Double, A]] = - Resource.pure(Histogram.Labelled.noop) - - override def createAndRegisterLabelledDoubleExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Double] - )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, A]] = - Resource.pure(Histogram.Labelled.Exemplar.noop) - - override def createAndRegisterDoubleSummary( - prefix: Option[Metric.Prefix], - name: Summary.Name, - help: Metric.Help, - commonLabels: CommonLabels, - quantiles: Seq[QuantileDefinition], - maxAge: FiniteDuration, - ageBuckets: Summary.AgeBuckets - ): Resource[F, Summary[F, Double]] = Resource.pure(Summary.noop) - - override def createAndRegisterLabelledDoubleSummary[A]( - prefix: Option[Metric.Prefix], - name: Summary.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - quantiles: Seq[QuantileDefinition], - maxAge: FiniteDuration, - ageBuckets: Summary.AgeBuckets - )(f: A => IndexedSeq[String]): Resource[F, Summary.Labelled[F, Double, A]] = - Resource.pure(Summary.Labelled.noop) - - override def createAndRegisterInfo( - prefix: Option[Metric.Prefix], - name: Info.Name, - help: Metric.Help - ): Resource[F, Info[F, Map[Label.Name, String]]] = Resource.pure(Info.noop) - } - - private[prometheus4cats] def mapK[F[_], G[_]]( - self: MetricRegistry.WithExemplars[F], - fk: F ~> G - )(implicit F: MonadCancel[F, _], G: MonadCancel[G, _]): MetricRegistry.WithExemplars[G] = - new MetricRegistry.WithExemplars[G] { - override def createAndRegisterDoubleCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[G, Counter[G, Double]] = - self.createAndRegisterDoubleCounter(prefix, name, help, commonLabels).mapK(fk).map(_.mapK(fk)) - - override def createAndRegisterLabelledDoubleCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[G, Counter.Labelled[G, Double, A]] = - self - .createAndRegisterLabelledDoubleCounter( - prefix, - name, - help, - commonLabels, - labelNames - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterDoubleExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[G, Counter.Exemplar[G, Double]] = - self.createAndRegisterDoubleExemplarCounter(prefix, name, help, commonLabels).mapK(fk).map(_.mapK(fk)) - - override def createAndRegisterLabelledDoubleExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[G, Counter.Labelled.Exemplar[G, Double, A]] = - self - .createAndRegisterLabelledDoubleExemplarCounter( - prefix, - name, - help, - commonLabels, - labelNames - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterDoubleGauge( - prefix: Option[Metric.Prefix], - name: Gauge.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[G, Gauge[G, Double]] = - self - .createAndRegisterDoubleGauge(prefix, name, help, commonLabels) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLongGauge( - prefix: Option[Metric.Prefix], - name: Gauge.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[G, Gauge[G, Long]] = - self - .createAndRegisterLongGauge(prefix, name, help, commonLabels) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledDoubleGauge[A]( - prefix: Option[Metric.Prefix], - name: Gauge.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[G, Gauge.Labelled[G, Double, A]] = - self - .createAndRegisterLabelledDoubleGauge( - prefix, - name, - help, - commonLabels, - labelNames - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterDoubleHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - buckets: NonEmptySeq[Double] - ): Resource[G, Histogram[G, Double]] = - self - .createAndRegisterDoubleHistogram( - prefix, - name, - help, - commonLabels, - buckets - ) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledDoubleHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Double] - )(f: A => IndexedSeq[String]): Resource[G, Histogram.Labelled[G, Double, A]] = - self - .createAndRegisterLabelledDoubleHistogram( - prefix, - name, - help, - commonLabels, - labelNames, - buckets - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterDoubleExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - buckets: NonEmptySeq[Double] - ): Resource[G, Histogram.Exemplar[G, Double]] = - self - .createAndRegisterDoubleExemplarHistogram( - prefix, - name, - help, - commonLabels, - buckets - ) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledDoubleExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Double] - )(f: A => IndexedSeq[String]): Resource[G, Histogram.Labelled.Exemplar[G, Double, A]] = - self - .createAndRegisterLabelledDoubleExemplarHistogram( - prefix, - name, - help, - commonLabels, - labelNames, - buckets - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLongCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[G, Counter[G, Long]] = - self.createAndRegisterLongCounter(prefix, name, help, commonLabels).mapK(fk).map(_.mapK(fk)) - - override def createAndRegisterLabelledLongCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[G, Counter.Labelled[G, Long, A]] = - self - .createAndRegisterLabelledLongCounter(prefix, name, help, commonLabels, labelNames)(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLongExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels - ): Resource[G, Counter.Exemplar[G, Long]] = - self.createAndRegisterLongExemplarCounter(prefix, name, help, commonLabels).mapK(fk).map(_.mapK(fk)) - - override def createAndRegisterLabelledLongExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[G, Counter.Labelled.Exemplar[G, Long, A]] = - self - .createAndRegisterLabelledLongExemplarCounter(prefix, name, help, commonLabels, labelNames)(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledLongGauge[A]( - prefix: Option[Metric.Prefix], - name: Gauge.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[G, Gauge.Labelled[G, Long, A]] = - self - .createAndRegisterLabelledLongGauge(prefix, name, help, commonLabels, labelNames)(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLongHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - buckets: NonEmptySeq[Long] - ): Resource[G, Histogram[G, Long]] = - self.createAndRegisterLongHistogram(prefix, name, help, commonLabels, buckets).mapK(fk).map(_.mapK(fk)) - - override def createAndRegisterLabelledLongHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Long] - )(f: A => IndexedSeq[String]): Resource[G, Histogram.Labelled[G, Long, A]] = - self - .createAndRegisterLabelledLongHistogram(prefix, name, help, commonLabels, labelNames, buckets)(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLongExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - buckets: NonEmptySeq[Long] - ): Resource[G, Histogram.Exemplar[G, Long]] = - self - .createAndRegisterLongExemplarHistogram(prefix, name, help, commonLabels, buckets) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledLongExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Long] - )(f: A => IndexedSeq[String]): Resource[G, Histogram.Labelled.Exemplar[G, Long, A]] = - self - .createAndRegisterLabelledLongExemplarHistogram(prefix, name, help, commonLabels, labelNames, buckets)(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterDoubleSummary( - prefix: Option[Metric.Prefix], - name: Summary.Name, - help: Metric.Help, - commonLabels: CommonLabels, - quantiles: Seq[QuantileDefinition], - maxAge: FiniteDuration, - ageBuckets: Summary.AgeBuckets - ): Resource[G, Summary[G, Double]] = - self - .createAndRegisterDoubleSummary(prefix, name, help, commonLabels, quantiles, maxAge, ageBuckets) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLongSummary( - prefix: Option[Metric.Prefix], - name: Summary.Name, - help: Metric.Help, - commonLabels: CommonLabels, - quantiles: Seq[QuantileDefinition], - maxAge: FiniteDuration, - ageBuckets: Summary.AgeBuckets - ): Resource[G, Summary[G, Long]] = - self - .createAndRegisterLongSummary(prefix, name, help, commonLabels, quantiles, maxAge, ageBuckets) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledDoubleSummary[A]( - prefix: Option[Metric.Prefix], - name: Summary.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - quantiles: Seq[QuantileDefinition], - maxAge: FiniteDuration, - ageBuckets: Summary.AgeBuckets - )(f: A => IndexedSeq[String]): Resource[G, Summary.Labelled[G, Double, A]] = - self - .createAndRegisterLabelledDoubleSummary( - prefix, - name, - help, - commonLabels, - labelNames, - quantiles, - maxAge, - ageBuckets - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterLabelledLongSummary[A]( - prefix: Option[Metric.Prefix], - name: Summary.Name, - help: Metric.Help, - commonLabels: CommonLabels, - labelNames: IndexedSeq[Label.Name], - quantiles: Seq[QuantileDefinition], - maxAge: FiniteDuration, - ageBuckets: Summary.AgeBuckets - )(f: A => IndexedSeq[String]): Resource[G, Summary.Labelled[G, Long, A]] = - self - .createAndRegisterLabelledLongSummary( - prefix, - name, - help, - commonLabels, - labelNames, - quantiles, - maxAge, - ageBuckets - )(f) - .mapK(fk) - .map(_.mapK(fk)) - - override def createAndRegisterInfo( - prefix: Option[Metric.Prefix], - name: Info.Name, - help: Metric.Help - ): Resource[G, Info[G, Map[Label.Name, String]]] = - self.createAndRegisterInfo(prefix, name, help).mapK(fk).map(_.mapK(fk)) - - } - } - - def noop[F[_]: Applicative]: MetricRegistry[F] = WithExemplars.noop[F] + def noop[F[_]](implicit F: Applicative[F]): MetricRegistry[F] = + new DoubleMetricRegistry[F] { + + override def createAndRegisterDoubleExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[F, Counter.Exemplar[F, Double]] = Resource.pure(Counter.Exemplar.noop) + + override def createAndRegisterDoubleCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels + ): Resource[F, Counter[F, Double]] = Resource.pure(Counter.noop) + + override def createAndRegisterLabelledDoubleCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled[F, Double, A]] = + Resource.pure(Counter.Labelled.noop) + + override def createAndRegisterLabelledDoubleExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Labelled.Exemplar[F, Double, A]] = + Resource.pure(Counter.Labelled.Exemplar.noop) + + override def createAndRegisterDoubleGauge( + prefix: Option[Metric.Prefix], + name: Gauge.Name, + help: Metric.Help, + commonLabels: CommonLabels + ): Resource[F, Gauge[F, Double]] = + Resource.pure(Gauge.noop) + + override def createAndRegisterLabelledDoubleGauge[A]( + prefix: Option[Metric.Prefix], + name: Gauge.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Gauge.Labelled[F, Double, A]] = + Resource.pure(Gauge.Labelled.noop) + + override def createAndRegisterDoubleHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + buckets: NonEmptySeq[Double] + ): Resource[F, Histogram[F, Double]] = Resource.pure(Histogram.noop) + + override def createAndRegisterDoubleExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + buckets: NonEmptySeq[Double] + ): Resource[F, Histogram.Exemplar[F, Double]] = Resource.pure(Histogram.Exemplar.noop) + + override def createAndRegisterLabelledDoubleHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Double] + )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled[F, Double, A]] = + Resource.pure(Histogram.Labelled.noop) + + override def createAndRegisterLabelledDoubleExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Double] + )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, A]] = + Resource.pure(Histogram.Labelled.Exemplar.noop) + + override def createAndRegisterDoubleSummary( + prefix: Option[Metric.Prefix], + name: Summary.Name, + help: Metric.Help, + commonLabels: CommonLabels, + quantiles: Seq[QuantileDefinition], + maxAge: FiniteDuration, + ageBuckets: Summary.AgeBuckets + ): Resource[F, Summary[F, Double]] = Resource.pure(Summary.noop) + + override def createAndRegisterLabelledDoubleSummary[A]( + prefix: Option[Metric.Prefix], + name: Summary.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name], + quantiles: Seq[QuantileDefinition], + maxAge: FiniteDuration, + ageBuckets: Summary.AgeBuckets + )(f: A => IndexedSeq[String]): Resource[F, Summary.Labelled[F, Double, A]] = + Resource.pure(Summary.Labelled.noop) + + override def createAndRegisterInfo( + prefix: Option[Metric.Prefix], + name: Info.Name, + help: Metric.Help + ): Resource[F, Info[F, Map[Label.Name, String]]] = Resource.pure(Info.noop) + } private[prometheus4cats] def mapK[F[_], G[_]]( self: MetricRegistry[F], @@ -1199,6 +830,32 @@ object MetricRegistry { .mapK(fk) .map(_.mapK(fk)) + override def createAndRegisterDoubleExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels + ): Resource[G, Counter.Exemplar[G, Double]] = + self.createAndRegisterDoubleExemplarCounter(prefix, name, help, commonLabels).mapK(fk).map(_.mapK(fk)) + + override def createAndRegisterLabelledDoubleExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[G, Counter.Labelled.Exemplar[G, Double, A]] = + self + .createAndRegisterLabelledDoubleExemplarCounter( + prefix, + name, + help, + commonLabels, + labelNames + )(f) + .mapK(fk) + .map(_.mapK(fk)) + override def createAndRegisterDoubleGauge( prefix: Option[Metric.Prefix], name: Gauge.Name, @@ -1277,6 +934,44 @@ object MetricRegistry { .mapK(fk) .map(_.mapK(fk)) + override def createAndRegisterDoubleExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + buckets: NonEmptySeq[Double] + ): Resource[G, Histogram.Exemplar[G, Double]] = + self + .createAndRegisterDoubleExemplarHistogram( + prefix, + name, + help, + commonLabels, + buckets + ) + .mapK(fk) + .map(_.mapK(fk)) + + override def createAndRegisterLabelledDoubleExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Double] + )(f: A => IndexedSeq[String]): Resource[G, Histogram.Labelled.Exemplar[G, Double, A]] = + self + .createAndRegisterLabelledDoubleExemplarHistogram( + prefix, + name, + help, + commonLabels, + labelNames, + buckets + )(f) + .mapK(fk) + .map(_.mapK(fk)) + override def createAndRegisterLongCounter( prefix: Option[Metric.Prefix], name: Counter.Name, @@ -1297,6 +992,26 @@ object MetricRegistry { .mapK(fk) .map(_.mapK(fk)) + override def createAndRegisterLongExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels + ): Resource[G, Counter.Exemplar[G, Long]] = + self.createAndRegisterLongExemplarCounter(prefix, name, help, commonLabels).mapK(fk).map(_.mapK(fk)) + + override def createAndRegisterLabelledLongExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[G, Counter.Labelled.Exemplar[G, Long, A]] = + self + .createAndRegisterLabelledLongExemplarCounter(prefix, name, help, commonLabels, labelNames)(f) + .mapK(fk) + .map(_.mapK(fk)) + override def createAndRegisterLabelledLongGauge[A]( prefix: Option[Metric.Prefix], name: Gauge.Name, @@ -1331,6 +1046,31 @@ object MetricRegistry { .mapK(fk) .map(_.mapK(fk)) + override def createAndRegisterLongExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + buckets: NonEmptySeq[Long] + ): Resource[G, Histogram.Exemplar[G, Long]] = + self + .createAndRegisterLongExemplarHistogram(prefix, name, help, commonLabels, buckets) + .mapK(fk) + .map(_.mapK(fk)) + + override def createAndRegisterLabelledLongExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Long] + )(f: A => IndexedSeq[String]): Resource[G, Histogram.Labelled.Exemplar[G, Long, A]] = + self + .createAndRegisterLabelledLongExemplarHistogram(prefix, name, help, commonLabels, labelNames, buckets)(f) + .mapK(fk) + .map(_.mapK(fk)) + override def createAndRegisterDoubleSummary( prefix: Option[Metric.Prefix], name: Summary.Name, diff --git a/core/src/main/scala/prometheus4cats/util/DoubleMetricRegistry.scala b/core/src/main/scala/prometheus4cats/util/DoubleMetricRegistry.scala index 52bb5def..fe561b60 100644 --- a/core/src/main/scala/prometheus4cats/util/DoubleMetricRegistry.scala +++ b/core/src/main/scala/prometheus4cats/util/DoubleMetricRegistry.scala @@ -106,57 +106,52 @@ trait DoubleMetricRegistry[F[_]] extends MetricRegistry[F] { createAndRegisterLabelledDoubleSummary(prefix, name, help, commonLabels, labelNames, quantiles, maxAge, ageBuckets)( f ).map(_.contramap(_.toDouble)) -} - -object DoubleMetricRegistry { - trait WithExemplars[F[_]] extends DoubleMetricRegistry[F] with MetricRegistry.WithExemplars[F] { - override def createAndRegisterLongExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels - ): Resource[F, Counter.Exemplar[F, Long]] = - createAndRegisterDoubleExemplarCounter(prefix, name, help, commonLabels).map(_.contramap(_.toDouble)) - override def createAndRegisterLabelledLongExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Long, A]] = - createAndRegisterLabelledDoubleExemplarCounter(prefix, name, help, commonLabels, labelNames)(f).map( - _.contramap(_.toDouble) - ) + override def createAndRegisterLongExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[F, Counter.Exemplar[F, Long]] = + createAndRegisterDoubleExemplarCounter(prefix, name, help, commonLabels).map(_.contramap(_.toDouble)) - override def createAndRegisterLongExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - buckets: NonEmptySeq[Long] - ): Resource[F, Histogram.Exemplar[F, Long]] = - createAndRegisterDoubleExemplarHistogram(prefix, name, help, commonLabels, buckets.map(_.toDouble)) - .map(_.contramap(_.toDouble)) + override def createAndRegisterLabelledLongExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Long, A]] = + createAndRegisterLabelledDoubleExemplarCounter(prefix, name, help, commonLabels, labelNames)(f).map( + _.contramap(_.toDouble) + ) - override def createAndRegisterLabelledLongExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Long] - )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Long, A]] = - createAndRegisterLabelledDoubleExemplarHistogram( - prefix, - name, - help, - commonLabels, - labelNames, - buckets.map(_.toDouble) - )(f).map( - _.contramap(_.toDouble) - ) + override def createAndRegisterLongExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + buckets: NonEmptySeq[Long] + ): Resource[F, Histogram.Exemplar[F, Long]] = + createAndRegisterDoubleExemplarHistogram(prefix, name, help, commonLabels, buckets.map(_.toDouble)) + .map(_.contramap(_.toDouble)) - } + override def createAndRegisterLabelledLongExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Long] + )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Long, A]] = + createAndRegisterLabelledDoubleExemplarHistogram( + prefix, + name, + help, + commonLabels, + labelNames, + buckets.map(_.toDouble) + )(f).map( + _.contramap(_.toDouble) + ) } diff --git a/core/src/test/scala/test/ExternalPackageMetricRegistry.scala b/core/src/test/scala/test/ExternalPackageMetricRegistry.scala index 0b646997..a145e32b 100644 --- a/core/src/test/scala/test/ExternalPackageMetricRegistry.scala +++ b/core/src/test/scala/test/ExternalPackageMetricRegistry.scala @@ -313,4 +313,67 @@ class ExternalPackageMetricRegistry extends MetricRegistry[IO] with CallbackRegi callback: IO[MetricCollection] ): Resource[IO, Unit] = ??? + override def createAndRegisterLongExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[IO, Counter.Exemplar[IO, Long]] = ??? + + override def createAndRegisterDoubleExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[IO, Counter.Exemplar[IO, Double]] = ??? + + override def createAndRegisterLabelledDoubleExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[IO, Counter.Labelled.Exemplar[IO, Double, A]] = ??? + + override def createAndRegisterLabelledLongExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[IO, Counter.Labelled.Exemplar[IO, Long, A]] = ??? + + override def createAndRegisterDoubleExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + buckets: NonEmptySeq[Double] + ): Resource[IO, Histogram.Exemplar[IO, Double]] = ??? + + override def createAndRegisterLongExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + buckets: NonEmptySeq[Long] + ): Resource[IO, Histogram.Exemplar[IO, Long]] = ??? + + override def createAndRegisterLabelledDoubleExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Double] + )(f: A => IndexedSeq[String]): Resource[IO, Histogram.Labelled.Exemplar[IO, Double, A]] = ??? + + override def createAndRegisterLabelledLongExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Long] + )(f: A => IndexedSeq[String]): Resource[IO, Histogram.Labelled.Exemplar[IO, Long, A]] = ??? } diff --git a/java/src/main/scala/prometheus4cats/javasimpleclient/JavaMetricRegistry.scala b/java/src/main/scala/prometheus4cats/javasimpleclient/JavaMetricRegistry.scala index 8cde791d..196e519c 100644 --- a/java/src/main/scala/prometheus4cats/javasimpleclient/JavaMetricRegistry.scala +++ b/java/src/main/scala/prometheus4cats/javasimpleclient/JavaMetricRegistry.scala @@ -25,19 +25,7 @@ import cats.effect.std.{Dispatcher, Semaphore} import cats.syntax.all._ import cats.{Applicative, ApplicativeThrow, Functor, Monad, Show} import io.prometheus.client.Collector.MetricFamilySamples -import io.prometheus.client.{ - Collector, - CollectorRegistry, - CounterMetricFamily, - GaugeMetricFamily, - SimpleCollector, - SummaryMetricFamily, - Counter => PCounter, - Gauge => PGauge, - Histogram => PHistogram, - Info => PInfo, - Summary => PSummary -} +import io.prometheus.client.{Collector, CollectorRegistry, CounterMetricFamily, GaugeMetricFamily, SimpleCollector, SummaryMetricFamily, Counter => PCounter, Gauge => PGauge, Histogram => PHistogram, Info => PInfo, Summary => PSummary} import org.typelevel.log4cats.Logger import prometheus4cats._ import prometheus4cats.javasimpleclient.internal.{HistogramUtils, MetricCollectionProcessor, Utils} @@ -851,180 +839,146 @@ class JavaMetricRegistry[F[_]: Async: Logger] private ( callback: F[MetricCollection] ): Resource[F, Unit] = metricCollectionCollector.register(prefix, commonLabels, callback) -} - -object JavaMetricRegistry { - - private val callbacksCounterName = "prometheus4cats_combined_callback_metric_total" - private val callbacksCounterHelp = - "Number of times all callbacks for a metric have been executed, with a status (success, error, timeout)" - - private val callbackCounterName = "prometheus4cats_callback_total" - private val callbackCounterHelp = - "Number of times each metric callback has been executed, with a status (success, error, timeout)" - private val callbackCounterLabels = List("metric_name", "status") + private def transformExemplarLabels(labels: Exemplar.Labels): util.Map[String, String] = labels.value.map { + case (k, v) => k.value -> v + }.asJava - class WithExemplars[F[_]: Async: Logger: Exemplar] private[JavaMetricRegistry] ( - private val registry: CollectorRegistry, - private val ref: Ref[F, State], - private val callbackState: Ref[F, CallbackState[F]], - private val callbackTimeoutState: Ref[F, Set[String]], - private val callbackErrorState: Ref[F, Set[String]], - private val singleCallbackErrorState: Ref[F, (Set[String], Set[String])], - private val callbackCounter: PCounter, - private val singleCallbackCounter: PCounter, - private val metricCollectionCollector: MetricCollectionProcessor[F], - private val sem: Semaphore[F], - private val dispatcher: Dispatcher[F], - private val callbackTimeout: FiniteDuration, - private val singleCallbackTimeout: FiniteDuration - ) extends JavaMetricRegistry[F]( - registry, - ref, - callbackState, - callbackTimeoutState, - callbackErrorState, - singleCallbackErrorState, - callbackCounter, - singleCallbackCounter, - metricCollectionCollector, - sem, - dispatcher, - callbackTimeout, - singleCallbackTimeout - ) - with DoubleMetricRegistry.WithExemplars[F] { - - private def transformExemplarLabels(labels: Exemplar.Labels): util.Map[String, String] = labels.value.map { - case (k, v) => k.value -> v - }.asJava - - def createAndRegisterDoubleExemplarCounter( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels - ): Resource[F, Counter.Exemplar[F, Double]] = { - lazy val commonLabelNames = commonLabels.value.keys.toIndexedSeq - lazy val commonLabelValues = commonLabels.value.values.toIndexedSeq - - configureBuilderOrRetrieve( - PCounter.build().withExemplars(), - MetricType.Counter, - prefix, - name, - help, - commonLabels.value.keys.toIndexedSeq - ).map { counter => - Counter.Exemplar.make( - 1.0, - (d: Double, ex: Option[Exemplar.Labels]) => - Utils - .modifyMetric[F, Counter.Name, PCounter.Child]( - counter, - name, - commonLabelNames, - commonLabelValues, - c => ex.fold(c.inc(d))(e => c.incWithExemplar(d, transformExemplarLabels(e))) - ) - ) - } - } + def createAndRegisterDoubleExemplarCounter( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels + ): Resource[F, Counter.Exemplar[F, Double]] = { + lazy val commonLabelNames = commonLabels.value.keys.toIndexedSeq + lazy val commonLabelValues = commonLabels.value.values.toIndexedSeq - override def createAndRegisterLabelledDoubleExemplarCounter[A]( - prefix: Option[Metric.Prefix], - name: Counter.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name] - )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Double, A]] = { - val commonLabelNames = commonLabels.value.keys.toIndexedSeq - val commonLabelValues = commonLabels.value.values.toIndexedSeq - - configureBuilderOrRetrieve( - PCounter.build(), - MetricType.Counter, - prefix, - name, - help, - labelNames ++ commonLabelNames - ).map { counter => - Counter.Labelled.Exemplar.make( - 1.0, - (d: Double, labels: A, ex: Option[Exemplar.Labels]) => - Utils.modifyMetric[F, Counter.Name, PCounter.Child]( + configureBuilderOrRetrieve( + PCounter.build().withExemplars(), + MetricType.Counter, + prefix, + name, + help, + commonLabels.value.keys.toIndexedSeq + ).map { counter => + Counter.Exemplar.make( + 1.0, + (d: Double, ex: Option[Exemplar.Labels]) => + Utils + .modifyMetric[F, Counter.Name, PCounter.Child]( counter, name, - labelNames ++ commonLabelNames, - f(labels) ++ commonLabelValues, + commonLabelNames, + commonLabelValues, c => ex.fold(c.inc(d))(e => c.incWithExemplar(d, transformExemplarLabels(e))) ) - ) - } + ) } + } + + override def createAndRegisterLabelledDoubleExemplarCounter[A]( + prefix: Option[Metric.Prefix], + name: Counter.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name] + )(f: A => IndexedSeq[String]): Resource[F, Counter.Labelled.Exemplar[F, Double, A]] = { + val commonLabelNames = commonLabels.value.keys.toIndexedSeq + val commonLabelValues = commonLabels.value.values.toIndexedSeq - override def createAndRegisterDoubleExemplarHistogram( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - buckets: NonEmptySeq[Double] - ): Resource[F, Histogram.Exemplar[F, Double]] = { - val commonLabelNames = commonLabels.value.keys.toIndexedSeq - val commonLabelValues = commonLabels.value.values.toIndexedSeq - - configureBuilderOrRetrieve( - PHistogram.build().withExemplars().buckets(buckets.toSeq: _*), - MetricType.Histogram, - prefix, - name, - help, - commonLabelNames - ).map { histogram => - Histogram.Exemplar.make((d, ex) => - Utils.modifyMetric[F, Histogram.Name, PHistogram.Child]( - histogram, + configureBuilderOrRetrieve( + PCounter.build(), + MetricType.Counter, + prefix, + name, + help, + labelNames ++ commonLabelNames + ).map { counter => + Counter.Labelled.Exemplar.make( + 1.0, + (d: Double, labels: A, ex: Option[Exemplar.Labels]) => + Utils.modifyMetric[F, Counter.Name, PCounter.Child]( + counter, name, - commonLabelNames, - commonLabelValues, - h => ex.fold(h.observe(d))(e => h.observeWithExemplar(d, transformExemplarLabels(e))) + labelNames ++ commonLabelNames, + f(labels) ++ commonLabelValues, + c => ex.fold(c.inc(d))(e => c.incWithExemplar(d, transformExemplarLabels(e))) ) + ) + } + } + + override def createAndRegisterDoubleExemplarHistogram( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + buckets: NonEmptySeq[Double] + ): Resource[F, Histogram.Exemplar[F, Double]] = { + val commonLabelNames = commonLabels.value.keys.toIndexedSeq + val commonLabelValues = commonLabels.value.values.toIndexedSeq + + configureBuilderOrRetrieve( + PHistogram.build().withExemplars().buckets(buckets.toSeq: _*), + MetricType.Histogram, + prefix, + name, + help, + commonLabelNames + ).map { histogram => + Histogram.Exemplar.make((d, ex) => + Utils.modifyMetric[F, Histogram.Name, PHistogram.Child]( + histogram, + name, + commonLabelNames, + commonLabelValues, + h => ex.fold(h.observe(d))(e => h.observeWithExemplar(d, transformExemplarLabels(e))) ) - } + ) } + } - override def createAndRegisterLabelledDoubleExemplarHistogram[A]( - prefix: Option[Metric.Prefix], - name: Histogram.Name, - help: Metric.Help, - commonLabels: Metric.CommonLabels, - labelNames: IndexedSeq[Label.Name], - buckets: NonEmptySeq[Double] - )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, A]] = { - val commonLabelNames = commonLabels.value.keys.toIndexedSeq - val commonLabelValues = commonLabels.value.values.toIndexedSeq - - configureBuilderOrRetrieve( - PHistogram.build().buckets(buckets.toSeq: _*), - MetricType.Histogram, - prefix, - name, - help, - labelNames ++ commonLabelNames - ).map { histogram => - Histogram.Labelled.Exemplar.make[F, Double, A](_observe = { - (d: Double, labels: A, ex: Option[Exemplar.Labels]) => - Utils.modifyMetric[F, Histogram.Name, PHistogram.Child]( - histogram, - name, - labelNames ++ commonLabelNames, - f(labels) ++ commonLabelValues, - h => ex.fold(h.observe(d))(e => h.observeWithExemplar(d, transformExemplarLabels(e))) - ) - }) - } + override def createAndRegisterLabelledDoubleExemplarHistogram[A]( + prefix: Option[Metric.Prefix], + name: Histogram.Name, + help: Metric.Help, + commonLabels: Metric.CommonLabels, + labelNames: IndexedSeq[Label.Name], + buckets: NonEmptySeq[Double] + )(f: A => IndexedSeq[String]): Resource[F, Histogram.Labelled.Exemplar[F, Double, A]] = { + val commonLabelNames = commonLabels.value.keys.toIndexedSeq + val commonLabelValues = commonLabels.value.values.toIndexedSeq + + configureBuilderOrRetrieve( + PHistogram.build().buckets(buckets.toSeq: _*), + MetricType.Histogram, + prefix, + name, + help, + labelNames ++ commonLabelNames + ).map { histogram => + Histogram.Labelled.Exemplar.make[F, Double, A](_observe = { (d: Double, labels: A, ex: Option[Exemplar.Labels]) => + Utils.modifyMetric[F, Histogram.Name, PHistogram.Child]( + histogram, + name, + labelNames ++ commonLabelNames, + f(labels) ++ commonLabelValues, + h => ex.fold(h.observe(d))(e => h.observeWithExemplar(d, transformExemplarLabels(e))) + ) + }) } } +} + +object JavaMetricRegistry { + + private val callbacksCounterName = "prometheus4cats_combined_callback_metric_total" + private val callbacksCounterHelp = + "Number of times all callbacks for a metric have been executed, with a status (success, error, timeout)" + + private val callbackCounterName = "prometheus4cats_callback_total" + private val callbackCounterHelp = + "Number of times each metric callback has been executed, with a status (success, error, timeout)" + private val callbackCounterLabels = List("metric_name", "status") sealed abstract class Builder( val promRegistry: CollectorRegistry, @@ -1043,7 +997,7 @@ object JavaMetricRegistry { def withCallbackCollectionTimeout(callbackCollectionTimeout: FiniteDuration): Builder = copy(callbackCollectionTimeout = callbackCollectionTimeout) - def build[F[_]: Async: Logger: Exemplar]: Resource[F, JavaMetricRegistry.WithExemplars[F]] = + def build[F[_]: Async: Logger]: Resource[F, JavaMetricRegistry[F]] = Dispatcher.sequential[F].flatMap { dis => val callbacksCounter = PCounter @@ -1085,7 +1039,7 @@ object JavaMetricRegistry { metricsGauge, callbacksGauge, metricCollectionProcessor._2, - new JavaMetricRegistry.WithExemplars[F]( + new JavaMetricRegistry[F]( promRegistry, ref, callbackState, diff --git a/testing/src/main/scala/prometheus4cats/testing/TestingMetricRegistry.scala b/testing/src/main/scala/prometheus4cats/testing/TestingMetricRegistry.scala index 57a0aad9..23f4ebef 100644 --- a/testing/src/main/scala/prometheus4cats/testing/TestingMetricRegistry.scala +++ b/testing/src/main/scala/prometheus4cats/testing/TestingMetricRegistry.scala @@ -32,7 +32,7 @@ sealed abstract class TestingMetricRegistry[F[_]] private ( ]], private val info: MapRef[F, String, Option[(Int, Info[F, Map[Label.Name, String]])]] )(implicit override val F: Concurrent[F]) - extends DoubleMetricRegistry.WithExemplars[F] + extends DoubleMetricRegistry[F] with DoubleCallbackRegistry[F] { def counterHistory(name: Counter.Name, commonLabels: Metric.CommonLabels): F[Option[Chain[Double]]] =