Skip to content

expose text Content-Type in embedded http server #1386

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -41,6 +41,7 @@ class SunEmbeddedHttpServer(hostname: String, port: Int, path: String, scrapeSou
val bytes = data.getBytes(StandardCharsets.UTF_8)
var os: OutputStream = null
try {
httpExchange.getResponseHeaders.set("Content-Type", "text/plain; charset=UTF-8")
if (shouldUseCompression(httpExchange)) {
httpExchange.getResponseHeaders.set("Content-Encoding", "gzip")
httpExchange.sendResponseHeaders(200, 0)
Expand All @@ -51,7 +52,7 @@ class SunEmbeddedHttpServer(hostname: String, port: Int, path: String, scrapeSou
httpExchange.sendResponseHeaders(200, bytes.length)
os.write(bytes)
}
} finally Option(os).map(_.close())
} finally Option(os).foreach(_.close())
} else httpExchange.sendResponseHeaders(404, -1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import java.io.FileNotFoundException
import java.net.URL
import java.net.{URL, URLConnection}
import java.util.zip.GZIPInputStream
import scala.jdk.CollectionConverters._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import does not work with earlier Scala versions, but I'm about to merge removing support for Scala 2.11 and 2.12. Probably there is no need to change this but just wait until #1387 gets merged


class SunHttpServerSpecSuite extends EmbeddedHttpServerSpecSuite {
override def testConfig: Config = ConfigFactory.load()
Expand All @@ -32,7 +33,7 @@ abstract class EmbeddedHttpServerSpecSuite extends AnyWordSpec
"the embedded sun http server" should {
"provide no data comment on GET to /metrics when no data loaded yet" in {
// act
val metrics = httpGetMetrics("/metrics")
val metrics = httpGetMetrics("/metrics").content
// 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 AnyWordSpec
// arrange
testee.reportPeriodSnapshot(emptyPeriodSnapshot)
// act
val metrics = httpGetMetrics("/metrics")
val metrics = httpGetMetrics("/metrics").content
// assert
metrics shouldBe ""
}
Expand All @@ -50,7 +51,7 @@ abstract class EmbeddedHttpServerSpecSuite extends AnyWordSpec
// arrange
testee.reportPeriodSnapshot(counter("jvm.mem"))
// act
val metrics = httpGetMetrics("/metrics")
val metrics = httpGetMetrics("/metrics").content
// assert
metrics shouldBe "# TYPE jvm_mem_total counter\njvm_mem_total 1.0\n"
}
Expand All @@ -60,8 +61,9 @@ abstract class EmbeddedHttpServerSpecSuite extends AnyWordSpec
testee.reconfigure(testConfig)
testee.reportPeriodSnapshot(counter("jvm.mem"))
// act
val metrics = httpGetMetrics("/metrics")
val metrics = httpGetMetrics("/metrics").content
// assert
println(metrics)
metrics shouldBe "# TYPE jvm_mem_total counter\njvm_mem_total 2.0\n"
}

Expand All @@ -70,42 +72,58 @@ abstract class EmbeddedHttpServerSpecSuite extends AnyWordSpec
testee.reportPeriodSnapshot(counter("jvm.mem"))
// act
val metrics = httpGetMetrics("/metrics")
val gzippedMetrics = httpGetGzippedMetrics("/metrics")
val gzippedMetrics = httpGetMetrics("/metrics", useGzipEncoding = true)
// assert
metrics.contentLength should be > gzippedMetrics.contentLength
}

"property set Content-Type" in {
// arrange
testee.reportPeriodSnapshot(counter("jvm.mem"))
// act
val metrics = httpGetMetrics("/metrics")
val gzippedMetrics = httpGetMetrics("/metrics", useGzipEncoding = true)
// assert
metrics.length should be > gzippedMetrics.length
metrics.headers("Content-type").head shouldBe "text/plain; charset=UTF-8"
gzippedMetrics.headers("Content-type").head shouldBe "text/plain; charset=UTF-8"
}

"respect the path configuration" in {
httpGetMetrics("/metrics") should not be empty
httpGetMetrics("/metrics").content should not be empty
assertThrows[FileNotFoundException] {
httpGetMetrics("/new-metrics")
}

testee.reconfigure(changeEndpoint("/new-metrics"))
httpGetMetrics("/new-metrics") should not be empty
httpGetMetrics("/new-metrics").content should not be empty

assertThrows[FileNotFoundException] {
httpGetMetrics("/metrics")
httpGetMetrics("/metrics").content
}
}
}

private def httpGetMetrics(endpoint: String): String = {
val url = new URL(s"http://127.0.0.1:$port$endpoint")
val src = scala.io.Source.fromURL(url)
try src.mkString
finally src.close()
}
private case class Result(content: String, headers: Map[String, List[String]], contentLength: Int)

private def httpGetGzippedMetrics(endpoint: String): String = {
private def httpGetMetrics(endpoint: String, useGzipEncoding: Boolean = false): Result = {
val url = new URL(s"http://127.0.0.1:$port$endpoint")
val connection = url.openConnection
connection.setRequestProperty("Accept-Encoding", "gzip")
val gzipStream = new GZIPInputStream(connection.getInputStream)
val src = scala.io.Source.fromInputStream(gzipStream)
connection.getRequestProperty("Accept-Encoding") shouldBe "gzip"
try src.getLines.mkString
finally gzipStream.close()
val stream = if (useGzipEncoding) {
connection.setRequestProperty("Accept-Encoding", "gzip")
new GZIPInputStream(connection.getInputStream)
} else connection.getInputStream
val src = scala.io.Source.fromInputStream(stream)
if (useGzipEncoding) {
connection.getRequestProperty("Accept-Encoding") shouldBe "gzip"
}
try {
val content = src.mkString
Result(
content,
connection.getHeaderFields.asScala.toMap.view.mapValues(_.asScala.toList).toMap,
connection.getContentLength
)
} finally stream.close()
}

private def changeEndpoint(path: String): Config = {
Expand Down