Skip to content

Commit 3f328e3

Browse files
authored
Update prometheus to v1 (RustedBones#136)
Fix RustedBones#84
1 parent 628f8a9 commit 3f328e3

File tree

11 files changed

+466
-217
lines changed

11 files changed

+466
-217
lines changed

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ libraryDependencies += "fr.davit" %% "pekko-http-metrics-prometheus" % <version>
360360
Create your registry
361361

362362
```scala
363-
import io.prometheus.client.CollectorRegistry
363+
import io.prometheus.metrics.core.metrics.CollectorRegistry
364364
import fr.davit.pekko.http.metrics.prometheus.{PrometheusRegistry, PrometheusSettings}
365365

366366
val prometheus: CollectorRegistry = ... // your prometheus registry
@@ -373,7 +373,7 @@ You can fine-tune the `histogram/summary` configuration of `buckets/quantiles` f
373373

374374
```scala
375375
settings
376-
.withDurationConfig(Buckets(1, 2, 3, 5, 8, 13, 21, 34))
376+
.withDurationConfig(ClassicBuckets(1, 2, 3, 5, 8, 13, 21, 34))
377377
.withReceivedBytesConfig(Quantiles(0.5, 0.75, 0.9, 0.95, 0.99))
378378
.withSentBytesConfig(PrometheusSettings.DefaultQuantiles)
379379
```
@@ -388,16 +388,26 @@ val route = (get & path("metrics"))(metrics(registry))
388388
```
389389

390390
All metrics from the prometheus collector registry will be exposed.
391-
You can find some external exporters [here](https://github.com/prometheus/client_java). For instance, to expose some JVM
392-
metrics, you have to add the dedicated client dependency and initialize/register it to your collector registry:
391+
Marshalling [format](https://prometheus.github.io/client_java/exporters/formats/) depends on the `Accept`/`Content-Type` header sent by the client:
392+
393+
* `Content-Type: text/plain`: Prometheus text format
394+
* `Content-Type: application/openmetrics-text`: OpenMetrics text format
395+
* `Content-Type: application/vnd.google.protobuf`: Prometheus protobuf format
396+
397+
No `Accept` header or matching several (eg `Accept: application/*`) will take the 1st matching type from the above list.
398+
399+
400+
You can find some instrumentations [here](https://prometheus.github.io/client_java). For instance, to expose some JVM
401+
metrics, you have to add the dedicated dependency and initialize/register it to your collector registry:
393402

394403
```sbt
395-
libraryDependencies += "io.prometheus" % "simpleclient_hotspot" % <vesion>
404+
libraryDependencies += "io.prometheus" % "rometheus-metrics-instrumentation-jvm" % <vesion>
396405
```
397406

398407
```scala
399-
import io.prometheus.client.hotspot.DefaultExports
408+
import io.prometheus.metrics.model.registry.PrometheusRegistry
409+
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics
400410

401-
val prometheus: CollectorRegistry = ... // your prometheus registry
402-
DefaultExports.register(prometheus) // or DefaultExports.initialize() to use the default registry
411+
val prometheus: PrometheusRegistry = ??? // your prometheus registry
412+
JvmMetrics.builder().register(prometheus) // or JvmMetrics.builder().register() to use the default registry
403413
```

build.sbt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ val username = "RustedBones"
33
val repo = "pekko-http-metrics"
44
val githubUrl = s"https://github.com/$username/$repo"
55

6-
ThisBuild / tlBaseVersion := "1.1"
6+
ThisBuild / tlBaseVersion := "2.0"
77
ThisBuild / organization := "fr.davit"
88
ThisBuild / organizationName := "Michel Davit"
99
ThisBuild / startYear := Some(2019)
@@ -130,7 +130,8 @@ lazy val `pekko-http-metrics-prometheus` = (project in file("prometheus"))
130130
.dependsOn(`pekko-http-metrics-core`)
131131
.settings(
132132
libraryDependencies ++= Seq(
133-
Dependencies.PrometheusCommon,
133+
Dependencies.PrometheusCore,
134+
Dependencies.PrometheusExpositionFormats,
134135
Dependencies.Provided.PekkoStream,
135136
Dependencies.Test.Logback,
136137
Dependencies.Test.PekkoHttpTestkit,
@@ -159,7 +160,7 @@ lazy val integration = (project in file("integration"))
159160
Dependencies.Test.PekkoSlf4j,
160161
Dependencies.Test.PekkoStreamTestkit,
161162
Dependencies.Test.PekkoTestkit,
162-
Dependencies.Test.PrometheusHotspot,
163+
Dependencies.Test.PrometheusJvm,
163164
Dependencies.Test.ScalaTest
164165
)
165166
)

integration/src/test/scala/fr/davit/pekko/http/metrics/prometheus/PrometheusMetricsItSpec.scala

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package fr.davit.pekko.http.metrics.prometheus
1919
import fr.davit.pekko.http.metrics.core.HttpMetrics.*
2020
import fr.davit.pekko.http.metrics.core.scaladsl.server.HttpMetricsDirectives.*
2121
import fr.davit.pekko.http.metrics.prometheus.marshalling.PrometheusMarshallers.*
22-
import io.prometheus.client.hotspot.DefaultExports
22+
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics
2323
import org.apache.pekko.actor.ActorSystem
2424
import org.apache.pekko.http.scaladsl.Http
2525
import org.apache.pekko.http.scaladsl.model.{HttpRequest, StatusCodes, Uri}
@@ -55,7 +55,7 @@ class PrometheusMetricsItSpec
5555
.withIncludeMethodDimension(true)
5656
.withIncludePathDimension(true)
5757
.withIncludeStatusDimension(true)
58-
DefaultExports.initialize() // JVM
58+
JvmMetrics.builder().register()
5959
val registry = PrometheusRegistry(settings = settings)
6060

6161
val route: Route = (get & path("metrics"))(metrics(registry))
@@ -85,23 +85,26 @@ class PrometheusMetricsItSpec
8585
"# TYPE jvm_buffer_pool_used_bytes gauge",
8686
"# TYPE jvm_classes_currently_loaded gauge",
8787
"# TYPE jvm_classes_loaded_total counter",
88-
"# TYPE jvm_classes_unloaded_total counter",
88+
// might be missing if GC did not trigger ?
89+
// "# TYPE jvm_classes_unloaded_total counter",
90+
"# TYPE jvm_compilation_time_seconds_total counter",
8991
"# TYPE jvm_gc_collection_seconds summary",
90-
"# TYPE jvm_info gauge",
91-
"# TYPE jvm_memory_bytes_committed gauge",
92-
"# TYPE jvm_memory_bytes_init gauge",
93-
"# TYPE jvm_memory_bytes_max gauge",
94-
"# TYPE jvm_memory_bytes_used gauge",
92+
"# TYPE jvm_memory_committed_bytes gauge",
93+
"# TYPE jvm_memory_init_bytes gauge",
94+
"# TYPE jvm_memory_max_bytes gauge",
9595
"# TYPE jvm_memory_objects_pending_finalization gauge",
96-
"# TYPE jvm_memory_pool_allocated_bytes_total counter",
97-
"# TYPE jvm_memory_pool_bytes_committed gauge",
98-
"# TYPE jvm_memory_pool_bytes_init gauge",
99-
"# TYPE jvm_memory_pool_bytes_max gauge",
100-
"# TYPE jvm_memory_pool_bytes_used gauge",
96+
// might be missing
97+
// "# TYPE jvm_memory_pool_allocated_bytes_total counter",
10198
"# TYPE jvm_memory_pool_collection_committed_bytes gauge",
10299
"# TYPE jvm_memory_pool_collection_init_bytes gauge",
103100
"# TYPE jvm_memory_pool_collection_max_bytes gauge",
104101
"# TYPE jvm_memory_pool_collection_used_bytes gauge",
102+
"# TYPE jvm_memory_pool_committed_bytes gauge",
103+
"# TYPE jvm_memory_pool_init_bytes gauge",
104+
"# TYPE jvm_memory_pool_max_bytes gauge",
105+
"# TYPE jvm_memory_pool_used_bytes gauge",
106+
"# TYPE jvm_memory_used_bytes gauge",
107+
"# TYPE jvm_runtime_info gauge",
105108
"# TYPE jvm_threads_current gauge",
106109
"# TYPE jvm_threads_daemon gauge",
107110
"# TYPE jvm_threads_deadlocked gauge",
@@ -110,12 +113,9 @@ class PrometheusMetricsItSpec
110113
"# TYPE jvm_threads_started_total counter",
111114
"# TYPE jvm_threads_state gauge",
112115
"# TYPE pekko_http_connections_active gauge",
113-
"# TYPE pekko_http_connections_created gauge",
114116
"# TYPE pekko_http_connections_total counter",
115117
"# TYPE pekko_http_requests_active gauge",
116-
"# TYPE pekko_http_requests_created gauge",
117118
"# TYPE pekko_http_requests_size_bytes histogram",
118-
"# TYPE pekko_http_requests_size_bytes_created gauge",
119119
"# TYPE pekko_http_requests_total counter",
120120
"# TYPE process_cpu_seconds_total counter",
121121
"# TYPE process_max_fds gauge",
@@ -124,7 +124,6 @@ class PrometheusMetricsItSpec
124124
"# TYPE process_start_time_seconds gauge",
125125
"# TYPE process_virtual_memory_bytes gauge"
126126
)
127-
128127
binding.terminate(30.seconds).futureValue
129128
Http()
130129
}

project/Dependencies.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,39 @@ object Dependencies {
1010
val Logback = "1.5.17"
1111
val Pekko = "1.1.3"
1212
val PekkoHttp = "1.1.0"
13-
val Prometheus = "0.16.0"
13+
val Prometheus = "1.3.6"
1414
val ScalaCollectionCompat = "2.13.0"
1515
val ScalaLogging = "3.9.5"
1616
val ScalaMock = "6.2.0"
1717
val ScalaTest = "3.2.19"
1818
}
1919

20-
val Datadog = "com.datadoghq" % "java-dogstatsd-client" % Versions.Datadog
21-
val DropwizardCore = "io.dropwizard.metrics" % "metrics-core" % Versions.Dropwizard
22-
val DropwizardJson = "io.dropwizard.metrics" % "metrics-json" % Versions.Dropwizard
23-
val DropwizardV5Core = "io.dropwizard.metrics5" % "metrics-core" % Versions.DropwizardV5
24-
val DropwizardV5Json = "io.dropwizard.metrics5" % "metrics-json" % Versions.DropwizardV5
25-
val Enumeratum = "com.beachape" %% "enumeratum" % Versions.Enumeratum
26-
val PekkoHttp = "org.apache.pekko" %% "pekko-http" % Versions.PekkoHttp
27-
val PrometheusCommon = "io.prometheus" % "simpleclient_common" % Versions.Prometheus
28-
val ScalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % Versions.ScalaLogging
20+
val Datadog = "com.datadoghq" % "java-dogstatsd-client" % Versions.Datadog
21+
val DropwizardCore = "io.dropwizard.metrics" % "metrics-core" % Versions.Dropwizard
22+
val DropwizardJson = "io.dropwizard.metrics" % "metrics-json" % Versions.Dropwizard
23+
val DropwizardV5Core = "io.dropwizard.metrics5" % "metrics-core" % Versions.DropwizardV5
24+
val DropwizardV5Json = "io.dropwizard.metrics5" % "metrics-json" % Versions.DropwizardV5
25+
val Enumeratum = "com.beachape" %% "enumeratum" % Versions.Enumeratum
26+
val PekkoHttp = "org.apache.pekko" %% "pekko-http" % Versions.PekkoHttp
27+
val PrometheusCore = "io.prometheus" % "prometheus-metrics-core" % Versions.Prometheus
28+
val PrometheusExpositionFormats =
29+
"io.prometheus" % "prometheus-metrics-exposition-formats" % Versions.Prometheus
30+
val ScalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % Versions.ScalaLogging
2931

3032
object Provided {
3133
val PekkoStream = "org.apache.pekko" %% "pekko-stream" % Versions.Pekko % "provided"
3234
}
3335

3436
object Test {
35-
val DropwizardJvm = "io.dropwizard.metrics" % "metrics-jvm" % Versions.Dropwizard % "test"
36-
val DropwizardV5Jvm = "io.dropwizard.metrics5" % "metrics-jvm" % Versions.DropwizardV5 % "test"
37-
val Logback = "ch.qos.logback" % "logback-classic" % Versions.Logback % "test"
38-
val PekkoHttpJson = "org.apache.pekko" %% "pekko-http-spray-json" % Versions.PekkoHttp % "test"
39-
val PekkoHttpTestkit = "org.apache.pekko" %% "pekko-http-testkit" % Versions.PekkoHttp % "test"
40-
val PekkoSlf4j = "org.apache.pekko" %% "pekko-slf4j" % Versions.Pekko % "test"
41-
val PekkoStreamTestkit = "org.apache.pekko" %% "pekko-stream-testkit" % Versions.Pekko % "test"
42-
val PekkoTestkit = "org.apache.pekko" %% "pekko-testkit" % Versions.Pekko % "test"
43-
val PrometheusHotspot = "io.prometheus" % "simpleclient_hotspot" % Versions.Prometheus % "test"
37+
val DropwizardJvm = "io.dropwizard.metrics" % "metrics-jvm" % Versions.Dropwizard % "test"
38+
val DropwizardV5Jvm = "io.dropwizard.metrics5" % "metrics-jvm" % Versions.DropwizardV5 % "test"
39+
val Logback = "ch.qos.logback" % "logback-classic" % Versions.Logback % "test"
40+
val PekkoHttpJson = "org.apache.pekko" %% "pekko-http-spray-json" % Versions.PekkoHttp % "test"
41+
val PekkoHttpTestkit = "org.apache.pekko" %% "pekko-http-testkit" % Versions.PekkoHttp % "test"
42+
val PekkoSlf4j = "org.apache.pekko" %% "pekko-slf4j" % Versions.Pekko % "test"
43+
val PekkoStreamTestkit = "org.apache.pekko" %% "pekko-stream-testkit" % Versions.Pekko % "test"
44+
val PekkoTestkit = "org.apache.pekko" %% "pekko-testkit" % Versions.Pekko % "test"
45+
val PrometheusJvm = "io.prometheus" % "prometheus-metrics-instrumentation-jvm" % Versions.Prometheus % "test"
4446
val ScalaCollectionCompat =
4547
"org.scala-lang.modules" %% "scala-collection-compat" % Versions.ScalaCollectionCompat % "test"
4648
val ScalaMock = "org.scalamock" %% "scalamock" % Versions.ScalaMock % "test"

prometheus/src/main/scala/fr/davit/pekko/http/metrics/prometheus/PrometheusConverters.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ package fr.davit.pekko.http.metrics.prometheus
1818

1919
trait PrometheusConverters {
2020

21-
implicit def convertCounter(counter: io.prometheus.client.Counter): PrometheusCounter =
21+
implicit def convertCounter(counter: io.prometheus.metrics.core.metrics.Counter): PrometheusCounter =
2222
new PrometheusCounter(counter)
2323

24-
implicit def convertGauge(gauge: io.prometheus.client.Gauge): PrometheusGauge =
24+
implicit def convertGauge(gauge: io.prometheus.metrics.core.metrics.Gauge): PrometheusGauge =
2525
new PrometheusGauge(gauge)
2626

27-
implicit def convertSummaryTimer(summary: io.prometheus.client.Summary): PrometheusSummaryTimer =
27+
implicit def convertSummaryTimer(summary: io.prometheus.metrics.core.metrics.Summary): PrometheusSummaryTimer =
2828
new PrometheusSummaryTimer(summary)
2929

30-
implicit def convertHistogramTimer(histogram: io.prometheus.client.Histogram): PrometheusHistogramTimer =
30+
implicit def convertHistogramTimer(
31+
histogram: io.prometheus.metrics.core.metrics.Histogram
32+
): PrometheusHistogramTimer =
3133
new PrometheusHistogramTimer(histogram)
3234

33-
implicit def convertSummary(summary: io.prometheus.client.Summary): PrometheusSummary =
35+
implicit def convertSummary(summary: io.prometheus.metrics.core.metrics.Summary): PrometheusSummary =
3436
new PrometheusSummary(summary)
3537

36-
implicit def convertHistogram(histogram: io.prometheus.client.Histogram): PrometheusHistogram =
38+
implicit def convertHistogram(histogram: io.prometheus.metrics.core.metrics.Histogram): PrometheusHistogram =
3739
new PrometheusHistogram(histogram)
3840

3941
}

prometheus/src/main/scala/fr/davit/pekko/http/metrics/prometheus/PrometheusMetrics.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,48 @@ import fr.davit.pekko.http.metrics.core._
2020

2121
import scala.concurrent.duration.FiniteDuration
2222

23-
class PrometheusCounter(counter: io.prometheus.client.Counter) extends Counter {
23+
class PrometheusCounter(counter: io.prometheus.metrics.core.metrics.Counter) extends Counter {
2424

2525
override def inc(dimensions: Seq[Dimension]): Unit = {
26-
counter.labels(dimensions.map(_.label): _*).inc()
26+
counter.labelValues(dimensions.map(_.label): _*).inc()
2727
}
2828
}
2929

30-
class PrometheusGauge(gauge: io.prometheus.client.Gauge) extends Gauge {
30+
class PrometheusGauge(gauge: io.prometheus.metrics.core.metrics.Gauge) extends Gauge {
3131

3232
override def inc(dimensions: Seq[Dimension] = Seq.empty): Unit = {
33-
gauge.labels(dimensions.map(_.label): _*).inc()
33+
gauge.labelValues(dimensions.map(_.label): _*).inc()
3434
}
3535

3636
override def dec(dimensions: Seq[Dimension] = Seq.empty): Unit = {
37-
gauge.labels(dimensions.map(_.label): _*).dec()
37+
gauge.labelValues(dimensions.map(_.label): _*).dec()
3838
}
3939
}
4040

41-
class PrometheusSummaryTimer(summary: io.prometheus.client.Summary) extends Timer {
41+
class PrometheusSummaryTimer(summary: io.prometheus.metrics.core.metrics.Summary) extends Timer {
4242

4343
override def observe(duration: FiniteDuration, dimensions: Seq[Dimension] = Seq.empty): Unit = {
44-
summary.labels(dimensions.map(_.label): _*).observe(duration.toMillis.toDouble / 1000.0)
44+
summary.labelValues(dimensions.map(_.label): _*).observe(duration.toMillis.toDouble / 1000.0)
4545
}
4646
}
4747

48-
class PrometheusHistogramTimer(summary: io.prometheus.client.Histogram) extends Timer {
48+
class PrometheusHistogramTimer(summary: io.prometheus.metrics.core.metrics.Histogram) extends Timer {
4949

5050
override def observe(duration: FiniteDuration, dimensions: Seq[Dimension] = Seq.empty): Unit = {
51-
summary.labels(dimensions.map(_.label): _*).observe(duration.toMillis.toDouble / 1000.0)
51+
summary.labelValues(dimensions.map(_.label): _*).observe(duration.toMillis.toDouble / 1000.0)
5252
}
5353
}
5454

55-
class PrometheusSummary(summary: io.prometheus.client.Summary) extends Histogram {
55+
class PrometheusSummary(summary: io.prometheus.metrics.core.metrics.Summary) extends Histogram {
5656

5757
override def update[T](value: T, dimensions: Seq[Dimension] = Seq.empty)(implicit numeric: Numeric[T]): Unit = {
58-
summary.labels(dimensions.map(_.label): _*).observe(numeric.toDouble(value))
58+
summary.labelValues(dimensions.map(_.label): _*).observe(numeric.toDouble(value))
5959
}
6060
}
6161

62-
class PrometheusHistogram(histogram: io.prometheus.client.Histogram) extends Histogram {
62+
class PrometheusHistogram(histogram: io.prometheus.metrics.core.metrics.Histogram) extends Histogram {
6363

6464
override def update[T](value: T, dimensions: Seq[Dimension] = Seq.empty)(implicit numeric: Numeric[T]): Unit = {
65-
histogram.labels(dimensions.map(_.label): _*).observe(numeric.toDouble(value))
65+
histogram.labelValues(dimensions.map(_.label): _*).observe(numeric.toDouble(value))
6666
}
6767
}

0 commit comments

Comments
 (0)