aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/instrumentation/SprayServerInstrumentation.scala
blob: f8ab709ef76a3f3f98375fc1756451db12cf55e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package kamon.instrumentation

import org.aspectj.lang.annotation.{DeclareMixin, After, Pointcut, Aspect}
import kamon.{TraceContext, Tracer}
import kamon.trace.UowTracing.{WebExternal, Finish, Rename}
import spray.http.HttpRequest
import spray.can.server.OpenRequestComponent
import spray.can.client.HttpHostConnector.RequestContext
import spray.http.HttpHeaders.Host

@Aspect
class SprayServerInstrumentation {

  @Pointcut("execution(spray.can.server.OpenRequestComponent$DefaultOpenRequest.new(..)) && args(enclosing, request, closeAfterResponseCompletion, timestamp)")
  def openRequestInit(enclosing: OpenRequestComponent, request: HttpRequest, closeAfterResponseCompletion: Boolean, timestamp: Long): Unit = {}

  @After("openRequestInit(enclosing, request, closeAfterResponseCompletion, timestamp)")
  def afterInit(enclosing: OpenRequestComponent, request: HttpRequest, closeAfterResponseCompletion: Boolean, timestamp: Long): Unit = {
  //@After("openRequestInit()")
  //def afterInit(): Unit = {
    Tracer.start
    //println("Created the context: " + Tracer.context() + " for the transaction: " + request.uri.path.toString())
    Tracer.context().map(_.entries ! Rename(request.uri.path.toString()))
  }

  @Pointcut("execution(* spray.can.server.OpenRequest.handleResponseEndAndReturnNextOpenRequest(..))")
  def openRequestCreation(): Unit = {}

  @After("openRequestCreation()")
  def afterFinishingRequest(): Unit = {
    println("Finishing a request: " + Tracer.context())

    Tracer.context().map(_.entries ! Finish())
  }




  @Pointcut("execution(spray.can.client.HttpHostConnector.RequestContext.new(..)) && this(ctx)")
  def requestRecordInit(ctx: TracingAwareRequestContext): Unit = {}

  @After("requestRecordInit(ctx)")
  def whenCreatedRequestRecord(ctx: TracingAwareRequestContext): Unit = {
    // Necessary to force the initialization of TracingAwareRequestContext at the moment of creation.
    ctx.context
  }






  @Pointcut("execution(* spray.can.client.HttpHostConnectionSlot.dispatchToCommander(..)) && args(ctx, msg)")
  def requestRecordInit2(ctx: TracingAwareRequestContext, msg: Any): Unit = {}

  @After("requestRecordInit2(ctx, msg)")
  def whenCreatedRequestRecord2(ctx: TracingAwareRequestContext, msg: Any): Unit = {
    println("=======> Spent in WEB External: " + (System.nanoTime() - ctx.timestamp))

    // TODO: REMOVE THIS:
    val request = (ctx.asInstanceOf[RequestContext]).request

    ctx.context.map(_.entries ! WebExternal(ctx.timestamp, System.nanoTime(), request.header[Host].map(_.host).getOrElse("UNKNOWN")))

  }
}

trait TracingAwareRequestContext {
  def context: Option[TraceContext]
  def timestamp: Long
}

case class DefaultTracingAwareRequestContext(context: Option[TraceContext] = Tracer.context(),
                                             timestamp: Long = System.nanoTime) extends TracingAwareRequestContext


@Aspect
class SprayRequestContextTracing {

  @DeclareMixin("spray.can.client.HttpHostConnector.RequestContext")
  def mixin: TracingAwareRequestContext = DefaultTracingAwareRequestContext()
}