|
| 1 | +/* |
| 2 | + * ========================================================================================= |
| 3 | + * Copyright © 2013-2015 the kamon project <http://kamon.io/> |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 6 | + * except in compliance with the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 11 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 12 | + * either express or implied. See the License for the specific language governing permissions |
| 13 | + * and limitations under the License. |
| 14 | + * ========================================================================================= |
| 15 | + */ |
| 16 | + |
| 17 | +package kamon.play |
| 18 | + |
| 19 | +import akka.actor._ |
| 20 | +import kamon.Kamon |
| 21 | +import kamon.util.http.HttpServerMetrics |
| 22 | +import kamon.util.logger.LazyLogger |
| 23 | +import play.api.libs.ws.WSRequest |
| 24 | +import play.api.mvc.RequestHeader |
| 25 | + |
| 26 | +object PlayExtension { |
| 27 | + val SegmentLibraryName = "WS-client" |
| 28 | + |
| 29 | + val log = LazyLogger("kamon.play.PlayExtension") |
| 30 | + |
| 31 | + private val config = Kamon.config.getConfig("kamon.play") |
| 32 | + private val dynamic = new ReflectiveDynamicAccess(getClass.getClassLoader) |
| 33 | + val httpServerMetrics = Kamon.metrics.entity(HttpServerMetrics, "play-server") |
| 34 | + |
| 35 | + val includeTraceToken: Boolean = config.getBoolean("automatic-trace-token-propagation") |
| 36 | + val traceTokenHeaderName: String = config.getString("trace-token-header-name") |
| 37 | + |
| 38 | + private val nameGeneratorFQN = config.getString("name-generator") |
| 39 | + private val nameGenerator: NameGenerator = dynamic.createInstanceFor[NameGenerator](nameGeneratorFQN, Nil).get |
| 40 | + |
| 41 | + def generateTraceName(requestHeader: RequestHeader): String = nameGenerator.generateTraceName(requestHeader) |
| 42 | + def generateHttpClientSegmentName(request: WSRequest): String = nameGenerator.generateHttpClientSegmentName(request) |
| 43 | +} |
| 44 | + |
| 45 | +trait NameGenerator { |
| 46 | + def generateTraceName(requestHeader: RequestHeader): String |
| 47 | + def generateHttpClientSegmentName(request: WSRequest): String |
| 48 | +} |
| 49 | + |
| 50 | +class DefaultNameGenerator extends NameGenerator { |
| 51 | + import scala.collection.concurrent.TrieMap |
| 52 | + import play.api.routing.Router |
| 53 | + import java.util.Locale |
| 54 | + import kamon.util.TriemapAtomicGetOrElseUpdate.Syntax |
| 55 | + |
| 56 | + private val cache = TrieMap.empty[String, String] |
| 57 | + private val normalizePattern = """\$([^<]+)<[^>]+>""".r |
| 58 | + |
| 59 | + def generateTraceName(requestHeader: RequestHeader): String = requestHeader.tags.get(Router.Tags.RouteVerb).map { verb ⇒ |
| 60 | + val path = requestHeader.tags(Router.Tags.RoutePattern) |
| 61 | + cache.atomicGetOrElseUpdate(s"$verb$path", { |
| 62 | + val traceName = { |
| 63 | + // Convert paths of form GET /foo/bar/$paramname<regexp>/blah to foo.bar.paramname.blah.get |
| 64 | + val p = normalizePattern.replaceAllIn(path, "$1").replace('/', '.').dropWhile(_ == '.') |
| 65 | + val normalisedPath = { |
| 66 | + if (p.lastOption.exists(_ != '.')) s"$p." |
| 67 | + else p |
| 68 | + } |
| 69 | + s"$normalisedPath${verb.toLowerCase(Locale.ENGLISH)}" |
| 70 | + } |
| 71 | + traceName |
| 72 | + }) |
| 73 | + } getOrElse "UntaggedTrace" |
| 74 | + |
| 75 | + def generateHttpClientSegmentName(request: WSRequest): String = request.uri.getAuthority |
| 76 | +} |
0 commit comments