diff options
Diffstat (limited to 'kamon-spray/src/main/scala/spray/can')
-rw-r--r-- | kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala | 50 | ||||
-rw-r--r-- | kamon-spray/src/main/scala/spray/can/server/ServerRequestTracing.scala | 37 |
2 files changed, 43 insertions, 44 deletions
diff --git a/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala b/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala index a1505a63..c74019dd 100644 --- a/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala +++ b/kamon-spray/src/main/scala/spray/can/client/ClientRequestTracing.scala @@ -20,58 +20,60 @@ 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 +import kamon.trace.{ TraceRecorder, SegmentCompletionHandleAware, TraceContextAware } +import kamon.metrics.TraceMetrics.HttpClientRequest +import kamon.Kamon +import kamon.spray.Spray @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 - } + def mixin: SegmentCompletionHandleAware = SegmentCompletionHandleAware.default @Pointcut("execution(spray.can.client.HttpHostConnector.RequestContext.new(..)) && this(ctx) && args(request, *, *, *)") - def requestContextCreation(ctx: ContextAndSegmentCompletionAware, request: HttpRequest): Unit = {} + def requestContextCreation(ctx: SegmentCompletionHandleAware, request: HttpRequest): Unit = {} @After("requestContextCreation(ctx, request)") - def afterRequestContextCreation(ctx: ContextAndSegmentCompletionAware, request: HttpRequest): Unit = { + def afterRequestContextCreation(ctx: SegmentCompletionHandleAware, 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) + if (ctx.segmentCompletionHandle.isEmpty) { + TraceRecorder.currentContext.map { traceContext ⇒ + val requestAttributes = Map[String, String]( + "host" -> request.header[Host].map(_.value).getOrElse("unknown"), + "path" -> request.uri.path.toString(), + "method" -> request.method.toString()) + + val clientRequestName = Kamon(Spray)(traceContext.system).assignHttpClientRequestName(request) + val completionHandle = traceContext.startSegment(HttpClientRequest(clientRequestName), requestAttributes) + ctx.segmentCompletionHandle = Some(completionHandle) + } } } @Pointcut("execution(* spray.can.client.HttpHostConnector.RequestContext.copy(..)) && this(old)") - def copyingRequestContext(old: ContextAndSegmentCompletionAware): Unit = {} + def copyingRequestContext(old: SegmentCompletionHandleAware): Unit = {} @Around("copyingRequestContext(old)") - def aroundCopyingRequestContext(pjp: ProceedingJoinPoint, old: ContextAndSegmentCompletionAware) = { - Trace.withContext(old.traceContext) { + def aroundCopyingRequestContext(pjp: ProceedingJoinPoint, old: SegmentCompletionHandleAware): Any = { + TraceRecorder.withTraceContext(old.traceContext) { pjp.proceed() } } @Pointcut("execution(* spray.can.client.HttpHostConnectionSlot.dispatchToCommander(..)) && args(requestContext, message)") - def dispatchToCommander(requestContext: ContextAndSegmentCompletionAware, message: Any): Unit = {} + def dispatchToCommander(requestContext: SegmentCompletionHandleAware, message: Any): Unit = {} @Around("dispatchToCommander(requestContext, message)") - def aroundDispatchToCommander(pjp: ProceedingJoinPoint, requestContext: ContextAndSegmentCompletionAware, message: Any) = { + def aroundDispatchToCommander(pjp: ProceedingJoinPoint, requestContext: SegmentCompletionHandleAware, message: Any) = { requestContext.traceContext match { case ctx @ Some(_) ⇒ - Trace.withContext(ctx) { + TraceRecorder.withTraceContext(ctx) { if (message.isInstanceOf[HttpMessageEnd]) - requestContext.completionHandle.map(_.complete(Segments.End())) + requestContext.segmentCompletionHandle.map(_.finish(Map.empty)) pjp.proceed() } @@ -80,4 +82,4 @@ class ClientRequestTracing { } } -}
\ No newline at end of file +} diff --git a/kamon-spray/src/main/scala/spray/can/server/ServerRequestTracing.scala b/kamon-spray/src/main/scala/spray/can/server/ServerRequestTracing.scala index f0e52d12..b7479d2b 100644 --- a/kamon-spray/src/main/scala/spray/can/server/ServerRequestTracing.scala +++ b/kamon-spray/src/main/scala/spray/can/server/ServerRequestTracing.scala @@ -16,37 +16,34 @@ package spray.can.server import org.aspectj.lang.annotation._ -import kamon.trace.{ Trace, ContextAware } -import spray.http.HttpRequest +import kamon.trace.{ TraceRecorder, TraceContextAware } import akka.actor.ActorSystem -import akka.event.Logging.Warning -import org.aspectj.lang.ProceedingJoinPoint import spray.http.HttpRequest import akka.event.Logging.Warning import scala.Some +import kamon.Kamon +import kamon.spray.Spray @Aspect class ServerRequestTracing { @DeclareMixin("spray.can.server.OpenRequestComponent.DefaultOpenRequest") - def mixinContextAwareToOpenRequest: ContextAware = ContextAware.default + def mixinContextAwareToOpenRequest: TraceContextAware = TraceContextAware.default @Pointcut("execution(spray.can.server.OpenRequestComponent$DefaultOpenRequest.new(..)) && this(openRequest) && args(*, request, *, *)") - def openRequestInit(openRequest: ContextAware, request: HttpRequest): Unit = {} + def openRequestInit(openRequest: TraceContextAware, request: HttpRequest): Unit = {} @After("openRequestInit(openRequest, request)") - def afterInit(openRequest: ContextAware, request: HttpRequest): Unit = { + def afterInit(openRequest: TraceContextAware, request: HttpRequest): Unit = { val system: ActorSystem = openRequest.asInstanceOf[OpenRequest].context.actorContext.system - val config = system.settings.config.getConfig("kamon.spray") - - val token = if (config.getBoolean("include-trace-token-header")) { - val traceTokenHeader = config.getString("trace-token-header-name") - request.headers.find(_.name == traceTokenHeader).map(_.value) - } else None + val sprayExtension = Kamon(Spray)(system) val defaultTraceName: String = request.method.value + ": " + request.uri.path + val token = if (sprayExtension.includeTraceToken) { + request.headers.find(_.name == sprayExtension.traceTokenHeaderName).map(_.value) + } else None - Trace.start(defaultTraceName, token)(system) + TraceRecorder.start(defaultTraceName, token)(system) // Necessary to force initialization of traceContext when initiating the request. openRequest.traceContext @@ -57,26 +54,26 @@ class ServerRequestTracing { @After("openNewRequest()") def afterOpenNewRequest(): Unit = { - Trace.clear + TraceRecorder.clearContext } @Pointcut("execution(* spray.can.server.OpenRequestComponent$DefaultOpenRequest.handleResponseEndAndReturnNextOpenRequest(..)) && target(openRequest)") - def openRequestCreation(openRequest: ContextAware): Unit = {} + def openRequestCreation(openRequest: TraceContextAware): Unit = {} @After("openRequestCreation(openRequest)") - def afterFinishingRequest(openRequest: ContextAware): Unit = { + def afterFinishingRequest(openRequest: TraceContextAware): Unit = { val storedContext = openRequest.traceContext - val incomingContext = Trace.finish() + val incomingContext = TraceRecorder.currentContext + TraceRecorder.finish() for (original ← storedContext) { incomingContext match { - case Some(incoming) if original.id != incoming.id ⇒ + case Some(incoming) if original.token != incoming.token ⇒ publishWarning(s"Different ids when trying to close a Trace, original: [$original] - incoming: [$incoming]") case Some(_) ⇒ // nothing to do here. case None ⇒ - original.finish publishWarning(s"Trace context not present while closing the Trace: [$original]") } } |