diff options
Diffstat (limited to 'kamon-play24/src/main/scala/kamon/play/instrumentation')
3 files changed, 175 insertions, 0 deletions
diff --git a/kamon-play24/src/main/scala/kamon/play/instrumentation/LoggerLikeInstrumentation.scala b/kamon-play24/src/main/scala/kamon/play/instrumentation/LoggerLikeInstrumentation.scala new file mode 100644 index 00000000..3c79fae4 --- /dev/null +++ b/kamon-play24/src/main/scala/kamon/play/instrumentation/LoggerLikeInstrumentation.scala @@ -0,0 +1,42 @@ +/* ========================================================================================= + * Copyright © 2013-2014 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 kamon.play.instrumentation + +import kamon.trace.logging.MdcKeysSupport +import org.aspectj.lang.ProceedingJoinPoint +import org.aspectj.lang.annotation._ + +@Aspect +class LoggerLikeInstrumentation extends MdcKeysSupport { + + @Pointcut("execution(* play.api.LoggerLike+.info(..))") + def infoPointcut(): Unit = {} + + @Pointcut("execution(* play.api.LoggerLike+.warn(..))") + def warnPointcut(): Unit = {} + + @Pointcut("execution(* play.api.LoggerLike+.error(..))") + def errorPointcut(): Unit = {} + + @Pointcut("execution(* play.api.LoggerLike+.trace(..))") + def tracePointcut(): Unit = {} + + @Around("(infoPointcut() || warnPointcut() || errorPointcut() || tracePointcut())") + def aroundLog(pjp: ProceedingJoinPoint): Any = withMdc { + pjp.proceed() + } +} + diff --git a/kamon-play24/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala b/kamon-play24/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala new file mode 100644 index 00000000..7bf6d0d0 --- /dev/null +++ b/kamon-play24/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala @@ -0,0 +1,86 @@ +/* ========================================================================================= + * Copyright © 2013-2015 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 kamon.play.instrumentation + +import kamon.Kamon +import kamon.Kamon.tracer +import kamon.play.Play +import kamon.trace._ +import kamon.util.SameThreadExecutionContext +import org.aspectj.lang.ProceedingJoinPoint +import org.aspectj.lang.annotation._ +import play.api.mvc.Results._ +import play.api.mvc._ + +@Aspect +class RequestInstrumentation { + + @DeclareMixin("play.api.mvc.RequestHeader+") + def mixinContextAwareNewRequest: TraceContextAware = TraceContextAware.default + + @Before("call(* play.api.http.DefaultHttpRequestHandler.routeRequest(..)) && args(requestHeader)") + def beforeRouteRequest(requestHeader: RequestHeader): Unit = { + val playExtension = Kamon(Play) + + val token = if (playExtension.includeTraceToken) { + requestHeader.headers.get(playExtension.traceTokenHeaderName) + } else None + + Tracer.setCurrentContext(tracer.newContext("UnnamedTrace", token)) + } + + @Around("call(* play.api.http.HttpFilters.filters(..))") + def filters(pjp: ProceedingJoinPoint): Any = { + val filter = new EssentialFilter { + def apply(next: EssentialAction): EssentialAction = { + val action = (requestHeader: RequestHeader) ⇒ { + val playExtension = Kamon(Play) + + def onResult(result: Result): Result = { + Tracer.currentContext.collect { ctx ⇒ + ctx.finish() + playExtension.httpServerMetrics.recordResponse(ctx.name, result.header.status.toString) + + if (playExtension.includeTraceToken) result.withHeaders(playExtension.traceTokenHeaderName -> ctx.token) + else result + + } getOrElse result + } + //override the current trace name + Tracer.currentContext.rename(playExtension.generateTraceName(requestHeader)) + // Invoke the action + next(requestHeader).map(onResult)(SameThreadExecutionContext) + } + EssentialAction(action) + } + } + pjp.proceed().asInstanceOf[Seq[EssentialFilter]] :+ filter + } + + @Before("call(* play.api.http.HttpErrorHandler.onClientServerError(..)) && args(requestContextAware, statusCode, *)") + def onClientError(requestContextAware: TraceContextAware, statusCode: Int): Unit = { + requestContextAware.traceContext.collect { ctx ⇒ + Kamon(Play).httpServerMetrics.recordResponse(ctx.name, statusCode.toString) + } + } + + @Before("call(* play.api.http.HttpErrorHandler.onServerError(..)) && args(requestContextAware, ex)") + def onServerError(requestContextAware: TraceContextAware, ex: Throwable): Unit = { + requestContextAware.traceContext.collect { ctx ⇒ + Kamon(Play).httpServerMetrics.recordResponse(ctx.name, InternalServerError.header.status.toString) + } + } +} diff --git a/kamon-play24/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala b/kamon-play24/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala new file mode 100644 index 00000000..93f6d95a --- /dev/null +++ b/kamon-play24/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala @@ -0,0 +1,47 @@ +/* =================================================== + * Copyright © 2013-2014 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 kamon.play.instrumentation + +import kamon.Kamon +import kamon.play.Play +import kamon.trace.{ Tracer, SegmentCategory } +import kamon.util.SameThreadExecutionContext +import org.aspectj.lang.ProceedingJoinPoint +import org.aspectj.lang.annotation.{ Around, Aspect, Pointcut } +import play.api.libs.ws.{ WSRequest, WSResponse } +import scala.concurrent.Future +import scala.util.Try + +@Aspect +class WSInstrumentation { + + @Pointcut("execution(* play.api.libs.ws.ning.NingWSRequest.execute()) && this(request)") + def onExecuteRequest(request: WSRequest): Unit = {} + + @Around("onExecuteRequest(request)") + def aroundExecuteRequest(pjp: ProceedingJoinPoint, request: WSRequest): Any = { + Tracer.currentContext.collect { ctx ⇒ + val playExtension = Kamon(Play) + val segmentName = playExtension.generateHttpClientSegmentName(request) + val segment = ctx.startSegment(segmentName, SegmentCategory.HttpClient, Play.SegmentLibraryName) + val response = pjp.proceed().asInstanceOf[Future[WSResponse]] + + response.onComplete(result ⇒ segment.finish())(SameThreadExecutionContext) + response + } getOrElse pjp.proceed() + } +}
\ No newline at end of file |