Skip to content

Commit

Permalink
Expose prometheus metrics strictly on '/metrics'-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippus committed Nov 24, 2020
1 parent bcfc41e commit ddc9ebd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ class SunEmbeddedHttpServer(hostname: String, port: Int, scrapeSource: ScrapeSou
s.setExecutor(null)
val handler = new HttpHandler {
override def handle(httpExchange: HttpExchange): Unit = {
val data = scrapeSource.scrapeData()
val bytes = data.getBytes(StandardCharsets.UTF_8)
httpExchange.sendResponseHeaders(200, bytes.length)
val os = httpExchange.getResponseBody
try {
os.write(bytes)
if (httpExchange.getRequestURI.getPath == "/metrics") {
val data = scrapeSource.scrapeData()
val bytes = data.getBytes(StandardCharsets.UTF_8)
httpExchange.sendResponseHeaders(200, bytes.length)
val os = httpExchange.getResponseBody
try {
os.write(bytes)
}
finally
os.close()
}
else {
httpExchange.sendResponseHeaders(404, -1)
}
finally
os.close()
}

}
s.createContext("/metrics", handler)
s.createContext("/", handler)
s.start()
s
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kamon.prometheus

import java.io.FileNotFoundException
import java.net.URL

import com.typesafe.config.{Config, ConfigFactory}
Expand Down Expand Up @@ -32,7 +33,7 @@ abstract class EmbeddedHttpServerSpecSuite extends WordSpec with Matchers with B
"the embedded sun http server" should {
"provide no data comment on GET to /metrics when no data loaded yet" in {
//act
val metrics = httpGetMetrics()
val metrics = httpGetMetrics("/metrics")
//assert
metrics shouldBe "# The kamon-prometheus module didn't receive any data just yet.\n"
}
Expand All @@ -41,7 +42,7 @@ abstract class EmbeddedHttpServerSpecSuite extends WordSpec with Matchers with B
//arrange
testee.reportPeriodSnapshot(emptyPeriodSnapshot)
//act
val metrics = httpGetMetrics()
val metrics = httpGetMetrics("/metrics")
//assert
metrics shouldBe ""
}
Expand All @@ -50,7 +51,7 @@ abstract class EmbeddedHttpServerSpecSuite extends WordSpec with Matchers with B
//arrange
testee.reportPeriodSnapshot(counter("jvm.mem"))
//act
val metrics = httpGetMetrics()
val metrics = httpGetMetrics("/metrics")
//assert
metrics shouldBe "# TYPE jvm_mem_total counter\njvm_mem_total 1.0\n"
}
Expand All @@ -60,18 +61,26 @@ abstract class EmbeddedHttpServerSpecSuite extends WordSpec with Matchers with B
testee.reconfigure(testConfig)
testee.reportPeriodSnapshot(counter("jvm.mem"))
//act
val metrics = httpGetMetrics()
val metrics = httpGetMetrics("/metrics")
//assert
metrics shouldBe "# TYPE jvm_mem_total counter\njvm_mem_total 2.0\n"
}
}

"provide the metrics strictly on /metrics endpoint" in {
assertThrows[FileNotFoundException] {
httpGetMetrics("")
}
assertThrows[FileNotFoundException] {
httpGetMetrics("/metricsss")
}
}
}

private def httpGetMetrics(): String = {
val src = scala.io.Source.fromURL(new URL(s"http://127.0.0.1:$port/metrics"))
private def httpGetMetrics(endpoint: String): String = {
val src = scala.io.Source.fromURL(new URL(s"http://127.0.0.1:$port$endpoint"))
try
src.mkString
finally
src.close()
}
}
}

0 comments on commit ddc9ebd

Please sign in to comment.