Skip to content

Commit 28cdeac

Browse files
authored
Merge pull request #934 from SimunKaracic/akka-http-upgrade
Akka http upgrade
2 parents dd8713f + eb1289d commit 28cdeac

File tree

8 files changed

+64
-24
lines changed

8 files changed

+64
-24
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
- run: git fetch --depth=1 origin '+refs/tags/*:refs/tags/*'
1212
- uses: olafurpg/setup-scala@v10
1313
with:
14-
java-version: [email protected]242
14+
java-version: [email protected]252
1515
- name: Test
1616
run: csbt +test

build.sbt

+11-5
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ lazy val `kamon-akka` = (project in file("instrumentation/kamon-akka"))
392392
)
393393

394394

395+
def akkaHttpVersion(scalaVersion: String) = scalaVersion match {
396+
case "2.11" => "10.1.12"
397+
case _ => "10.2.3"
398+
}
399+
400+
395401
lazy val `kamon-akka-http` = (project in file("instrumentation/kamon-akka-http"))
396402
.enablePlugins(JavaAgent)
397403
.disablePlugins(AssemblyPlugin)
@@ -401,18 +407,18 @@ lazy val `kamon-akka-http` = (project in file("instrumentation/kamon-akka-http")
401407
javaAgents += "org.mortbay.jetty.alpn" % "jetty-alpn-agent" % "2.0.10" % "test",
402408
libraryDependencies ++= Seq(
403409
kanelaAgent % "provided",
404-
"com.typesafe.akka" %% "akka-http" % "10.1.12" % "provided",
405-
"com.typesafe.akka" %% "akka-http2-support" % "10.1.12" % "provided",
406-
"com.typesafe.akka" %% "akka-stream" % "2.5.31" % "provided",
410+
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion(scalaBinaryVersion.value) % "provided",
411+
"com.typesafe.akka" %% "akka-http2-support" % akkaHttpVersion(scalaBinaryVersion.value) % "provided",
412+
"com.typesafe.akka" %% "akka-stream" % "2.5.32" % "provided",
407413

408414
scalatest % "test",
409415
slf4jApi % "test",
410416
slf4jnop % "test",
411417
okHttp % "test",
412-
"com.typesafe.akka" %% "akka-http-testkit" % "10.1.12" % "test",
418+
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion(scalaBinaryVersion.value) % "test",
413419
"de.heikoseeberger" %% "akka-http-json4s" % "1.27.0" % "test",
414420
"org.json4s" %% "json4s-native" % "3.6.7" % "test",
415-
)
421+
),
416422
)).dependsOn(`kamon-akka`, `kamon-testkit` % "test")
417423

418424

instrumentation/kamon-akka-http/src/main/scala-2.11/kamon/instrumentation/akka/http/AkkaHttpServerInstrumentation.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,4 @@ object Http2BlueprintInterceptor {
353353
zuper.call()
354354
}
355355
}
356-
}
356+
}

instrumentation/kamon-akka-http/src/main/scala-2.12/kamon/instrumentation/akka/http/AkkaHttpServerInstrumentation.scala

+15-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class AkkaHttpServerInstrumentation extends InstrumentationBuilder {
5858
* operation name before the request processing hits the routing tree, we are delaying the sampling decision to the
5959
* point at which we have some operation name.
6060
*/
61+
6162
onType("akka.http.scaladsl.HttpExt")
6263
.advise(method("bindAndHandle"), classOf[HttpExtBindAndHandleAdvice])
6364

@@ -66,7 +67,8 @@ class AkkaHttpServerInstrumentation extends InstrumentationBuilder {
6667
* creation happen at different times we are wrapping the handler with the interface/port data and reading that
6768
* information when turning the handler function into a flow and wrapping it the same way we would for HTTP/1.
6869
*/
69-
onType("akka.http.scaladsl.Http2Ext")
70+
71+
onType("akka.http.impl.engine.http2.Http2Ext")
7072
.advise(method("bindAndHandleAsync") and isPublic(), classOf[Http2ExtBindAndHandleAdvice])
7173

7274
onType("akka.http.impl.engine.http2.Http2Blueprint$")
@@ -102,12 +104,20 @@ class AkkaHttpServerInstrumentation extends InstrumentationBuilder {
102104
*/
103105
onTypes("akka.http.scaladsl.util.FastFuture$FulfilledFuture", "akka.http.scaladsl.util.FastFuture$ErrorFuture")
104106
.mixin(classOf[HasContext.MixinWithInitializer])
105-
.advise(method("transform"), InvokeWithCapturedContext)
106-
.advise(method("transformWith"), InvokeWithCapturedContext)
107-
.advise(method("onComplete"), InvokeWithCapturedContext)
107+
.advise(method("transform"), InvokeWithCapturedContext)
108+
.advise(method("transformWith"), InvokeWithCapturedContext)
109+
.advise(method("onComplete"), InvokeWithCapturedContext)
108110

109111
onType("akka.http.scaladsl.util.FastFuture$")
110112
.intercept(method("transformWith$extension1"), FastFutureTransformWithAdvice)
113+
114+
115+
/**
116+
* Akka-http 10.1.x compatibility.
117+
*/
118+
119+
onType("akka.http.scaladsl.Http2Ext")
120+
.advise(method("bindAndHandleAsync") and isPublic(), classOf[Http2ExtBindAndHandleAdvice])
111121
}
112122

113123
trait HasMatchingContext {
@@ -369,4 +379,4 @@ object Http2BlueprintInterceptor {
369379
zuper.call()
370380
}
371381
}
372-
}
382+
}

instrumentation/kamon-akka-http/src/main/scala-2.13/kamon/instrumentation/akka/http/AkkaHttpServerInstrumentation.scala

+10-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import scala.collection.immutable
3232

3333

3434
class AkkaHttpServerInstrumentation extends InstrumentationBuilder {
35-
3635
/**
3736
* When instrumenting bindAndHandle what we do is wrap the Flow[HttpRequest, HttpResponse, NotUsed] provided by
3837
* the user and add all the processing there. This is the part of the instrumentation that performs Context
@@ -49,8 +48,9 @@ class AkkaHttpServerInstrumentation extends InstrumentationBuilder {
4948
* For the HTTP/2 instrumentation, since the parts where we can capture the interface/port and the actual flow
5049
* creation happen at different times we are wrapping the handler with the interface/port data and reading that
5150
* information when turning the handler function into a flow and wrapping it the same way we would for HTTP/1.
51+
*
5252
*/
53-
onType("akka.http.scaladsl.Http2Ext")
53+
onType("akka.http.impl.engine.http2.Http2Ext")
5454
.advise(method("bindAndHandleAsync") and isPublic(), classOf[Http2ExtBindAndHandleAdvice])
5555

5656
onType("akka.http.impl.engine.http2.Http2Blueprint$")
@@ -92,6 +92,13 @@ class AkkaHttpServerInstrumentation extends InstrumentationBuilder {
9292

9393
onType("akka.http.scaladsl.util.FastFuture$")
9494
.intercept(method("transformWith$extension").and(takesArguments(4)), FastFutureTransformWithAdvice)
95+
96+
/**
97+
* Akka-http 10.1.x compatibility.
98+
*/
99+
100+
onType("akka.http.scaladsl.Http2Ext")
101+
.advise(method("bindAndHandleAsync") and isPublic(), classOf[Http2ExtBindAndHandleAdvice])
95102
}
96103

97104
trait HasMatchingContext {
@@ -359,4 +366,4 @@ object Http2BlueprintInterceptor {
359366
zuper.call()
360367
}
361368
}
362-
}
369+
}

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,22 @@ import kanela.agent.api.instrumentation.InstrumentationBuilder
2727
import scala.concurrent.Future
2828
import scala.util.{Failure, Success}
2929

30-
class AkkaHttpClientInstrumentation extends InstrumentationBuilder {
30+
class AkkaHttpClientInstrumentation extends InstrumentationBuilder with VersionFiltering {
3131

3232
/**
3333
* Simply modifies the requests as they are submitted. This does not cover connection pooling, just requests sent
3434
* via the Http.singleRequest mechanism.
3535
*/
36-
onType("akka.http.scaladsl.HttpExt")
37-
.advise(method("singleRequestImpl"), classOf[HttpExtSingleRequestAdvice])
36+
37+
onAkkaHttp("10.1") {
38+
onType("akka.http.scaladsl.HttpExt")
39+
.advise(method("singleRequestImpl"), classOf[HttpExtSingleRequestAdvice])
40+
}
41+
42+
onAkkaHttp("10.2") {
43+
onType("akka.http.scaladsl.HttpExt")
44+
.advise(method("singleRequest"), classOf[HttpExtSingleRequestAdvice])
45+
}
3846

3947
onType("akka.http.impl.engine.client.PoolMaster")
4048
.advise(method("dispatchRequest"), classOf[PoolMasterDispatchRequestAdvice])
@@ -58,4 +66,4 @@ object AkkaHttpClientInstrumentation {
5866

5967
responseFuture
6068
}
61-
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package kamon.instrumentation.akka.http
2+
3+
trait VersionFiltering {
4+
def onAkkaHttp(version: String)(block: => Unit): Unit = {
5+
if(akka.http.Version.current.startsWith(version))
6+
block
7+
}
8+
}

instrumentation/kamon-instrumentation-common/src/main/scala/kamon/instrumentation/http/HttpOperationNameGenerator.scala

+6-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ object HttpOperationNameGenerator {
3737
* Uses the request Host to assign a name.
3838
*/
3939
object Hostname extends HttpOperationNameGenerator {
40-
override def name(request: Request): Option[String] =
41-
Option(request.host)
40+
override def name(request: Request): Option[String] = {
41+
Option(request.host).filter(_.nonEmpty) orElse request.read("host").map(_.takeWhile(_ != ':'))
42+
}
4243
}
43-
44-
/**
44+
45+
/**
4546
* Uses the request Host and Port to assign a name.
4647
*/
4748
object HostnameAndPort extends HttpOperationNameGenerator {
@@ -61,7 +62,7 @@ object HttpOperationNameGenerator {
6162
/**
6263
* Uses a static name.
6364
*/
64-
class Static(name:String) extends HttpOperationNameGenerator {
65+
class Static(name: String) extends HttpOperationNameGenerator {
6566
override def name(request: Request): Option[String] =
6667
Option(name)
6768
}

0 commit comments

Comments
 (0)