From 0159cc5bc1e6712ab68a921a65d677504e0301fb Mon Sep 17 00:00:00 2001 From: Hugh Simpson Date: Thu, 10 Oct 2024 12:48:30 +0100 Subject: [PATCH] fix RequestContextImpl.copy instrumentation issue with pekko-http 1.1.+ --- .../pekko/http/PekkoHttpServerInstrumentation.scala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/instrumentation/kamon-pekko-http/src/main/scala/kamon/instrumentation/pekko/http/PekkoHttpServerInstrumentation.scala b/instrumentation/kamon-pekko-http/src/main/scala/kamon/instrumentation/pekko/http/PekkoHttpServerInstrumentation.scala index 473184aa2..89606e73f 100644 --- a/instrumentation/kamon-pekko-http/src/main/scala/kamon/instrumentation/pekko/http/PekkoHttpServerInstrumentation.scala +++ b/instrumentation/kamon-pekko-http/src/main/scala/kamon/instrumentation/pekko/http/PekkoHttpServerInstrumentation.scala @@ -55,9 +55,10 @@ class PekkoHttpServerInstrumentation extends InstrumentationBuilder { * The rest of these sections are just about making sure that we can generate an appropriate operation name (i.e. free * of variables) and take a Sampling Decision in case none has been taken so far. */ - onType("org.apache.pekko.http.scaladsl.server.RequestContextImpl") - .mixin(classOf[HasMatchingContext.Mixin]) - .intercept(method("copy"), RequestContextCopyInterceptor) + Try(Class.forName("org.apache.pekko.http.scaladsl.server.RequestContext")).toOption + .foldLeft( + onType("org.apache.pekko.http.scaladsl.server.RequestContextImpl").mixin(classOf[HasMatchingContext.Mixin]) + )((advice, klass) => advice.intercept(withReturnTypes(klass), RequestContextCopyInterceptor)) onType("org.apache.pekko.http.scaladsl.server.directives.PathDirectives") .intercept(method("rawPathPrefix"), classOf[PathDirectivesRawPathPrefixInterceptor]) @@ -262,7 +263,7 @@ object RequestContextCopyInterceptor { @RuntimeType def copy(@This context: RequestContext, @SuperCall copyCall: Callable[RequestContext]): RequestContext = { val copiedRequestContext = copyCall.call() - copiedRequestContext.asInstanceOf[HasMatchingContext].setMatchingContext( + if (copiedRequestContext ne context) copiedRequestContext.asInstanceOf[HasMatchingContext].setMatchingContext( context.asInstanceOf[HasMatchingContext].matchingContext ) copiedRequestContext