Skip to content

Commit

Permalink
fix okhttp + spring for scala 3. Add a kamon-bundle-3
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsimpson committed Dec 1, 2023
1 parent 6b689c2 commit 681c5d1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 20 deletions.
74 changes: 68 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ lazy val `kamon-logback` = (project in file("instrumentation/kamon-logback"))
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(
crossScalaVersions += scala_3_version,
libraryDependencies ++= Seq(
kanelaAgent % "provided",
logbackClassic % "provided",
Expand All @@ -289,25 +290,30 @@ lazy val `kamon-logback` = (project in file("instrumentation/kamon-logback"))
).dependsOn(`kamon-core`, `kamon-instrumentation-common`, `kamon-testkit` % "test")


def slickVersion(scalaVersion: String) = scalaVersion match {
case "3" => "3.5.0-M5"
case x => "3.3.2"
}
lazy val `kamon-jdbc` = (project in file("instrumentation/kamon-jdbc"))
.disablePlugins(AssemblyPlugin)
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(
crossScalaVersions += scala_3_version, //
libraryDependencies ++= Seq(
jsqlparser,
kanelaAgent % "provided",
"com.zaxxer" % "HikariCP" % "4.0.3" % "provided",
"org.mariadb.jdbc" % "mariadb-java-client" % "2.2.6" % "provided",
"com.typesafe.slick" %% "slick" % "3.3.2" % "provided",
"com.typesafe.slick" %% "slick" % slickVersion(scalaBinaryVersion.value) % "provided",
"org.postgresql" % "postgresql" % "42.2.5" % "provided",

scalatest % "test",
logbackClassic % "test",
"com.typesafe.slick" %% "slick-hikaricp" % "3.3.2" % "test",
"com.h2database" % "h2" % "1.4.182" % "test",
"com.typesafe.slick" %% "slick-hikaricp" % slickVersion(scalaBinaryVersion.value) % "test",
"com.h2database" % "h2" % "1.4.192" % "test",
"org.xerial" % "sqlite-jdbc" % "3.34.0" % "test",
"ch.vorburger.mariaDB4j" % "mariaDB4j" % "2.4.0" % "test"
"ch.vorburger.mariaDB4j" % "mariaDB4j" % "2.5.3" % "test"
)
).dependsOn(`kamon-core`, `kamon-executors`, `kamon-testkit` % "test")

Expand Down Expand Up @@ -368,22 +374,24 @@ lazy val `kamon-cassandra` = (project in file("instrumentation/kamon-cassandra")
.disablePlugins(AssemblyPlugin)
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(crossScalaVersions += scala_3_version)
.dependsOn(`kamon-core`, `kamon-instrumentation-common`, `kamon-testkit` % "test", `kamon-executors`)

lazy val `kamon-elasticsearch` = (project in file("instrumentation/kamon-elasticsearch"))
.disablePlugins(AssemblyPlugin)
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(
crossScalaVersions += scala_3_version,
Test / run / fork := true,
libraryDependencies ++= Seq(
kanelaAgent % "provided",
"org.elasticsearch.client" % "elasticsearch-rest-client" % "7.9.1" % "provided",
"org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "7.9.1" % "provided",
scalatest % "test",
logbackClassic % "test",
"com.dimafeng" %% "testcontainers-scala" % "0.39.3" % "test",
"com.dimafeng" %% "testcontainers-scala-elasticsearch" % "0.39.3" % "test"
"com.dimafeng" %% "testcontainers-scala" % "0.41.0" % "test",
"com.dimafeng" %% "testcontainers-scala-elasticsearch" % "0.41.0" % "test"
)
).dependsOn(`kamon-core`, `kamon-instrumentation-common`, `kamon-testkit` % "test")

Expand All @@ -392,6 +400,7 @@ lazy val `kamon-spring` = (project in file("instrumentation/kamon-spring"))
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(
crossScalaVersions += scala_3_version,
Test / run / fork := true,
libraryDependencies ++= Seq(
kanelaAgent % "provided",
Expand Down Expand Up @@ -616,6 +625,7 @@ lazy val `kamon-okhttp` = (project in file("instrumentation/kamon-okhttp"))
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(
crossScalaVersions += scala_3_version,
libraryDependencies ++= Seq(
kanelaAgent % "provided",
"com.squareup.okhttp3" % "okhttp" % "4.10.0" % "provided",
Expand Down Expand Up @@ -718,6 +728,7 @@ lazy val `kamon-aws-sdk` = (project in file("instrumentation/kamon-aws-sdk"))
.enablePlugins(JavaAgent)
.settings(instrumentationSettings)
.settings(
crossScalaVersions += `scala_3_version`,
libraryDependencies ++= Seq(
kanelaAgent % "provided",
"com.amazonaws" % "aws-java-sdk-lambda" % "1.12.225" % "provided",
Expand Down Expand Up @@ -1050,6 +1061,45 @@ lazy val `kamon-bundle-dependencies-2-12-and-up` = (project in file("bundle/kamo
`kamon-alpakka-kafka`
)

/**
* Add a reference here to all the project dependencies that can be built
* for 3
*/
lazy val `kamon-bundle-dependencies-3` = (project in file("bundle/kamon-bundle-dependencies-3"))
.disablePlugins(AssemblyPlugin)
.settings(noPublishing: _*)
.settings(ideSkipProject: _*)
.settings(
crossScalaVersions := Seq(scala_3_version)
)
.dependsOn(
`kamon-status-page`,
`kamon-instrumentation-common`,
`kamon-executors`,
`kamon-scala-future`,
`kamon-logback`,
`kamon-jdbc`,
`kamon-kafka`,
`kamon-cassandra`,
`kamon-elasticsearch`,
`kamon-spring`,
`kamon-annotation`,
`kamon-annotation-api`,
`kamon-system-metrics`,
`kamon-akka`,
`kamon-akka-http`,
`kamon-akka-grpc`,
`kamon-redis`,
`kamon-okhttp`,
`kamon-caffeine`,
`kamon-aws-sdk`,
`kamon-cats-io-3`,
`kamon-pekko`,
`kamon-pekko-http`,
`kamon-pekko-grpc`,
`kamon-tapir`
)

lazy val `kamon-bundle` = (project in file("bundle/kamon-bundle"))
.enablePlugins(AssemblyPlugin)
.settings(commonBundleSettings)
Expand All @@ -1065,6 +1115,18 @@ lazy val `kamon-bundle` = (project in file("bundle/kamon-bundle"))
`kamon-bundle-dependencies-2-12-and-up` % "shaded"
)

lazy val `kamon-bundle-3` = (project in file("bundle/kamon-bundle-3"))
.enablePlugins(AssemblyPlugin)
.settings(commonBundleSettings)
.settings(ideSkipProject: _*)
.settings(
crossScalaVersions := Seq(scala_3_version)
)
.dependsOn(
`kamon-core`,
`kamon-bundle-dependencies-3` % "shaded"
)

lazy val `kamon-bundle_2_11` = (project in file("bundle/kamon-bundle_2.11"))
.enablePlugins(AssemblyPlugin)
.settings(commonBundleSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import kanela.agent.api.instrumentation.InstrumentationBuilder
import kanela.agent.libs.net.bytebuddy.asm.Advice
import okhttp3.OkHttpClient

import scala.annotation.static

class OkHttpInstrumentation extends InstrumentationBuilder {

/**
Expand All @@ -41,7 +43,7 @@ object OkHttpClientBuilderAdvisor {
import scala.collection.JavaConverters._

@Advice.OnMethodEnter(suppress = classOf[Throwable])
def addKamonInterceptor(@Advice.Argument(0) builder: OkHttpClient.Builder): Unit = {
@static def addKamonInterceptor(@Advice.Argument(0) builder: OkHttpClient.Builder): Unit = {
val interceptors = builder.networkInterceptors.asScala
if (!interceptors.exists(_.isInstanceOf[KamonTracingInterceptor])) {
builder.addNetworkInterceptor(new KamonTracingInterceptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class OkHttpTracingInstrumentationSpec extends AnyWordSpec
with Matchers
with Eventually
with SpanSugar
with InitAndStopKamonAfterAll
with BeforeAndAfterEach
with TestSpanReporter
with JettySupport
Expand Down Expand Up @@ -281,6 +280,7 @@ class OkHttpTracingInstrumentationSpec extends AnyWordSpec
}

override protected def beforeAll(): Unit = {
Kamon.init()
super.beforeAll()
applyConfig(
s"""
Expand All @@ -293,15 +293,14 @@ class OkHttpTracingInstrumentationSpec extends AnyWordSpec
| }
|}
|""".stripMargin)
enableFastSpanFlushing()
sampleAlways()

startServer()
}

override protected def afterAll(): Unit = {
stopServer()
super.afterAll()
Kamon.stop()
}

override protected def beforeEach(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.springframework.web.servlet.HandlerMapping

import java.util.concurrent.Callable
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import scala.annotation.static

class SpringMVCInstrumentation extends InstrumentationBuilder {

Expand All @@ -35,10 +36,10 @@ class SpringMVCInstrumentation extends InstrumentationBuilder {
*/
onType("org.springframework.web.servlet.DispatcherServlet")
.mixin(classOf[HasServerInstrumentation.Mixin])
.advise(method("doDispatch"), DispatchAdvice)
.advise(method("render"), RenderAdvice)
.advise(method("processHandlerException"), ProcessHandlerExceptionAdvice)
.advise(method("getHandler").and(takesArguments(1)), GetHandlerAdvice)
.advise(method("doDispatch"), classOf[DispatchAdvice])
.advise(method("render"), classOf[RenderAdvice])
.advise(method("processHandlerException"), classOf[ProcessHandlerExceptionAdvice])
.advise(method("getHandler").and(takesArguments(1)), classOf[GetHandlerAdvice])

/*
* Changes Callable argument of startCallableProcessing with an
Expand All @@ -50,9 +51,10 @@ class SpringMVCInstrumentation extends InstrumentationBuilder {
.and(withArgument(0, classOf[Callable[_]])), classOf[CallableWrapper])
}

class DispatchAdvice
object DispatchAdvice {
@Advice.OnMethodEnter()
def enter(@Advice.This dispatcherServlet: HasServerInstrumentation,
@static def enter(@Advice.This dispatcherServlet: HasServerInstrumentation,
@Advice.Argument(0) request: HttpServletRequest): (RequestHandler, Scope) = {
val requestHandler = Option(request.getAttribute("kamon-handler").asInstanceOf[RequestHandler])
.getOrElse({
Expand All @@ -68,7 +70,7 @@ object DispatchAdvice {
}

@Advice.OnMethodExit(onThrowable = classOf[Throwable], suppress = classOf[Throwable])
def exit(@Advice.Enter enter: (RequestHandler, Scope),
@static def exit(@Advice.Enter enter: (RequestHandler, Scope),
@Advice.Argument(0) request: HttpServletRequest,
@Advice.Argument(1) response: HttpServletResponse): Unit = {
val (handler, scope) = enter
Expand All @@ -83,9 +85,10 @@ object DispatchAdvice {
}
}

class GetHandlerAdvice
object GetHandlerAdvice {
@Advice.OnMethodExit()
def exit(@Advice.Argument(0) request: HttpServletRequest): Unit = {
@static def exit(@Advice.Argument(0) request: HttpServletRequest): Unit = {
val handler = request.getAttribute("kamon-handler").asInstanceOf[RequestHandler]
val pattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)

Expand All @@ -96,21 +99,23 @@ object GetHandlerAdvice {
}
}

class ProcessHandlerExceptionAdvice
object ProcessHandlerExceptionAdvice {
@Advice.OnMethodExit(onThrowable = classOf[Throwable], suppress = classOf[Throwable])
def exit(@Advice.Argument(3) throwable: Throwable): Unit = {
@static def exit(@Advice.Argument(3) throwable: Throwable): Unit = {
if (throwable != null) {
Kamon.currentSpan().fail(throwable)
}
}
}

class RenderAdvice
object RenderAdvice {
@Advice.OnMethodEnter()
def enter(): Span =
@static def enter(): Span =
Kamon.internalSpanBuilder("view.render", "spring.server").start()

@Advice.OnMethodExit()
def exit(@Advice.Enter span: Span): Unit =
@static def exit(@Advice.Enter span: Span): Unit =
span.finish()
}

0 comments on commit 681c5d1

Please sign in to comment.