-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
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
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
30 changes: 29 additions & 1 deletion
30
schemer-registry/src/main/scala/schemer/registry/routes/HealthRoutes.scala
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
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())) | ||
} | ||
} | ||
} | ||
} |