Skip to content

Commit

Permalink
Expose metrics in prometheus format
Browse files Browse the repository at this point in the history
  • Loading branch information
manojlds committed Feb 16, 2018
1 parent 1c0a385 commit bc76b7a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ lazy val registry = (project in file("schemer-registry"))
quill,
quillAsyncPostgres,
flyway,
prometheusClient,
prometheusClientCommon,
prometheusHotspot,
scalaTest
),
excludeDependencies ++= Seq(
Expand Down
4 changes: 4 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ object Dependencies {
ExclusionRule("javax.mail")
}

lazy val prometheusClient = "io.prometheus" % "simpleclient" % "0.2.0"
lazy val prometheusClientCommon = "io.prometheus" % "simpleclient_common" % "0.2.0"
lazy val prometheusHotspot = "io.prometheus" % "simpleclient_hotspot" % "0.2.0"

lazy val akkaHttpCore = "com.typesafe.akka" %% "akka-http-core" % Versions.akkaHttpVersion
lazy val akkaHttp = "com.typesafe.akka" %% "akka-http" % Versions.akkaHttpVersion
lazy val sprayJsonAkka = "com.typesafe.akka" %% "akka-http-spray-json" % Versions.akkaHttpVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
package schemer.registry.routes

import akka.http.scaladsl.server.Directives.{complete, get, path}
import java.io.{StringWriter, Writer}
import java.util

import akka.http.scaladsl.model.{HttpCharsets, HttpEntity, MediaType}
import akka.http.scaladsl.server.Directives._
import io.prometheus.client.Collector.MetricFamilySamples
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.exporter.common.TextFormat
import io.prometheus.client.hotspot.{DefaultExports, StandardExports}

trait HealthRoutes {

DefaultExports.initialize()
val collectorRegistry = CollectorRegistry.defaultRegistry
private val mediaTypeParams = Map("version" -> "0.0.4")
private val mediaType =
MediaType.customWithFixedCharset("text", "plain", HttpCharsets.`UTF-8`, params = mediaTypeParams)

def toPrometheusTextFormat(e: util.Enumeration[MetricFamilySamples]): String = {
val writer: Writer = new StringWriter()
TextFormat.write004(writer, e)

writer.toString
}

val healthRoutes = path("health") {
get {
complete {
"OK"
}
}
} ~ path("metrics") {
get {
complete {
HttpEntity(mediaType, toPrometheusTextFormat(collectorRegistry.metricFamilySamples()))
}
}
}
}

0 comments on commit bc76b7a

Please sign in to comment.