From a9178e833fc6c34f282d79feb10ce17ae4cb0a58 Mon Sep 17 00:00:00 2001 From: DJ Chen <80285637+dj707chen@users.noreply.github.com> Date: Sat, 25 May 2024 12:10:28 -0500 Subject: [PATCH] Restore original name for binary compatibility --- ...tricsOpsBuilder.scala => Prometheus.scala} | 35 +++++++++---------- .../prometheus/PrometheusExportService.scala | 16 ++++----- ...etheusClientMetricsCustomLabelsSuite.scala | 4 +-- ...PrometheusExemplarsCustomLabelsSuite.scala | 6 ++-- ...etheusServerMetricsCustomLabelsSuite.scala | 4 +-- 5 files changed, 32 insertions(+), 33 deletions(-) rename prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/{MetricsOpsBuilder.scala => Prometheus.scala} (95%) diff --git a/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/MetricsOpsBuilder.scala b/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/Prometheus.scala similarity index 95% rename from prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/MetricsOpsBuilder.scala rename to prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/Prometheus.scala index 4c17ebf..b634735 100644 --- a/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/MetricsOpsBuilder.scala +++ b/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/Prometheus.scala @@ -21,7 +21,7 @@ import cats.effect.{Resource, Sync} import cats.syntax.all.* import io.prometheus.client.* import org.http4s.metrics.TerminationType.{Abnormal, Canceled, Error, Timeout} -import org.http4s.metrics.prometheus.MetricsOpsBuilder.{registerCollector, toFlatArray} +import org.http4s.metrics.prometheus.Prometheus.{registerCollector, toFlatArray} import org.http4s.metrics.{MetricsOps, TerminationType} import org.http4s.{Method, Status} @@ -77,14 +77,13 @@ import org.http4s.{Method, Status} * custom labels: custom labels, provided by customLabelsAndValues.map(_._1) * values: custom label values, provided by customLabelsAndValues.map(_._2) */ -final class MetricsOpsBuilder[F[_]: Sync] private ( +final class Prometheus[F[_]: Sync] private ( private val prefix: String, private val registry: CollectorRegistry, private val sampleExemplar: F[Option[Map[String, String]]], private val customLabelsAndValues: List[(String, String)], private val responseDurationSecondsHistogramBuckets: NonEmptyList[Double], ) { self => - private def copy( prefix: String = self.prefix, registry: CollectorRegistry = self.registry, @@ -92,8 +91,8 @@ final class MetricsOpsBuilder[F[_]: Sync] private ( customLabelsAndValues: List[(String, String)] = self.customLabelsAndValues, responseDurationSecondsHistogramBuckets: NonEmptyList[Double] = self.responseDurationSecondsHistogramBuckets, - ): MetricsOpsBuilder[F] = - new MetricsOpsBuilder[F]( + ): Prometheus[F] = + new Prometheus[F]( prefix = prefix, registry = registry, sampleExemplar = sampleExemplar, @@ -101,23 +100,23 @@ final class MetricsOpsBuilder[F[_]: Sync] private ( responseDurationSecondsHistogramBuckets = responseDurationSecondsHistogramBuckets, ) - def withPrefix(prefix: String): MetricsOpsBuilder[F] = copy(prefix = prefix) - def withRegister(registry: CollectorRegistry): MetricsOpsBuilder[F] = copy(registry = registry) + def withPrefix(prefix: String): Prometheus[F] = copy(prefix = prefix) + def withRegister(registry: CollectorRegistry): Prometheus[F] = copy(registry = registry) - def withSampleExemplar(sampleExemplar: F[Option[Map[String, String]]]): MetricsOpsBuilder[F] = + def withSampleExemplar(sampleExemplar: F[Option[Map[String, String]]]): Prometheus[F] = copy(sampleExemplar = sampleExemplar) def withCustomLabelsAndValues( customLabelsAndValues: List[(String, String)] - ): MetricsOpsBuilder[F] = copy(customLabelsAndValues = customLabelsAndValues) + ): Prometheus[F] = copy(customLabelsAndValues = customLabelsAndValues) def withResponseDurationSecondsHistogramBuckets( responseDurationSecondsHistogramBuckets: NonEmptyList[Double] - ): MetricsOpsBuilder[F] = + ): Prometheus[F] = copy(responseDurationSecondsHistogramBuckets = responseDurationSecondsHistogramBuckets) - /** Creates a [[MetricsOps]] that supports Prometheus metrics */ - def metricsOps: Resource[F, MetricsOps[F]] = createMetricsCollection.map(createMetricsOps) + /** Build a [[MetricsOps]] that supports Prometheus metrics */ + def build: Resource[F, MetricsOps[F]] = createMetricsCollection.map(createMetricsOps) private def createMetricsOps(metrics: MetricsCollection): MetricsOps[F] = { val customLabels: List[String] = customLabelsAndValues.map(_._2) @@ -360,7 +359,7 @@ final class MetricsOpsBuilder[F[_]: Sync] private ( } } -object MetricsOpsBuilder { +object Prometheus { def collectorRegistry[F[_]](implicit F: Sync[F]): Resource[F, CollectorRegistry] = Resource.make(F.delay(new CollectorRegistry()))(cr => F.blocking(cr.clear())) @@ -374,11 +373,11 @@ object MetricsOpsBuilder { prefix: String = "org_http4s_server", responseDurationSecondsHistogramBuckets: NonEmptyList[Double] = DefaultHistogramBuckets, ): Resource[F, MetricsOps[F]] = - MetricsOpsBuilder + Prometheus .default(registry) .withPrefix(prefix) .withResponseDurationSecondsHistogramBuckets(responseDurationSecondsHistogramBuckets) - .metricsOps + .build /** Creates a [[MetricsOps]] that supports Prometheus metrics and records exemplars. * @@ -397,12 +396,12 @@ object MetricsOpsBuilder { prefix: String = "org_http4s_server", responseDurationSecondsHistogramBuckets: NonEmptyList[Double] = DefaultHistogramBuckets, ): Resource[F, MetricsOps[F]] = - MetricsOpsBuilder + Prometheus .default[F](registry) .withPrefix(prefix) .withSampleExemplar(sampleExemplar) .withResponseDurationSecondsHistogramBuckets(responseDurationSecondsHistogramBuckets) - .metricsOps + .build private[prometheus] def registerCollector[F[_], C <: Collector]( collector: C, @@ -429,7 +428,7 @@ object MetricsOpsBuilder { } def default[F[_]: Sync](registry: CollectorRegistry) = - new MetricsOpsBuilder[F]( + new Prometheus[F]( prefix = "org_http4s_server", registry = registry, sampleExemplar = Option.empty[Map[String, String]].pure, diff --git a/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/PrometheusExportService.scala b/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/PrometheusExportService.scala index 9e2cdfa..86c65ed 100644 --- a/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/PrometheusExportService.scala +++ b/prometheus-metrics/src/main/scala/org/http4s/metrics/prometheus/PrometheusExportService.scala @@ -110,13 +110,13 @@ object PrometheusExportService { def addDefaults[F[_]: Sync](cr: CollectorRegistry): Resource[F, Unit] = for { - _ <- MetricsOpsBuilder.registerCollector(new StandardExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new MemoryPoolsExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new BufferPoolsExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new GarbageCollectorExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new ThreadExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new ClassLoadingExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new VersionInfoExports(), cr) - _ <- MetricsOpsBuilder.registerCollector(new MemoryAllocationExports(), cr) + _ <- Prometheus.registerCollector(new StandardExports(), cr) + _ <- Prometheus.registerCollector(new MemoryPoolsExports(), cr) + _ <- Prometheus.registerCollector(new BufferPoolsExports(), cr) + _ <- Prometheus.registerCollector(new GarbageCollectorExports(), cr) + _ <- Prometheus.registerCollector(new ThreadExports(), cr) + _ <- Prometheus.registerCollector(new ClassLoadingExports(), cr) + _ <- Prometheus.registerCollector(new VersionInfoExports(), cr) + _ <- Prometheus.registerCollector(new MemoryAllocationExports(), cr) } yield () } diff --git a/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusClientMetricsCustomLabelsSuite.scala b/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusClientMetricsCustomLabelsSuite.scala index a2b263c..6a67778 100644 --- a/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusClientMetricsCustomLabelsSuite.scala +++ b/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusClientMetricsCustomLabelsSuite.scala @@ -224,11 +224,11 @@ class PrometheusClientMetricsCustomLabelsSuite extends CatsEffectSuite { for { registry <- Prometheus.collectorRegistry[IO] - metrics <- MetricsOpsBuilder + metrics <- Prometheus .default[IO](registry) .withPrefix("client") .withCustomLabelsAndValues(custLblVals) - .metricsOps + .build } yield (registry, Metrics(metrics, classifier)(client)) } diff --git a/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusExemplarsCustomLabelsSuite.scala b/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusExemplarsCustomLabelsSuite.scala index 0145043..16ed109 100644 --- a/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusExemplarsCustomLabelsSuite.scala +++ b/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusExemplarsCustomLabelsSuite.scala @@ -23,7 +23,7 @@ import munit.CatsEffectSuite import org.http4s.HttpApp import org.http4s.client.Client import org.http4s.client.middleware.Metrics -import org.http4s.metrics.prometheus.MetricsOpsBuilder.DefaultHistogramBuckets +import org.http4s.metrics.prometheus.Prometheus.DefaultHistogramBuckets import org.http4s.metrics.prometheus.util.* class PrometheusExemplarsCustomLabelsSuite extends CatsEffectSuite { @@ -55,13 +55,13 @@ class PrometheusExemplarsCustomLabelsSuite extends CatsEffectSuite { for { registry <- Prometheus.collectorRegistry[IO] - metrics <- MetricsOpsBuilder + metrics <- Prometheus .default[IO](registry) .withPrefix("exemplars") .withSampleExemplar(IO.pure(Some(exemplar))) .withCustomLabelsAndValues(custLblVals) .withResponseDurationSecondsHistogramBuckets(DefaultHistogramBuckets) - .metricsOps + .build } yield (registry, Metrics[IO](metrics)(client)) } diff --git a/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusServerMetricsCustomLabelsSuite.scala b/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusServerMetricsCustomLabelsSuite.scala index 7e14843..f54cc3a 100644 --- a/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusServerMetricsCustomLabelsSuite.scala +++ b/prometheus-metrics/src/test/scala/org/http4s/metrics/prometheus/PrometheusServerMetricsCustomLabelsSuite.scala @@ -290,11 +290,11 @@ class PrometheusServerMetricsCustomLabelsSuite extends CatsEffectSuite { implicit val clock: Clock[IO] = FakeClock[IO] for { registry <- Prometheus.collectorRegistry[IO] - metrics <- MetricsOpsBuilder + metrics <- Prometheus .default[IO](registry) .withPrefix("server") .withCustomLabelsAndValues(custLblVals) - .metricsOps + .build } yield (registry, Metrics(metrics, classifierF = classifier)(testRoutes).orNotFound) }