Skip to content

Commit

Permalink
Merge pull request #853 from bio-aeon/sdk-exporter/prometheus-exporte…
Browse files Browse the repository at this point in the history
…r-accept
  • Loading branch information
iRevive authored Nov 23, 2024
2 parents e2c4ee7 + ace8860 commit 6176527
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ object PrometheusHttpRoutes {

val routes: HttpRoutes[F] = HttpRoutes.of {
case Request(GET, _, _, headers, _, _) =>
if (headers.get[Accept].forall(_.values.exists(_.mediaRange.satisfies(MediaRange.`text/*`)))) {
if (
headers
.get[Accept]
.forall(
_.values
.exists(v => v.mediaRange.equals(MediaRange.`*/*`) || v.mediaRange.satisfies(MediaRange.`text/*`))
)
) {
for {
metrics <- exporter.metricReader.collectAllMetrics
} yield Response().withEntity(writer.write(metrics)).withHeaders("Content-Type" -> writer.contentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ class PrometheusHttpRoutesSuite extends CatsEffectSuite with SuiteRuntimePlatfor
}
}

test("respond with a text on GET request and wildcard accept") {
PrometheusMetricExporter.builder[IO].build.flatMap { exporter =>
val routes: HttpRoutes[IO] = PrometheusHttpRoutes.routes(exporter, PrometheusWriter.Config.default)

routes.orNotFound
.run(
Request(method = Method.GET, uri = uri"/", headers = Headers(Accept(MediaRange.`*/*`)))
)
.flatMap { response =>
IO {
assertEquals(response.status, Status.Ok)
assertEquals(response.contentType.exists(_.mediaType.isText), true)
}
}
}
}

test("respond with an empty body on HEAD request and wildcard accept") {
PrometheusMetricExporter.builder[IO].build.flatMap { exporter =>
val routes: HttpRoutes[IO] = PrometheusHttpRoutes.routes(exporter, PrometheusWriter.Config.default)
Expand Down

0 comments on commit 6176527

Please sign in to comment.