Skip to content

Commit d8a5791

Browse files
committed
handle HttpEntity.Default in the Akka HTTP instrumentation
1 parent 558e837 commit d8a5791

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

instrumentation/kamon-akka-http/src/main/scala/kamon/instrumentation/akka/http/ServerFlowWrapper.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ object ServerFlowWrapper {
137137
requestSpan.mark("http.response.ready")
138138

139139
responseWithContext.entity match {
140-
case strict@HttpEntity.Strict(_, bs) =>
140+
case strict @ HttpEntity.Strict(_, bs) =>
141141
requestHandler.responseSent(bs.size)
142142
strict
143+
144+
case default: HttpEntity.Default =>
145+
requestHandler.responseSent(default.contentLength)
146+
default
147+
143148
case _ =>
144149
val responseSizeCounter = new AtomicLong(0L)
145150
responseWithContext.entity.transformDataBytes(

instrumentation/kamon-akka-http/src/test/scala/kamon/akka/http/ServerFlowWrapperSpec.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package kamon.akka.http
22

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

24+
private val defaultReturningFlow = Flow[HttpRequest].map { _ =>
25+
HttpResponse(status = StatusCodes.OK, entity = HttpEntity.Default(
26+
ContentTypes.`text/plain(UTF-8)`,
27+
2,
28+
Source.single(ByteString.apply("OK"))))
29+
}
30+
2331
"the server flow wrapper" should {
2432
"keep strict entities strict" in {
2533
val flow = ServerFlowWrapper(okReturningFlow, "localhost", 8080)
@@ -28,9 +36,23 @@ class ServerFlowWrapperSpec extends AnyWordSpecLike with Matchers with ScalaFutu
2836
.via(flow)
2937
.runWith(Sink.head)
3038
.futureValue
39+
3140
response.entity should matchPattern {
3241
case HttpEntity.Strict(_, _) =>
3342
}
3443
}
44+
45+
"keep default entities default" in {
46+
val flow = ServerFlowWrapper(defaultReturningFlow, "localhost", 8081)
47+
val request = HttpRequest()
48+
val response = Source.single(request)
49+
.via(flow)
50+
.runWith(Sink.head)
51+
.futureValue
52+
53+
response.entity should matchPattern {
54+
case _: HttpEntity.Default =>
55+
}
56+
}
3557
}
3658
}

0 commit comments

Comments
 (0)