Skip to content

Commit

Permalink
Restore original name for binary compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dj707chen committed May 25, 2024
1 parent 5522c7d commit a9178e8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -77,47 +77,46 @@ 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,
sampleExemplar: F[Option[Map[String, String]]] = self.sampleExemplar,
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,
customLabelsAndValues = customLabelsAndValues,
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)
Expand Down Expand Up @@ -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()))

Expand All @@ -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.
*
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}

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

Expand Down

0 comments on commit a9178e8

Please sign in to comment.