From 7a10c0ef2a6566229e8571f6d385ca2ff794cc20 Mon Sep 17 00:00:00 2001 From: Ivan Topolnak Date: Thu, 2 Jan 2014 18:09:53 -0300 Subject: integrate trace and metrics into the base project --- kamon-trace/src/main/resources/META-INF/aop.xml | 26 ---- kamon-trace/src/main/resources/reference.conf | 3 - .../actor/ActorSystemMessagePassingTracing.scala | 65 -------- .../scala/akka/pattern/AskPatternTracing.scala | 48 ------ .../src/main/scala/kamon/trace/Segments.scala | 38 ----- kamon-trace/src/main/scala/kamon/trace/Trace.scala | 114 -------------- .../src/main/scala/kamon/trace/TraceContext.scala | 49 ------ .../src/main/scala/kamon/trace/UowTracing.scala | 82 ---------- .../instrumentation/ActorLoggingTracing.scala | 37 ----- .../ActorMessagePassingTracing.scala | 59 -------- .../trace/instrumentation/FutureTracing.scala | 47 ------ .../kamon/trace/logging/LogbackUowConverter.scala | 24 --- kamon-trace/src/test/resources/application.conf | 3 - kamon-trace/src/test/resources/logback.xml | 12 -- .../trace/instrumentation/ActorLoggingSpec.scala | 51 ------- .../ActorMessagePassingTracingSpec.scala | 84 ----------- ...orSystemMessagePassingInstrumentationSpec.scala | 165 --------------------- .../instrumentation/AskPatternTracingSpec.scala | 59 -------- .../trace/instrumentation/FutureTracingSpec.scala | 62 -------- .../instrumentation/TraceAggregatorSpec.scala | 51 ------- .../instrumentation/TraceContextFixture.scala | 10 -- 21 files changed, 1089 deletions(-) delete mode 100644 kamon-trace/src/main/resources/META-INF/aop.xml delete mode 100644 kamon-trace/src/main/resources/reference.conf delete mode 100644 kamon-trace/src/main/scala/akka/actor/ActorSystemMessagePassingTracing.scala delete mode 100644 kamon-trace/src/main/scala/akka/pattern/AskPatternTracing.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/Segments.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/Trace.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/TraceContext.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/UowTracing.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorLoggingTracing.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorMessagePassingTracing.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/instrumentation/FutureTracing.scala delete mode 100644 kamon-trace/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala delete mode 100644 kamon-trace/src/test/resources/application.conf delete mode 100644 kamon-trace/src/test/resources/logback.xml delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorLoggingSpec.scala delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorMessagePassingTracingSpec.scala delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorSystemMessagePassingInstrumentationSpec.scala delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/AskPatternTracingSpec.scala delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/FutureTracingSpec.scala delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceAggregatorSpec.scala delete mode 100644 kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceContextFixture.scala (limited to 'kamon-trace/src') diff --git a/kamon-trace/src/main/resources/META-INF/aop.xml b/kamon-trace/src/main/resources/META-INF/aop.xml deleted file mode 100644 index d9916724..00000000 --- a/kamon-trace/src/main/resources/META-INF/aop.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/kamon-trace/src/main/resources/reference.conf b/kamon-trace/src/main/resources/reference.conf deleted file mode 100644 index 03f13f01..00000000 --- a/kamon-trace/src/main/resources/reference.conf +++ /dev/null @@ -1,3 +0,0 @@ -kamon.trace { - ask-pattern-tracing = off -} \ No newline at end of file diff --git a/kamon-trace/src/main/scala/akka/actor/ActorSystemMessagePassingTracing.scala b/kamon-trace/src/main/scala/akka/actor/ActorSystemMessagePassingTracing.scala deleted file mode 100644 index 6a82782c..00000000 --- a/kamon-trace/src/main/scala/akka/actor/ActorSystemMessagePassingTracing.scala +++ /dev/null @@ -1,65 +0,0 @@ -package akka.actor - -import org.aspectj.lang.annotation._ -import kamon.trace.{ Trace, ContextAware } -import akka.dispatch.sysmsg.EarliestFirstSystemMessageList -import org.aspectj.lang.ProceedingJoinPoint - -@Aspect -class SystemMessageTraceContextMixin { - - @DeclareMixin("akka.dispatch.sysmsg.SystemMessage+") - def mixin: ContextAware = ContextAware.default - - @Pointcut("execution(akka.dispatch.sysmsg.SystemMessage+.new(..)) && this(ctx)") - def envelopeCreation(ctx: ContextAware): Unit = {} - - @After("envelopeCreation(ctx)") - def afterEnvelopeCreation(ctx: ContextAware): Unit = { - // Necessary to force the initialization of ContextAware at the moment of creation. - ctx.traceContext - } -} - -@Aspect -class RepointableActorRefTraceContextMixin { - - @DeclareMixin("akka.actor.RepointableActorRef") - def mixin: ContextAware = ContextAware.default - - @Pointcut("execution(akka.actor.RepointableActorRef.new(..)) && this(ctx)") - def envelopeCreation(ctx: ContextAware): Unit = {} - - @After("envelopeCreation(ctx)") - def afterEnvelopeCreation(ctx: ContextAware): Unit = { - // Necessary to force the initialization of ContextAware at the moment of creation. - ctx.traceContext - } - - @Pointcut("execution(* akka.actor.RepointableActorRef.point(..)) && this(repointableActorRef)") - def repointableActorRefCreation(repointableActorRef: ContextAware): Unit = {} - - @Around("repointableActorRefCreation(repointableActorRef)") - def afterRepointableActorRefCreation(pjp: ProceedingJoinPoint, repointableActorRef: ContextAware): Any = { - Trace.withContext(repointableActorRef.traceContext) { - pjp.proceed() - } - } - -} - -@Aspect -class ActorSystemMessagePassingTracing { - - @Pointcut("execution(* akka.actor.ActorCell.invokeAll$1(..)) && args(messages, *)") - def systemMessageProcessing(messages: EarliestFirstSystemMessageList): Unit = {} - - @Around("systemMessageProcessing(messages)") - def aroundSystemMessageInvoke(pjp: ProceedingJoinPoint, messages: EarliestFirstSystemMessageList): Any = { - if (messages.nonEmpty) { - val ctx = messages.head.asInstanceOf[ContextAware].traceContext - Trace.withContext(ctx)(pjp.proceed()) - - } else pjp.proceed() - } -} diff --git a/kamon-trace/src/main/scala/akka/pattern/AskPatternTracing.scala b/kamon-trace/src/main/scala/akka/pattern/AskPatternTracing.scala deleted file mode 100644 index 970a4a51..00000000 --- a/kamon-trace/src/main/scala/akka/pattern/AskPatternTracing.scala +++ /dev/null @@ -1,48 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 akka.pattern - -import org.aspectj.lang.annotation.{ AfterReturning, Pointcut, Aspect } -import akka.event.Logging.Warning -import scala.compat.Platform.EOL -import akka.actor.ActorRefProvider - -@Aspect -class AskPatternTracing { - - class StackTraceCaptureException extends Throwable - - @Pointcut(value = "execution(* akka.pattern.PromiseActorRef$.apply(..)) && args(provider, *)", argNames = "provider") - def promiseActorRefApply(provider: ActorRefProvider): Unit = { - provider.settings.config.getBoolean("kamon.trace.ask-pattern-tracing") - } - - @AfterReturning(pointcut = "promiseActorRefApply(provider)", returning = "promiseActor") - def hookAskTimeoutWarning(provider: ActorRefProvider, promiseActor: PromiseActorRef): Unit = { - val future = promiseActor.result.future - val system = promiseActor.provider.guardian.underlying.system - implicit val ec = system.dispatcher - val stack = new StackTraceCaptureException - - future onFailure { - case timeout: AskTimeoutException ⇒ - val stackString = stack.getStackTrace.drop(3).mkString("", EOL, EOL) - - system.eventStream.publish(Warning("AskPatternTracing", classOf[AskPatternTracing], - "Timeout triggered for ask pattern registered at: " + stackString)) - } - } -} diff --git a/kamon-trace/src/main/scala/kamon/trace/Segments.scala b/kamon-trace/src/main/scala/kamon/trace/Segments.scala deleted file mode 100644 index 0bc68ee7..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/Segments.scala +++ /dev/null @@ -1,38 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace - -import kamon.trace.Trace.SegmentCompletionHandle - -object Segments { - - trait Category - case object HttpClientRequest extends Category - - case class Start(category: Category, description: String = "", - attributes: Map[String, String] = Map(), timestamp: Long = System.nanoTime()) - - case class End(attributes: Map[String, String] = Map(), timestamp: Long = System.nanoTime()) - - case class Segment(start: Start, end: End) - - trait SegmentCompletionHandleAware { - var completionHandle: Option[SegmentCompletionHandle] - } - - trait ContextAndSegmentCompletionAware extends ContextAware with SegmentCompletionHandleAware -} diff --git a/kamon-trace/src/main/scala/kamon/trace/Trace.scala b/kamon-trace/src/main/scala/kamon/trace/Trace.scala deleted file mode 100644 index 0985f547..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/Trace.scala +++ /dev/null @@ -1,114 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace - -import kamon.Kamon -import akka.actor._ -import scala.Some -import kamon.trace.Trace.Register -import scala.concurrent.duration._ -import java.util.concurrent.atomic.AtomicLong - -object Trace extends ExtensionId[TraceExtension] with ExtensionIdProvider { - def lookup(): ExtensionId[_ <: Extension] = Trace - def createExtension(system: ExtendedActorSystem): TraceExtension = new TraceExtension(system) - - /*** Protocol */ - case object Register - - /** User API */ - //private[trace] val traceContext = new DynamicVariable[Option[TraceContext]](None) - private[trace] val traceContext = new ThreadLocal[Option[TraceContext]] { - override def initialValue(): Option[TraceContext] = None - } - private[trace] val tranid = new AtomicLong() - - def context() = traceContext.get - private def set(ctx: Option[TraceContext]) = traceContext.set(ctx) - - def clear: Unit = traceContext.remove() - def start(name: String)(implicit system: ActorSystem): TraceContext = { - val ctx = newTraceContext(name) - ctx.start(name) - set(Some(ctx)) - - ctx - } - - def withContext[T](ctx: Option[TraceContext])(thunk: ⇒ T): T = { - val oldval = context - set(ctx) - - try thunk - finally set(oldval) - } - - def transformContext(f: TraceContext ⇒ TraceContext): Unit = { - context.map(f).foreach(ctx ⇒ set(Some(ctx))) - } - - def finish(): Option[TraceContext] = { - val ctx = context() - ctx.map(_.finish) - clear - ctx - } - - // TODO: FIX - def newTraceContext(name: String)(implicit system: ActorSystem): TraceContext = TraceContext(Kamon(Trace), tranid.getAndIncrement, name) - - def startSegment(category: Segments.Category, description: String = "", attributes: Map[String, String] = Map()): SegmentCompletionHandle = { - val start = Segments.Start(category, description, attributes) - SegmentCompletionHandle(start) - } - - def startSegment(start: Segments.Start): SegmentCompletionHandle = SegmentCompletionHandle(start) - - case class SegmentCompletionHandle(start: Segments.Start) { - def complete(): Unit = { - val end = Segments.End() - println(s"Completing the Segment: $start - $end") - } - def complete(end: Segments.End): Unit = { - println(s"Completing the Segment: $start - $end") - } - } -} - -class TraceExtension(system: ExtendedActorSystem) extends Kamon.Extension { - val manager: ActorRef = system.actorOf(Props[TraceManager], "kamon-trace") -} - -class TraceManager extends Actor with ActorLogging { - var listeners: Seq[ActorRef] = Seq.empty - - def receive = { - case Register ⇒ - listeners = sender +: listeners - log.info("Registered [{}] as listener for Kamon traces", sender) - - case segment: UowSegment ⇒ - val tracerName = segment.id.toString - context.child(tracerName).getOrElse(newTracer(tracerName)) ! segment - - case trace: UowTrace ⇒ - listeners foreach (_ ! trace) - } - - def newTracer(name: String): ActorRef = { - context.actorOf(UowTraceAggregator.props(self, 30 seconds), name) - } -} diff --git a/kamon-trace/src/main/scala/kamon/trace/TraceContext.scala b/kamon-trace/src/main/scala/kamon/trace/TraceContext.scala deleted file mode 100644 index 3698cea1..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/TraceContext.scala +++ /dev/null @@ -1,49 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace - -import java.util.UUID -import akka.actor._ -import java.util.concurrent.atomic.AtomicLong -import scala.concurrent.duration._ -import kamon.Kamon -import kamon.trace.UowTracing.{ Finish, Start } - -// TODO: Decide if we need or not an ID, generating it takes time and it doesn't seem necessary. -case class TraceContext(private val collector: ActorRef, id: Long, uow: String = "", userContext: Option[Any] = None) { - - def start(name: String) = collector ! Start(id, name) - - def finish: Unit = { - collector ! Finish(id) - } - -} - -trait ContextAware { - def traceContext: Option[TraceContext] -} - -object ContextAware { - def default: ContextAware = new ContextAware { - val traceContext: Option[TraceContext] = Trace.context() - } -} - -trait TimedContextAware { - def timestamp: Long - def traceContext: Option[TraceContext] -} diff --git a/kamon-trace/src/main/scala/kamon/trace/UowTracing.scala b/kamon-trace/src/main/scala/kamon/trace/UowTracing.scala deleted file mode 100644 index 20cce830..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/UowTracing.scala +++ /dev/null @@ -1,82 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace - -import akka.actor._ -import scala.concurrent.duration.Duration -import kamon.trace.UowTracing._ - -sealed trait UowSegment { - def id: Long - def timestamp: Long -} - -trait AutoTimestamp extends UowSegment { - val timestamp = System.nanoTime -} - -object UowTracing { - case class Start(id: Long, name: String) extends AutoTimestamp - case class Finish(id: Long) extends AutoTimestamp - case class Rename(id: Long, name: String) extends AutoTimestamp - case class WebExternalStart(id: Long, host: String) extends AutoTimestamp - case class WebExternalFinish(id: Long) extends AutoTimestamp - case class WebExternal(id: Long, start: Long, finish: Long, host: String) extends AutoTimestamp -} - -case class UowTrace(name: String, uow: String, start: Long, end: Long, segments: Seq[UowSegment]) { - def elapsed: Long = end - start -} - -class UowTraceAggregator(reporting: ActorRef, aggregationTimeout: Duration) extends Actor with ActorLogging { - context.setReceiveTimeout(aggregationTimeout) - - var name: String = "UNKNOWN" - var segments: Seq[UowSegment] = Nil - - var pendingExternal = List[WebExternalStart]() - - var start = 0L - var end = 0L - - def receive = { - case start: Start ⇒ - this.start = start.timestamp - segments = segments :+ start - name = start.name - case finish: Finish ⇒ - end = finish.timestamp - segments = segments :+ finish; finishTracing() - case wes: WebExternalStart ⇒ pendingExternal = pendingExternal :+ wes - case finish @ WebExternalFinish(id) ⇒ pendingExternal.find(_.id == id).map(start ⇒ { - segments = segments :+ WebExternal(finish.id, start.timestamp, finish.timestamp, start.host) - }) - case Rename(id, newName) ⇒ name = newName - case segment: UowSegment ⇒ segments = segments :+ segment - case ReceiveTimeout ⇒ - log.warning("Transaction {} did not complete properly, the recorded segments are: {}", name, segments) - context.stop(self) - } - - def finishTracing(): Unit = { - reporting ! UowTrace(name, "", start, end, segments) - context.stop(self) - } -} - -object UowTraceAggregator { - def props(reporting: ActorRef, aggregationTimeout: Duration) = Props(classOf[UowTraceAggregator], reporting, aggregationTimeout) -} diff --git a/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorLoggingTracing.scala b/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorLoggingTracing.scala deleted file mode 100644 index 0d06eb2c..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorLoggingTracing.scala +++ /dev/null @@ -1,37 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import org.aspectj.lang.annotation.{ Around, Pointcut, DeclareMixin, Aspect } -import org.aspectj.lang.ProceedingJoinPoint -import kamon.trace.{ ContextAware, Trace } - -@Aspect -class ActorLoggingTracing { - - @DeclareMixin("akka.event.Logging.LogEvent+") - def mixin: ContextAware = ContextAware.default - - @Pointcut("execution(* akka.event.slf4j.Slf4jLogger.withMdc(..)) && args(logSource, logEvent, logStatement)") - def withMdcInvocation(logSource: String, logEvent: ContextAware, logStatement: () ⇒ _): Unit = {} - - @Around("withMdcInvocation(logSource, logEvent, logStatement)") - def aroundWithMdcInvocation(pjp: ProceedingJoinPoint, logSource: String, logEvent: ContextAware, logStatement: () ⇒ _): Unit = { - Trace.withContext(logEvent.traceContext) { - pjp.proceed() - } - } -} diff --git a/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorMessagePassingTracing.scala b/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorMessagePassingTracing.scala deleted file mode 100644 index 399ddf61..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorMessagePassingTracing.scala +++ /dev/null @@ -1,59 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import org.aspectj.lang.annotation._ -import org.aspectj.lang.ProceedingJoinPoint -import akka.actor.{ Props, ActorSystem, ActorRef } -import akka.dispatch.{ Envelope, MessageDispatcher } -import com.codahale.metrics.Timer -import kamon.trace.{ ContextAware, TraceContext, Trace } - -@Aspect -class BehaviourInvokeTracing { - - @Pointcut("execution(akka.actor.ActorCell.new(..)) && args(system, ref, props, dispatcher, parent)") - def actorCellCreation(system: ActorSystem, ref: ActorRef, props: Props, dispatcher: MessageDispatcher, parent: ActorRef): Unit = {} - - @Pointcut("(execution(* akka.actor.ActorCell.invoke(*)) || execution(* akka.routing.RoutedActorCell.sendMessage(*))) && args(envelope)") - def invokingActorBehaviourAtActorCell(envelope: Envelope) = {} - - @Around("invokingActorBehaviourAtActorCell(envelope)") - def aroundBehaviourInvoke(pjp: ProceedingJoinPoint, envelope: Envelope): Unit = { - //safe cast - val ctxInMessage = envelope.asInstanceOf[ContextAware].traceContext - - Trace.withContext(ctxInMessage) { - pjp.proceed() - } - } -} - -@Aspect -class EnvelopeTraceContextMixin { - - @DeclareMixin("akka.dispatch.Envelope") - def mixin: ContextAware = ContextAware.default - - @Pointcut("execution(akka.dispatch.Envelope.new(..)) && this(ctx)") - def envelopeCreation(ctx: ContextAware): Unit = {} - - @After("envelopeCreation(ctx)") - def afterEnvelopeCreation(ctx: ContextAware): Unit = { - // Necessary to force the initialization of ContextAware at the moment of creation. - ctx.traceContext - } -} diff --git a/kamon-trace/src/main/scala/kamon/trace/instrumentation/FutureTracing.scala b/kamon-trace/src/main/scala/kamon/trace/instrumentation/FutureTracing.scala deleted file mode 100644 index 844f1d61..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/instrumentation/FutureTracing.scala +++ /dev/null @@ -1,47 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import org.aspectj.lang.annotation._ -import org.aspectj.lang.ProceedingJoinPoint -import kamon.trace.{ ContextAware, TraceContext, Trace } - -@Aspect -class FutureTracing { - - @DeclareMixin("scala.concurrent.impl.CallbackRunnable || scala.concurrent.impl.Future.PromiseCompletingRunnable") - def mixin: ContextAware = ContextAware.default - - @Pointcut("execution((scala.concurrent.impl.CallbackRunnable || scala.concurrent.impl.Future.PromiseCompletingRunnable).new(..)) && this(runnable)") - def futureRelatedRunnableCreation(runnable: ContextAware): Unit = {} - - @After("futureRelatedRunnableCreation(runnable)") - def afterCreation(runnable: ContextAware): Unit = { - // Force traceContext initialization. - runnable.traceContext - } - - @Pointcut("execution(* (scala.concurrent.impl.CallbackRunnable || scala.concurrent.impl.Future.PromiseCompletingRunnable).run()) && this(runnable)") - def futureRelatedRunnableExecution(runnable: ContextAware) = {} - - @Around("futureRelatedRunnableExecution(runnable)") - def aroundExecution(pjp: ProceedingJoinPoint, runnable: ContextAware): Any = { - Trace.withContext(runnable.traceContext) { - pjp.proceed() - } - } - -} \ No newline at end of file diff --git a/kamon-trace/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala b/kamon-trace/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala deleted file mode 100644 index add47fdf..00000000 --- a/kamon-trace/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala +++ /dev/null @@ -1,24 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.logging - -import ch.qos.logback.classic.pattern.ClassicConverter -import ch.qos.logback.classic.spi.ILoggingEvent -import kamon.trace.Trace - -class LogbackUowConverter extends ClassicConverter { - def convert(event: ILoggingEvent): String = Trace.context().map(_.uow).getOrElse("undefined") -} diff --git a/kamon-trace/src/test/resources/application.conf b/kamon-trace/src/test/resources/application.conf deleted file mode 100644 index e8217fc2..00000000 --- a/kamon-trace/src/test/resources/application.conf +++ /dev/null @@ -1,3 +0,0 @@ -akka { - loggers = ["akka.event.slf4j.Slf4jLogger"] -} \ No newline at end of file diff --git a/kamon-trace/src/test/resources/logback.xml b/kamon-trace/src/test/resources/logback.xml deleted file mode 100644 index 2ae1e3bd..00000000 --- a/kamon-trace/src/test/resources/logback.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - %date{HH:mm:ss.SSS} %-5level [%X{uow}][%X{requestId}] [%thread] %logger{55} - %msg%n - - - - - - - - diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorLoggingSpec.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorLoggingSpec.scala deleted file mode 100644 index 89742651..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorLoggingSpec.scala +++ /dev/null @@ -1,51 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import akka.testkit.TestKit -import org.scalatest.{ Inspectors, Matchers, WordSpecLike } -import akka.actor.{ Props, ActorLogging, Actor, ActorSystem } -import akka.event.Logging.{ LogEvent } -import kamon.trace.{ ContextAware, TraceContext, Trace } - -class ActorLoggingSpec extends TestKit(ActorSystem("actor-logging-spec")) with WordSpecLike with Matchers with Inspectors { - - "the ActorLogging instrumentation" should { - "attach the TraceContext (if available) to log events" in { - val testTraceContext = Some(TraceContext(Actor.noSender, 1)) - val loggerActor = system.actorOf(Props[LoggerActor]) - system.eventStream.subscribe(testActor, classOf[LogEvent]) - - Trace.withContext(testTraceContext) { - loggerActor ! "info" - } - - fishForMessage() { - case event: LogEvent if event.message.toString contains "TraceContext =>" ⇒ - val ctxInEvent = event.asInstanceOf[ContextAware].traceContext - ctxInEvent === testTraceContext - - case event: LogEvent ⇒ false - } - } - } -} - -class LoggerActor extends Actor with ActorLogging { - def receive = { - case "info" ⇒ log.info("TraceContext => {}", Trace.context()) - } -} diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorMessagePassingTracingSpec.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorMessagePassingTracingSpec.scala deleted file mode 100644 index 89251bf4..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorMessagePassingTracingSpec.scala +++ /dev/null @@ -1,84 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import org.scalatest.{ WordSpecLike, Matchers } -import akka.actor.{ ActorRef, Actor, Props, ActorSystem } - -import akka.testkit.{ ImplicitSender, TestKit } -import kamon.trace.Trace -import akka.pattern.{ pipe, ask } -import akka.util.Timeout -import scala.concurrent.duration._ -import scala.concurrent.{ Await, Future } -import akka.routing.RoundRobinRouter -import kamon.trace.TraceContext - -class ActorMessagePassingTracingSpec extends TestKit(ActorSystem("actor-message-passing-tracing-spec")) with WordSpecLike with ImplicitSender { - implicit val executionContext = system.dispatcher - - "the message passing instrumentation" should { - "propagate the TraceContext using bang" in new TraceContextEchoFixture { - Trace.withContext(testTraceContext) { - ctxEchoActor ! "test" - } - - expectMsg(testTraceContext) - } - - "propagate the TraceContext using tell" in new TraceContextEchoFixture { - Trace.withContext(testTraceContext) { - ctxEchoActor.tell("test", testActor) - } - - expectMsg(testTraceContext) - } - - "propagate the TraceContext using ask" in new TraceContextEchoFixture { - implicit val timeout = Timeout(1 seconds) - Trace.withContext(testTraceContext) { - // The pipe pattern use Futures internally, so FutureTracing test should cover the underpinnings of it. - (ctxEchoActor ? "test") pipeTo (testActor) - } - - expectMsg(testTraceContext) - } - - "propagate the TraceContext to actors behind a router" in new RoutedTraceContextEchoFixture { - Trace.withContext(testTraceContext) { - ctxEchoActor ! "test" - } - - expectMsg(testTraceContext) - } - } - - trait TraceContextEchoFixture { - val testTraceContext = Some(Trace.newTraceContext("")) - val ctxEchoActor = system.actorOf(Props[TraceContextEcho]) - } - - trait RoutedTraceContextEchoFixture extends TraceContextEchoFixture { - override val ctxEchoActor = system.actorOf(Props[TraceContextEcho].withRouter(RoundRobinRouter(nrOfInstances = 1))) - } -} - -class TraceContextEcho extends Actor { - def receive = { - case msg: String ⇒ sender ! Trace.context() - } -} - diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorSystemMessagePassingInstrumentationSpec.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorSystemMessagePassingInstrumentationSpec.scala deleted file mode 100644 index 7d539370..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/ActorSystemMessagePassingInstrumentationSpec.scala +++ /dev/null @@ -1,165 +0,0 @@ -package kamon.trace.instrumentation - -import akka.testkit.{ ImplicitSender, TestKit } -import akka.actor._ -import org.scalatest.WordSpecLike -import kamon.trace.Trace -import scala.util.control.NonFatal -import akka.actor.SupervisorStrategy.{ Escalate, Stop, Restart, Resume } -import scala.concurrent.duration._ - -class ActorSystemMessagePassingInstrumentationSpec extends TestKit(ActorSystem("actor-message-passing-tracing-spec")) with WordSpecLike with ImplicitSender { - implicit val executionContext = system.dispatcher - - "the system message passing instrumentation" should { - "keep the TraceContext while processing the Create message in top level actors" in new TraceContextFixture { - Trace.withContext(testTraceContext) { - system.actorOf(Props(new Actor { - - testActor ! Trace.context() - - def receive: Actor.Receive = { case any ⇒ } - })) - } - - expectMsg(testTraceContext) - } - - "keep the TraceContext while processing the Create message in non top level actors" in new TraceContextFixture { - Trace.withContext(testTraceContext) { - system.actorOf(Props(new Actor { - def receive: Actor.Receive = { - case any ⇒ - context.actorOf(Props(new Actor { - - testActor ! Trace.context() - - def receive: Actor.Receive = { case any ⇒ } - })) - } - })) ! "any" - } - - expectMsg(testTraceContext) - } - - "keep the TraceContext in the supervision cycle" when { - "the actor is resumed" in new TraceContextFixture { - val supervisor = supervisorWithDirective(Resume) - - Trace.withContext(testTraceContext) { - supervisor ! "fail" - } - - expectMsg(testTraceContext) // From the parent executing the supervision strategy - - // Ensure we didn't tie the actor with the context - supervisor ! "context" - expectMsg(None) - } - - "the actor is restarted" in new TraceContextFixture { - val supervisor = supervisorWithDirective(Restart, sendPreRestart = true, sendPostRestart = true) - - Trace.withContext(testTraceContext) { - supervisor ! "fail" - } - - expectMsg(testTraceContext) // From the parent executing the supervision strategy - expectMsg(testTraceContext) // From the preRestart hook - expectMsg(testTraceContext) // From the postRestart hook - - // Ensure we didn't tie the actor with the context - supervisor ! "context" - expectMsg(None) - } - - "the actor is stopped" in new TraceContextFixture { - val supervisor = supervisorWithDirective(Stop, sendPostStop = true) - - Trace.withContext(testTraceContext) { - supervisor ! "fail" - } - - expectMsg(testTraceContext) // From the parent executing the supervision strategy - expectMsg(testTraceContext) // From the postStop hook - expectNoMsg(1 second) - } - - "the failure is escalated" in new TraceContextFixture { - val supervisor = supervisorWithDirective(Escalate, sendPostStop = true) - - Trace.withContext(testTraceContext) { - supervisor ! "fail" - } - - expectMsg(testTraceContext) // From the parent executing the supervision strategy - expectMsg(testTraceContext) // From the grandparent executing the supervision strategy - expectMsg(testTraceContext) // From the postStop hook in the child - expectMsg(testTraceContext) // From the postStop hook in the parent - expectNoMsg(1 second) - } - } - } - - def supervisorWithDirective(directive: SupervisorStrategy.Directive, sendPreRestart: Boolean = false, sendPostRestart: Boolean = false, - sendPostStop: Boolean = false, sendPreStart: Boolean = false): ActorRef = { - class GrandParent extends Actor { - val child = context.actorOf(Props(new Parent)) - - override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy() { - case NonFatal(throwable) ⇒ testActor ! Trace.context(); Stop - } - - def receive = { - case any ⇒ child forward any - } - } - - class Parent extends Actor { - val child = context.actorOf(Props(new Child)) - - override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy() { - case NonFatal(throwable) ⇒ testActor ! Trace.context(); directive - } - - def receive: Actor.Receive = { - case any ⇒ child forward any - } - - override def postStop(): Unit = { - if (sendPostStop) testActor ! Trace.context() - super.postStop() - } - } - - class Child extends Actor { - def receive = { - case "fail" ⇒ 1 / 0 - case "context" ⇒ sender ! Trace.context() - } - - override def preRestart(reason: Throwable, message: Option[Any]): Unit = { - if (sendPreRestart) testActor ! Trace.context() - super.preRestart(reason, message) - } - - override def postRestart(reason: Throwable): Unit = { - if (sendPostRestart) testActor ! Trace.context() - super.postRestart(reason) - } - - override def postStop(): Unit = { - if (sendPostStop) testActor ! Trace.context() - super.postStop() - } - - override def preStart(): Unit = { - if (sendPreStart) testActor ! Trace.context() - super.preStart() - } - } - - system.actorOf(Props(new GrandParent)) - } -} diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/AskPatternTracingSpec.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/AskPatternTracingSpec.scala deleted file mode 100644 index 9df67391..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/AskPatternTracingSpec.scala +++ /dev/null @@ -1,59 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import akka.testkit.TestKit -import akka.actor.{ Props, Actor, ActorSystem } -import org.scalatest.{ Matchers, WordSpecLike } -import akka.event.Logging.Warning -import scala.concurrent.duration._ -import akka.pattern.ask -import akka.util.Timeout -import kamon.trace.{ Trace, ContextAware } -import org.scalatest.OptionValues._ - -class AskPatternTracingSpec extends TestKit(ActorSystem("ask-pattern-tracing-spec")) with WordSpecLike with Matchers { - - "the AskPatternTracing" should { - "log a warning with a stack trace and TraceContext taken from the moment the ask was triggered" in new TraceContextFixture { - implicit val ec = system.dispatcher - implicit val timeout = Timeout(10 milliseconds) - val noReply = system.actorOf(Props[NoReply]) - system.eventStream.subscribe(testActor, classOf[Warning]) - - within(500 milliseconds) { - val initialCtx = Trace.withContext(testTraceContext) { - noReply ? "hello" - Trace.context() - } - - val warn = expectMsgPF() { - case warn: Warning if warn.message.toString.contains("Timeout triggered for ask pattern") ⇒ warn - } - val capturedCtx = warn.asInstanceOf[ContextAware].traceContext - - capturedCtx should be('defined) - capturedCtx should equal(initialCtx) - } - } - } -} - -class NoReply extends Actor { - def receive = { - case any ⇒ - } -} diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/FutureTracingSpec.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/FutureTracingSpec.scala deleted file mode 100644 index a5554836..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/FutureTracingSpec.scala +++ /dev/null @@ -1,62 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import scala.concurrent.{ ExecutionContext, Await, Promise, Future } -import org.scalatest.{ Matchers, OptionValues, WordSpec } -import org.scalatest.concurrent.{ ScalaFutures, PatienceConfiguration } -import java.util.UUID -import scala.util.{ Random, Success } -import scala.concurrent.duration._ -import java.util.concurrent.TimeUnit -import akka.actor.{ Actor, ActorSystem } -import kamon.trace.{ Trace, TraceContext } - -class FutureTracingSpec extends WordSpec with Matchers with ScalaFutures with PatienceConfiguration with OptionValues { - - implicit val execContext = ExecutionContext.Implicits.global - - "a Future created with FutureTracing" should { - "capture the TraceContext available when created" which { - "must be available when executing the future's body" in new TraceContextFixture { - var future: Future[Option[TraceContext]] = _ - - Trace.withContext(testTraceContext) { - future = Future(Trace.context) - } - - whenReady(future)(ctxInFuture ⇒ - ctxInFuture should equal(testTraceContext)) - } - - "must be available when executing callbacks on the future" in new TraceContextFixture { - var future: Future[Option[TraceContext]] = _ - - Trace.withContext(testTraceContext) { - future = Future("Hello Kamon!") - // The TraceContext is expected to be available during all intermediate processing. - .map(_.length) - .flatMap(len ⇒ Future(len.toString)) - .map(s ⇒ Trace.context()) - } - - whenReady(future)(ctxInFuture ⇒ - ctxInFuture should equal(testTraceContext)) - } - } - } -} - diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceAggregatorSpec.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceAggregatorSpec.scala deleted file mode 100644 index 3b32f3ac..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceAggregatorSpec.scala +++ /dev/null @@ -1,51 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 kamon.trace.instrumentation - -import org.scalatest.{ WordSpecLike, WordSpec } -import akka.testkit.{ TestKitBase, TestKit } -import akka.actor.ActorSystem -import scala.concurrent.duration._ -import kamon.trace.UowTracing.{ Finish, Rename, Start } -import kamon.trace.{ UowTrace, UowTraceAggregator } - -class TraceAggregatorSpec extends TestKit(ActorSystem("TraceAggregatorSpec")) with WordSpecLike { - - "a TraceAggregator" should { - "send a UowTrace message out after receiving a Finish message" in new AggregatorFixture { - within(1 second) { - aggregator ! Start(1, "/accounts") - aggregator ! Finish(1) - - //expectMsg(UowTrace("UNKNOWN", Seq(Start(1, "/accounts"), Finish(1)))) - } - } - - "change the uow name after receiving a Rename message" in new AggregatorFixture { - within(1 second) { - aggregator ! Start(1, "/accounts") - aggregator ! Rename(1, "test-uow") - aggregator ! Finish(1) - - //expectMsg(UowTrace("test-uow", Seq(Start(1, "/accounts"), Finish(1)))) - } - } - } - - trait AggregatorFixture { - val aggregator = system.actorOf(UowTraceAggregator.props(testActor, 10 seconds)) - } -} diff --git a/kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceContextFixture.scala b/kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceContextFixture.scala deleted file mode 100644 index 62f7ec84..00000000 --- a/kamon-trace/src/test/scala/kamon/trace/instrumentation/TraceContextFixture.scala +++ /dev/null @@ -1,10 +0,0 @@ -package kamon.trace.instrumentation - -import scala.util.Random -import kamon.trace.TraceContext -import akka.actor.Actor - -trait TraceContextFixture { - val random = new Random(System.nanoTime) - val testTraceContext = Some(TraceContext(Actor.noSender, random.nextInt)) -} \ No newline at end of file -- cgit v1.2.3