Last active
March 17, 2021 19:39
-
-
Save notxcain/4a9a07cc87c2a6c6ad6f4e9b3cff0be6 to your computer and use it in GitHub Desktop.
Akka Http Prometheus Endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.prometheus.client.CollectorRegistry | |
import io.prometheus.client.dropwizard.DropwizardExports | |
import io.prometheus.client.hotspot.DefaultExports | |
import nl.grons.metrics.scala.DefaultInstrumented | |
import akka.actor.ActorSystem | |
import akka.stream.ActorMaterializer | |
import akka.http.scaladsl.Http | |
object App extends App with DefaultInstrumented { | |
// Add JVM metrics exporters to default registry | |
DefaultExports.initialize() | |
// Add Dropwizard adapter exporter | |
CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry)) | |
val prometheusEndpoint = PrometheusEndpoint(CollectorRegistry.defaultRegistry) | |
implicit val system = ActorSystem("system") | |
implicit val materializer = ActorMaterializer() | |
Http().bindAndHandle(prometheusEndpoint, "0.0.0.0", 9000) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy val akkaHttpVersion = "10.0.3" | |
lazy val metricsScalaVersion = "3.5.6" | |
lazy val prometheusVersion = "0.0.26" | |
libraryDependencies ++= Seq( | |
// Akka Http | |
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion, | |
// Scala API for dropwizard metrics | |
"nl.grons" %% "metrics-scala" % metricsScalaVersion, | |
// Provides exporters for HotSpot JVM metrics from MBeans | |
"io.prometheus" % "simpleclient_hotspot" % prometheusVersion, | |
// Provides exporter that works as an adapter between Dropwizard and Prometheus registries | |
"io.prometheus" % "simpleclient_dropwizard" % prometheusVersion, | |
// Common stuff that would help us with encoding response in Prometheus format. | |
"io.prometheus" % "simpleclient_common" % prometheusVersion | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.StringWriter | |
import akka.http.scaladsl.model._ | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.Route | |
import io.prometheus.client.CollectorRegistry | |
import io.prometheus.client.exporter.common.TextFormat | |
import scala.collection.JavaConverters._ | |
object PrometheusEndpoint { | |
// Custom Prometheus content type. See https://prometheus.io/docs/instrumenting/exposition_formats | |
private val `text/plain; version=0.0.4; charset=utf-8` = ContentType { | |
MediaType.customWithFixedCharset( | |
"text", | |
"plain", | |
HttpCharsets.`UTF-8`, | |
params = Map("version" -> "0.0.4") | |
) | |
} | |
private def renderMetrics(registry: CollectorRegistry, names: Set[String]): String = { | |
val writer = new StringWriter() | |
TextFormat.write004(writer, registry.filteredMetricFamilySamples(names.toSet.asJava)) | |
writer.toString | |
} | |
def apply(registry: CollectorRegistry): Route = | |
// We gonna serve at `/metrics/prometheus` and use `name` parameter as filter if provided by scraper | |
(get & path("metrics" / "prometheus") & parameter('name.*)) { names => | |
val content = renderMetrics(registry, names.toSet) | |
complete { | |
HttpResponse(entity = HttpEntity(`text/plain; version=0.0.4; charset=utf-8`, content)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment