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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
package kamon.instrumentation
import org.aspectj.lang.annotation.{DeclareMixin, After, Pointcut, Aspect}
import kamon.{TraceContext, Tracer}
import kamon.trace.UowTracing.{Finish, Rename}
import spray.http.HttpRequest
import spray.can.server.{OpenRequest, OpenRequestComponent}
import kamon.trace.context.TracingAwareContext
//import spray.can.client.HttpHostConnector.RequestContext
trait ContextAware {
def traceContext: Option[TraceContext]
}
@Aspect
class SprayOpenRequestContextTracing {
@DeclareMixin("spray.can.server.OpenRequestComponent.DefaultOpenRequest")
def mixinContextAwareToOpenRequest: ContextAware = new ContextAware {
val traceContext: Option[TraceContext] = Tracer.context()
}
}
@Aspect
class SprayServerInstrumentation {
@Pointcut("execution(spray.can.server.OpenRequestComponent$DefaultOpenRequest.new(..)) && this(openRequest) && args(enclosing, request, closeAfterResponseCompletion, timestamp)")
def openRequestInit(openRequest: OpenRequest, enclosing: OpenRequestComponent, request: HttpRequest, closeAfterResponseCompletion: Boolean, timestamp: Long): Unit = {}
@After("openRequestInit(openRequest, enclosing, request, closeAfterResponseCompletion, timestamp)")
def afterInit(openRequest: OpenRequest, enclosing: OpenRequestComponent, request: HttpRequest, closeAfterResponseCompletion: Boolean, timestamp: Long): Unit = {
//@After("openRequestInit()")
//def afterInit(): Unit = {
Tracer.start
val discard = openRequest.asInstanceOf[ContextAware].traceContext
//println("Reply: %s - %s ", Tracer.context().get.id, request.uri.path.toString())
// if(discard.isEmpty || discard != Tracer.context()) {
// println("MEGA ERROR")
// }
//openRequest.traceContext
//println("Created the context: " + Tracer.context() + " for the transaction: " + request)
Tracer.context().map(_.entries ! Rename(request.uri.path.toString()))
}
@Pointcut("execution(* spray.can.server.OpenRequestComponent$DefaultOpenRequest.handleResponseEndAndReturnNextOpenRequest(..)) && target(openRequest)")
def openRequestCreation(openRequest: OpenRequest): Unit = {}
@After("openRequestCreation(openRequest)")
def afterFinishingRequest(openRequest: OpenRequest): Unit = {
// println("Finishing a request: " + Tracer.context())
val original = openRequest.asInstanceOf[ContextAware].traceContext
println("The original is: " + original + " - " + openRequest.request.uri.path)
Tracer.context().map(_.entries ! Finish())
if(Tracer.context() != original) {
println(s"OMG DIFFERENT Original: [${original}] - Came in: [${Tracer.context}]")
}
if(Tracer.context().isEmpty) {
println("WOOOOOPAAAAAAAAA")
}
}
@Pointcut("execution(spray.can.client.HttpHostConnector.RequestContext.new(..)) && this(ctx)")
def requestRecordInit(ctx: TracingAwareContext): Unit = {}
@After("requestRecordInit(ctx)")
def whenCreatedRequestRecord(ctx: TracingAwareContext): Unit = {
// Necessary to force the initialization of TracingAwareRequestContext at the moment of creation.
ctx.traceContext
}
@Pointcut("execution(* spray.can.client.HttpHostConnectionSlot.dispatchToCommander(..)) && args(ctx, msg)")
def requestRecordInit2(ctx: TracingAwareContext, msg: Any): Unit = {}
@After("requestRecordInit2(ctx, msg)")
def whenCreatedRequestRecord2(ctx: TracingAwareContext, 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")))
}
}
case class DefaultTracingAwareRequestContext(traceContext: Option[TraceContext] = Tracer.context(), timestamp: Long = System.nanoTime) extends TracingAwareContext
@Aspect
class SprayRequestContextTracing {
@DeclareMixin("spray.can.client.HttpHostConnector.RequestContext")
def mixin: TracingAwareContext = DefaultTracingAwareRequestContext()
}
|