Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducer: Span operation name duplicated when more than 1 endpoint defined per route #1063

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kamon.instrumentation.akka.http

import java.util.concurrent.Callable

import akka.http.scaladsl.marshalling.{ToEntityMarshaller, ToResponseMarshallable, ToResponseMarshaller}
import akka.http.scaladsl.model.StatusCodes.Redirection
import akka.http.scaladsl.model.{HttpHeader, HttpRequest, HttpResponse, StatusCode, Uri}
Expand All @@ -22,8 +21,8 @@ import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import java.util.regex.Pattern

import akka.NotUsed
import akka.http.scaladsl.server.RouteResult.Rejected
import akka.stream.scaladsl.Flow
import kamon.context.Context
import kanela.agent.libs.net.bytebuddy.matcher.ElementMatchers.isPublic
Expand Down Expand Up @@ -103,6 +102,7 @@ trait HasMatchingContext {
def setMatchingContext(ctx: Seq[PathMatchingContext]): Unit
def setDefaultOperationName(defaultOperationName: String): Unit
def prependMatchingContext(matched: PathMatchingContext): Unit
def popOneMatchingContext(): Unit
}

object HasMatchingContext {
Expand All @@ -123,6 +123,9 @@ object HasMatchingContext {
override def prependMatchingContext(matched: PathMatchingContext): Unit =
matchingContext = matched +: matchingContext

override def popOneMatchingContext(): Unit =
matchingContext = matchingContext.tail

@Initializer
def initialize(): Unit =
matchingContext = Seq.empty
Expand Down Expand Up @@ -280,18 +283,29 @@ object PathDirectivesRawPathPrefixInterceptor {
def rawPathPrefix[T](@Argument(1) matcher: PathMatcher[T]): Directive[T] = {
implicit val LIsTuple = matcher.ev

extract(ctx => {
extract { ctx =>
val fullPath = ctx.unmatchedPath.toString()
val matching = matcher(ctx.unmatchedPath)

matching match {
case m: Matched[_] =>
ctx.asInstanceOf[HasMatchingContext].prependMatchingContext(PathMatchingContext(fullPath, m))
ctx.asInstanceOf[HasMatchingContext]
.prependMatchingContext(PathMatchingContext(fullPath, m))
case _ =>
}
matching
}).flatMap {
case Matched(rest, values) => tprovide(values) & mapRequestContext(_ withUnmatchedPath rest)
case Unmatched => reject

(ctx, matching)
} flatMap {
case (ctx, Matched(rest, values)) =>
tprovide(values) & mapRequestContext(_ withUnmatchedPath rest) & mapRouteResult { routeResult =>

if(routeResult.isInstanceOf[Rejected])
ctx.asInstanceOf[HasMatchingContext].popOneMatchingContext()

routeResult
}

case (_, Unmatched) => reject
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package kamon.instrumentation.akka.http

import java.util.concurrent.Callable

import akka.http.scaladsl.marshalling.{ToEntityMarshaller, ToResponseMarshallable, ToResponseMarshaller}
import akka.http.scaladsl.model.StatusCodes.Redirection
import akka.http.scaladsl.model.{HttpHeader, HttpRequest, HttpResponse, StatusCode, Uri}
Expand All @@ -38,8 +37,8 @@ import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import java.util.regex.Pattern

import akka.NotUsed
import akka.http.scaladsl.server.RouteResult.Rejected
import akka.stream.scaladsl.Flow
import kamon.context.Context
import kanela.agent.libs.net.bytebuddy.matcher.ElementMatchers.isPublic
Expand Down Expand Up @@ -127,6 +126,7 @@ trait HasMatchingContext {
def setMatchingContext(ctx: Seq[PathMatchingContext]): Unit
def setDefaultOperationName(defaultOperationName: String): Unit
def prependMatchingContext(matched: PathMatchingContext): Unit
def popOneMatchingContext(): Unit
}

object HasMatchingContext {
Expand All @@ -147,6 +147,9 @@ object HasMatchingContext {
override def prependMatchingContext(matched: PathMatchingContext): Unit =
matchingContext = matched +: matchingContext

override def popOneMatchingContext(): Unit =
matchingContext = matchingContext.tail

@Initializer
def initialize(): Unit =
matchingContext = Seq.empty
Expand Down Expand Up @@ -303,18 +306,29 @@ object PathDirectivesRawPathPrefixInterceptor {
def rawPathPrefix[T](@Argument(0) matcher: PathMatcher[T]): Directive[T] = {
implicit val LIsTuple = matcher.ev

extract(ctx => {
extract { ctx =>
val fullPath = ctx.unmatchedPath.toString()
val matching = matcher(ctx.unmatchedPath)

matching match {
case m: Matched[_] =>
ctx.asInstanceOf[HasMatchingContext].prependMatchingContext(PathMatchingContext(fullPath, m))
ctx.asInstanceOf[HasMatchingContext]
.prependMatchingContext(PathMatchingContext(fullPath, m))
case _ =>
}
matching
}).flatMap {
case Matched(rest, values) => tprovide(values) & mapRequestContext(_ withUnmatchedPath rest)
case Unmatched => reject

(ctx, matching)
} flatMap {
case (ctx, Matched(rest, values)) =>
tprovide(values) & mapRequestContext(_ withUnmatchedPath rest) & mapRouteResult { routeResult =>

if(routeResult.isInstanceOf[Rejected])
ctx.asInstanceOf[HasMatchingContext].popOneMatchingContext()

routeResult
}

case (_, Unmatched) => reject
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kamon.instrumentation.akka.http

import java.util.concurrent.Callable

import akka.http.scaladsl.marshalling.{ToEntityMarshaller, ToResponseMarshallable, ToResponseMarshaller}
import akka.http.scaladsl.model.StatusCodes.Redirection
import akka.http.scaladsl.model.{HttpHeader, HttpRequest, HttpResponse, StatusCode, Uri}
Expand All @@ -22,8 +21,8 @@ import scala.concurrent.{Batchable, ExecutionContext, Future, Promise}
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import java.util.regex.Pattern

import akka.NotUsed
import akka.http.scaladsl.server.RouteResult.Rejected
import akka.stream.scaladsl.Flow
import kamon.context.Context
import kanela.agent.libs.net.bytebuddy.matcher.ElementMatchers.isPublic
Expand Down Expand Up @@ -110,6 +109,7 @@ trait HasMatchingContext {
def setMatchingContext(ctx: Seq[PathMatchingContext]): Unit
def setDefaultOperationName(defaultOperationName: String): Unit
def prependMatchingContext(matched: PathMatchingContext): Unit
def popOneMatchingContext(): Unit
}

object HasMatchingContext {
Expand All @@ -130,6 +130,9 @@ object HasMatchingContext {
override def prependMatchingContext(matched: PathMatchingContext): Unit =
matchingContext = matched +: matchingContext

override def popOneMatchingContext(): Unit =
matchingContext = matchingContext.tail

@Initializer
def initialize(): Unit =
matchingContext = Seq.empty
Expand Down Expand Up @@ -286,18 +289,29 @@ object PathDirectivesRawPathPrefixInterceptor {
def rawPathPrefix[T](@Argument(0) matcher: PathMatcher[T]): Directive[T] = {
implicit val LIsTuple = matcher.ev

extract(ctx => {
extract { ctx =>
val fullPath = ctx.unmatchedPath.toString()
val matching = matcher(ctx.unmatchedPath)

matching match {
case m: Matched[_] =>
ctx.asInstanceOf[HasMatchingContext].prependMatchingContext(PathMatchingContext(fullPath, m))
ctx.asInstanceOf[HasMatchingContext]
.prependMatchingContext(PathMatchingContext(fullPath, m))
case _ =>
}
matching
}).flatMap {
case Matched(rest, values) => tprovide(values) & mapRequestContext(_ withUnmatchedPath rest)
case Unmatched => reject

(ctx, matching)
} flatMap {
case (ctx, Matched(rest, values)) =>
tprovide(values) & mapRequestContext(_ withUnmatchedPath rest) & mapRouteResult { routeResult =>

if(routeResult.isInstanceOf[Rejected])
ctx.asInstanceOf[HasMatchingContext].popOneMatchingContext()

routeResult
}

case (_, Unmatched) => reject
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ class AkkaHttpServerTracingSpec extends WordSpecLike with Matchers with ScalaFut
}
}

"return the correct operation name with overloaded route" in {
val target = s"$protocol://$interface:$port/some_endpoint"

okHttp.newCall(new Request.Builder()
.get()
.url(target).build())
.execute()

val span = eventually(timeout(10 seconds))(testSpanReporter().nextSpan().value)
span.operationName shouldBe "/some_endpoint"
}
"not include variables in operation name" when {
"including nested directives" in {
val path = s"extraction/nested/42/fixed/anchor/32/${UUID.randomUUID().toString}/fixed/44/CafE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ trait TestWebServer extends TracingDirectives {
path("name-will-be-changed") {
complete("OK")
}
} ~
path("some_endpoint") {
post(complete(OK))
} ~
path("some_endpoint") {
get(complete(OK))
}
}

Expand Down