Skip to content

Commit

Permalink
handle HttpEntity.Default in the Akka HTTP instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantopo committed Nov 30, 2021
1 parent 558e837 commit d8a5791
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,14 @@ object ServerFlowWrapper {
requestSpan.mark("http.response.ready")

responseWithContext.entity match {
case strict@HttpEntity.Strict(_, bs) =>
case strict @ HttpEntity.Strict(_, bs) =>
requestHandler.responseSent(bs.size)
strict

case default: HttpEntity.Default =>
requestHandler.responseSent(default.contentLength)
default

case _ =>
val responseSizeCounter = new AtomicLong(0L)
responseWithContext.entity.transformDataBytes(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package kamon.akka.http

import akka.actor.ActorSystem
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, StatusCodes}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpRequest, HttpResponse, StatusCodes}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import akka.util.ByteString
import kamon.instrumentation.akka.http.ServerFlowWrapper
import kamon.testkit.InitAndStopKamonAfterAll
import org.scalatest.concurrent.ScalaFutures
Expand All @@ -20,6 +21,13 @@ class ServerFlowWrapperSpec extends AnyWordSpecLike with Matchers with ScalaFutu
HttpResponse(status = StatusCodes.OK, entity = HttpEntity("OK"))
}

private val defaultReturningFlow = Flow[HttpRequest].map { _ =>
HttpResponse(status = StatusCodes.OK, entity = HttpEntity.Default(
ContentTypes.`text/plain(UTF-8)`,
2,
Source.single(ByteString.apply("OK"))))
}

"the server flow wrapper" should {
"keep strict entities strict" in {
val flow = ServerFlowWrapper(okReturningFlow, "localhost", 8080)
Expand All @@ -28,9 +36,23 @@ class ServerFlowWrapperSpec extends AnyWordSpecLike with Matchers with ScalaFutu
.via(flow)
.runWith(Sink.head)
.futureValue

response.entity should matchPattern {
case HttpEntity.Strict(_, _) =>
}
}

"keep default entities default" in {
val flow = ServerFlowWrapper(defaultReturningFlow, "localhost", 8081)
val request = HttpRequest()
val response = Source.single(request)
.via(flow)
.runWith(Sink.head)
.futureValue

response.entity should matchPattern {
case _: HttpEntity.Default =>
}
}
}
}

0 comments on commit d8a5791

Please sign in to comment.