aboutsummaryrefslogtreecommitdiff
path: root/kamon-spray/src
diff options
context:
space:
mode:
authorIvan Topolnak <ivantopo@gmail.com>2013-12-12 17:55:49 -0300
committerIvan Topolnak <ivantopo@gmail.com>2013-12-12 17:55:49 -0300
commitf2d7734905a2279845e4d5a7143cff01a9eed1cb (patch)
tree69e94e2575ff73a0f9b199a66c2f9b943b446d23 /kamon-spray/src
parent282fc1adac088f9d51cc4e5e9d0002bc64aaa278 (diff)
parent5e214a33acabe972865dfab637556d680925d869 (diff)
downloadKamon-f2d7734905a2279845e4d5a7143cff01a9eed1cb.tar.gz
Kamon-f2d7734905a2279845e4d5a7143cff01a9eed1cb.tar.bz2
Kamon-f2d7734905a2279845e4d5a7143cff01a9eed1cb.zip
Merge branch 'wip/spray-client-tracing'
Diffstat (limited to 'kamon-spray/src')
-rw-r--r--kamon-spray/src/main/resources/META-INF/aop.xml1
-rw-r--r--kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala88
-rw-r--r--kamon-spray/src/test/scala/kamon/spray/ClientRequestTracingSpec.scala31
-rw-r--r--kamon-spray/src/test/scala/kamon/spray/ServerRequestTracingSpec.scala (renamed from kamon-spray/src/test/scala/kamon/ServerRequestTracingSpec.scala)28
4 files changed, 133 insertions, 15 deletions
diff --git a/kamon-spray/src/main/resources/META-INF/aop.xml b/kamon-spray/src/main/resources/META-INF/aop.xml
index afbbb8c0..8324617b 100644
--- a/kamon-spray/src/main/resources/META-INF/aop.xml
+++ b/kamon-spray/src/main/resources/META-INF/aop.xml
@@ -5,6 +5,7 @@
<aspects>
<aspect name="spray.can.server.ServerRequestTracing"/>
+ <aspect name="spray.can.client.ClientRequestTracing"/>
<include within="spray..*"/>
</aspects>
</aspectj>
diff --git a/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala b/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala
new file mode 100644
index 00000000..b081bf00
--- /dev/null
+++ b/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala
@@ -0,0 +1,88 @@
+/* ===================================================
+ * Copyright © 2013 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================== */
+
+package spray.can.client
+
+import org.aspectj.lang.annotation._
+import org.aspectj.lang.ProceedingJoinPoint
+import spray.http.{HttpMessageEnd, HttpRequest}
+import spray.http.HttpHeaders.Host
+import kamon.trace.{TraceContext, Trace, Segments}
+import kamon.trace.Segments.{ContextAndSegmentCompletionAware, HttpClientRequest}
+import kamon.trace.Trace.SegmentCompletionHandle
+
+@Aspect
+class ClientRequestTracing {
+
+ @DeclareMixin("spray.can.client.HttpHostConnector.RequestContext")
+ def mixin: ContextAndSegmentCompletionAware = new ContextAndSegmentCompletionAware {
+ val traceContext: Option[TraceContext] = Trace.context()
+ var completionHandle: Option[SegmentCompletionHandle] = None
+ }
+
+
+ @Pointcut("execution(spray.can.client.HttpHostConnector.RequestContext.new(..)) && this(ctx) && args(request, *, *, *)")
+ def requestContextCreation(ctx: ContextAndSegmentCompletionAware, request: HttpRequest): Unit = {}
+
+ @After("requestContextCreation(ctx, request)")
+ def afterRequestContextCreation(ctx: ContextAndSegmentCompletionAware, request: HttpRequest): Unit = {
+ // The RequestContext will be copied when a request needs to be retried but we are only interested in creating the
+ // completion handle the first time we create one.
+
+ // The read to ctx.completionHandle should take care of initializing the aspect timely.
+ if(ctx.completionHandle.isEmpty) {
+ val requestAttributes = Map[String, String](
+ "host" -> request.header[Host].map(_.value).getOrElse("unknown"),
+ "path" -> request.uri.path.toString(),
+ "method" -> request.method.toString()
+ )
+ val completionHandle = Trace.startSegment(category = HttpClientRequest, attributes = requestAttributes)
+ ctx.completionHandle = Some(completionHandle)
+ }
+ }
+
+
+ @Pointcut("execution(* spray.can.client.HttpHostConnector.RequestContext.copy(..)) && this(old)")
+ def copyingRequestContext(old: ContextAndSegmentCompletionAware): Unit = {}
+
+ @Around("copyingRequestContext(old)")
+ def aroundCopyingRequestContext(pjp: ProceedingJoinPoint, old: ContextAndSegmentCompletionAware) = {
+ Trace.withContext(old.traceContext) {
+ pjp.proceed()
+ }
+ }
+
+
+ @Pointcut("execution(* spray.can.client.HttpHostConnectionSlot.dispatchToCommander(..)) && args(requestContext, message)")
+ def dispatchToCommander(requestContext: ContextAndSegmentCompletionAware, message: Any): Unit = {}
+
+ @Around("dispatchToCommander(requestContext, message)")
+ def aroundDispatchToCommander(pjp: ProceedingJoinPoint, requestContext: ContextAndSegmentCompletionAware, message: Any) = {
+ requestContext.traceContext match {
+ case ctx @ Some(_) =>
+ Trace.withContext(ctx) {
+ if(message.isInstanceOf[HttpMessageEnd])
+ requestContext.completionHandle.map(_.complete(Segments.End()))
+
+ pjp.proceed()
+ }
+
+ case None => pjp.proceed()
+ }
+ }
+
+
+} \ No newline at end of file
diff --git a/kamon-spray/src/test/scala/kamon/spray/ClientRequestTracingSpec.scala b/kamon-spray/src/test/scala/kamon/spray/ClientRequestTracingSpec.scala
new file mode 100644
index 00000000..393ae6ff
--- /dev/null
+++ b/kamon-spray/src/test/scala/kamon/spray/ClientRequestTracingSpec.scala
@@ -0,0 +1,31 @@
+package kamon.spray
+
+import akka.testkit.TestKit
+import akka.actor.ActorSystem
+import org.scalatest.WordSpecLike
+import spray.httpx.RequestBuilding
+import spray.client.pipelining._
+import kamon.trace.{UowTrace, Trace}
+import scala.concurrent.Await
+
+class ClientRequestTracingSpec extends TestKit(ActorSystem("server-request-tracing-spec")) with WordSpecLike with RequestBuilding with TestServer {
+ implicit val ec = system.dispatcher
+
+
+ "the client instrumentation" should {
+ "record segments for a client http request" in {
+
+ Trace.start("record-segments")(system)
+
+ send {
+ Get(s"http://127.0.0.1:$port/ok")
+
+ // We don't care about the response, just make sure we finish the Trace after the response has been received.
+ } map(rsp => Trace.finish())
+
+ val trace = expectMsgType[UowTrace]
+ println(trace.segments)
+ }
+ }
+
+}
diff --git a/kamon-spray/src/test/scala/kamon/ServerRequestTracingSpec.scala b/kamon-spray/src/test/scala/kamon/spray/ServerRequestTracingSpec.scala
index d598431a..e54fe24f 100644
--- a/kamon-spray/src/test/scala/kamon/ServerRequestTracingSpec.scala
+++ b/kamon-spray/src/test/scala/kamon/spray/ServerRequestTracingSpec.scala
@@ -13,20 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================== */
-package kamon
+package kamon.spray
import _root_.spray.httpx.RequestBuilding
import _root_.spray.routing.SimpleRoutingApp
import akka.testkit.TestKit
-import akka.actor.{ActorRef, ActorSystem}
+import akka.actor.ActorSystem
import org.scalatest.WordSpecLike
import scala.concurrent.Await
import scala.concurrent.duration._
import _root_.spray.client.pipelining._
import akka.util.Timeout
-import kamon.trace.Trace
-import kamon.Kamon.Extension
-import kamon.trace.UowTracing.{Finish, Start}
+import kamon.trace.{UowTrace, Trace}
+import kamon.Kamon
class ServerRequestTracingSpec extends TestKit(ActorSystem("server-request-tracing-spec")) with WordSpecLike with RequestBuilding with TestServer {
@@ -37,8 +36,7 @@ class ServerRequestTracingSpec extends TestKit(ActorSystem("server-request-traci
}
within(5 seconds) {
- val traceId = expectMsgPF() { case Start(id, _) => id}
- expectMsgPF() { case Finish(traceId) => }
+ fishForNamedTrace("ok")
}
}
@@ -48,9 +46,7 @@ class ServerRequestTracingSpec extends TestKit(ActorSystem("server-request-traci
}
within(5 seconds) {
- val traceId = expectMsgPF() { case Start(id, _) => id }
- println("Expecting for trace: " + traceId)
- expectMsgPF() { case Finish(traceId) => }
+ fishForNamedTrace("clearcontext")
}
}
@@ -60,19 +56,21 @@ class ServerRequestTracingSpec extends TestKit(ActorSystem("server-request-traci
}
within(5 seconds) {
- expectMsgPF() { case Start(_, "GET: /accounts") => }
+ fishForNamedTrace("accounts")
}
}
}
+
+ def fishForNamedTrace(traceName: String) = fishForMessage() {
+ case trace: UowTrace if trace.name.contains(traceName) => true
+ case _ => false
+ }
}
trait TestServer extends SimpleRoutingApp {
self: TestKit =>
- // Nasty, but very helpful for tests.
- AkkaExtensionSwap.swap(system, Trace, new Extension {
- def manager: ActorRef = testActor
- })
+ Kamon(Trace).tell(Trace.Register, testActor)
implicit val timeout = Timeout(20 seconds)
val port: Int = Await.result(