diff options
Diffstat (limited to 'kamon-play-2.4.x/src/main/scala/kamon/play/Play.scala')
-rw-r--r-- | kamon-play-2.4.x/src/main/scala/kamon/play/Play.scala | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/kamon-play-2.4.x/src/main/scala/kamon/play/Play.scala b/kamon-play-2.4.x/src/main/scala/kamon/play/Play.scala new file mode 100644 index 00000000..4d72f16e --- /dev/null +++ b/kamon-play-2.4.x/src/main/scala/kamon/play/Play.scala @@ -0,0 +1,81 @@ +/* + * ========================================================================================= + * 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 + +import akka.actor.{ ExtendedActorSystem, Extension, ExtensionId, ExtensionIdProvider } +import akka.event.Logging +import kamon.Kamon +import kamon.http.HttpServerMetrics +import play.api.libs.ws.WSRequest +import play.api.mvc.RequestHeader + +object Play extends ExtensionId[PlayExtension] with ExtensionIdProvider { + override def lookup(): ExtensionId[_ <: Extension] = Play + override def createExtension(system: ExtendedActorSystem): PlayExtension = new PlayExtension(system) + + val SegmentLibraryName = "WS-client" +} + +class PlayExtension(private val system: ExtendedActorSystem) extends Kamon.Extension { + val log = Logging(system, classOf[PlayExtension]) + log.info(s"Starting the Kamon(Play) extension") + + private val config = system.settings.config.getConfig("kamon.play") + val httpServerMetrics = Kamon.metrics.entity(HttpServerMetrics, "play-server") + + val includeTraceToken: Boolean = config.getBoolean("automatic-trace-token-propagation") + val traceTokenHeaderName: String = config.getString("trace-token-header-name") + + private val nameGeneratorFQN = config.getString("name-generator") + private val nameGenerator: NameGenerator = system.dynamicAccess.createInstanceFor[NameGenerator](nameGeneratorFQN, Nil).get + + def generateTraceName(requestHeader: RequestHeader): String = nameGenerator.generateTraceName(requestHeader) + def generateHttpClientSegmentName(request: WSRequest): String = nameGenerator.generateHttpClientSegmentName(request) +} + +trait NameGenerator { + def generateTraceName(requestHeader: RequestHeader): String + def generateHttpClientSegmentName(request: WSRequest): String +} + +class DefaultNameGenerator extends NameGenerator { + import scala.collection.concurrent.TrieMap + import play.api.routing.Router + import java.util.Locale + import kamon.util.TriemapAtomicGetOrElseUpdate.Syntax + + private val cache = TrieMap.empty[String, String] + private val normalizePattern = """\$([^<]+)<[^>]+>""".r + + def generateTraceName(requestHeader: RequestHeader): String = requestHeader.tags.get(Router.Tags.RouteVerb).map { verb ⇒ + val path = requestHeader.tags(Router.Tags.RoutePattern) + cache.atomicGetOrElseUpdate(s"$verb$path", { + val traceName = { + // Convert paths of form GET /foo/bar/$paramname<regexp>/blah to foo.bar.paramname.blah.get + val p = normalizePattern.replaceAllIn(path, "$1").replace('/', '.').dropWhile(_ == '.') + val normalisedPath = { + if (p.lastOption.filter(_ != '.').isDefined) s"$p." + else p + } + s"$normalisedPath${verb.toLowerCase(Locale.ENGLISH)}" + } + traceName + }) + } getOrElse s"${requestHeader.method}: ${requestHeader.uri}" + + def generateHttpClientSegmentName(request: WSRequest): String = request.url +} |