Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose prometheus metrics strictly on '/metrics'-endpoint #907

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
}