diff options
Diffstat (limited to 'kamon-play/src')
4 files changed, 38 insertions, 9 deletions
diff --git a/kamon-play/src/main/resources/reference.conf b/kamon-play/src/main/resources/reference.conf index 72266a0c..5ad070ce 100644 --- a/kamon-play/src/main/resources/reference.conf +++ b/kamon-play/src/main/resources/reference.conf @@ -3,14 +3,24 @@ # ================================== # kamon { - metrics { - tick-interval = 1 hour - } - play { - include-trace-token-header = true + + # Header name used when propagating the `TraceContext.token` value across applications. trace-token-header-name = "X-Trace-Token" + # When set to true, Kamon will automatically set and propogate the `TraceContext.token` value under the following + # conditions: + # - When a server side request is received containing the trace token header, the new `TraceContext` will have that + # some token, and once the response to that request is ready, the trace token header is also included in the + # response. + # - When a WS client request is issued and a `TraceContext` is available, the trace token header will be included + # in the request headers. + automatic-trace-token-propagation = true + + # Fully qualified name of the implementation of kamon.play.PlayNameGenerator that will be used for assigning names + # to traces and client http segments. + name-generator = kamon.play.DefaultPlayNameGenerator + dispatcher = ${kamon.default-dispatcher} } }
\ No newline at end of file diff --git a/kamon-play/src/main/scala/kamon/play/Play.scala b/kamon-play/src/main/scala/kamon/play/Play.scala index 7b8777e0..6e2de3c1 100644 --- a/kamon-play/src/main/scala/kamon/play/Play.scala +++ b/kamon-play/src/main/scala/kamon/play/Play.scala @@ -21,6 +21,8 @@ import akka.event.Logging import kamon.Kamon import kamon.http.HttpServerMetrics import kamon.metric.Metrics +import play.api.libs.ws.WSRequest +import play.api.mvc.RequestHeader object Play extends ExtensionId[PlayExtension] with ExtensionIdProvider { override def lookup(): ExtensionId[_ <: Extension] = Play @@ -35,7 +37,22 @@ class PlayExtension(private val system: ExtendedActorSystem) extends Kamon.Exten val httpServerMetrics = Kamon(Metrics)(system).register(HttpServerMetrics, HttpServerMetrics.Factory).get val defaultDispatcher = system.dispatchers.lookup(config.getString("dispatcher")) - val includeTraceToken: Boolean = config.getBoolean("include-trace-token-header") + 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: PlayNameGenerator = system.dynamicAccess.createInstanceFor[PlayNameGenerator](nameGeneratorFQN, Nil).get + + def generateTraceName(requestHeader: RequestHeader): String = nameGenerator.generateTraceName(requestHeader) + def generateHttpClientSegmentName(request: WSRequest): String = nameGenerator.generateHttpClientSegmentName(request) } +trait PlayNameGenerator { + def generateTraceName(requestHeader: RequestHeader): String + def generateHttpClientSegmentName(request: WSRequest): String +} + +class DefaultPlayNameGenerator extends PlayNameGenerator { + def generateTraceName(requestHeader: RequestHeader): String = requestHeader.method + ": " + requestHeader.uri + def generateHttpClientSegmentName(request: WSRequest): String = request.url +} diff --git a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala index 82a43926..ca95781e 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala @@ -42,7 +42,7 @@ class RequestInstrumentation { def beforeRouteRequest(requestHeader: RequestHeader): Unit = { val system = Akka.system() val playExtension = Kamon(Play)(system) - val defaultTraceName: String = s"${requestHeader.method}: ${requestHeader.uri}" + val defaultTraceName = playExtension.generateTraceName(requestHeader) val token = if (playExtension.includeTraceToken) { requestHeader.headers.toSimpleMap.find(_._1 == playExtension.traceTokenHeaderName).map(_._2) diff --git a/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala b/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala index c2eafa2b..125db85e 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala @@ -34,8 +34,10 @@ class WSInstrumentation { @Around("onExecuteRequest(request)") def aroundExecuteRequest(pjp: ProceedingJoinPoint, request: WSRequest): Any = { TraceRecorder.withTraceContextAndSystem { (ctx, system) ⇒ - val executor = Kamon(Play)(system).defaultDispatcher - val segment = ctx.startSegment(request.url, SegmentMetricIdentityLabel.HttpClient) + val playExtension = Kamon(Play)(system) + val executor = playExtension.defaultDispatcher + val segmentName = playExtension.generateHttpClientSegmentName(request) + val segment = ctx.startSegment(segmentName, SegmentMetricIdentityLabel.HttpClient) val response = pjp.proceed().asInstanceOf[Future[WSResponse]] response.map(result ⇒ segment.finish())(executor) |