diff options
Diffstat (limited to 'kamon-spray/src/main')
-rw-r--r-- | kamon-spray/src/main/scala/kamon/spray/Spray.scala | 4 | ||||
-rw-r--r-- | kamon-spray/src/main/scala/spray/can/server/ServerRequestInstrumentation.scala | 19 |
2 files changed, 17 insertions, 6 deletions
diff --git a/kamon-spray/src/main/scala/kamon/spray/Spray.scala b/kamon-spray/src/main/scala/kamon/spray/Spray.scala index 9de1882a..76adb214 100644 --- a/kamon-spray/src/main/scala/kamon/spray/Spray.scala +++ b/kamon-spray/src/main/scala/kamon/spray/Spray.scala @@ -19,6 +19,8 @@ package kamon.spray import akka.actor.{ ExtendedActorSystem, ExtensionIdProvider, ExtensionId } import akka.actor import kamon.Kamon +import kamon.http.HttpServerMetrics +import kamon.metric.Metrics import spray.http.HttpRequest object Spray extends ExtensionId[SprayExtension] with ExtensionIdProvider { @@ -38,6 +40,8 @@ class SprayExtension(private val system: ExtendedActorSystem) extends Kamon.Exte val includeTraceToken: Boolean = config.getBoolean("automatic-trace-token-propagation") val traceTokenHeaderName: String = config.getString("trace-token-header-name") + val httpServerMetrics = Kamon(Metrics)(system).register(HttpServerMetrics, HttpServerMetrics.Factory).get + // It's safe to assume that HttpServerMetrics will always exist because there is no particular filter for it. val clientSegmentCollectionStrategy: ClientSegmentCollectionStrategy.Strategy = config.getString("client.segment-collection-strategy") match { diff --git a/kamon-spray/src/main/scala/spray/can/server/ServerRequestInstrumentation.scala b/kamon-spray/src/main/scala/spray/can/server/ServerRequestInstrumentation.scala index 85782e22..69b0160e 100644 --- a/kamon-spray/src/main/scala/spray/can/server/ServerRequestInstrumentation.scala +++ b/kamon-spray/src/main/scala/spray/can/server/ServerRequestInstrumentation.scala @@ -22,7 +22,7 @@ import spray.http.{ HttpResponse, HttpMessagePartWrapper, HttpRequest } import akka.event.Logging.Warning import scala.Some import kamon.Kamon -import kamon.spray.Spray +import kamon.spray.{ SprayExtension, Spray } import org.aspectj.lang.ProceedingJoinPoint import spray.http.HttpHeaders.RawHeader @@ -68,20 +68,21 @@ class ServerRequestInstrumentation { val storedContext = openRequest.traceContext verifyTraceContextConsistency(incomingContext, storedContext) - val proceedResult = incomingContext match { + incomingContext match { case None ⇒ pjp.proceed() case Some(traceContext) ⇒ val sprayExtension = Kamon(Spray)(traceContext.system) - if (sprayExtension.includeTraceToken) { + val proceedResult = if (sprayExtension.includeTraceToken) { val responseWithHeader = includeTraceTokenIfPossible(response, sprayExtension.traceTokenHeaderName, traceContext.token) pjp.proceed(Array(openRequest, responseWithHeader)) } else pjp.proceed - } - TraceRecorder.finish() - proceedResult + TraceRecorder.finish() + recordHttpServerMetrics(response, traceContext.name, sprayExtension) + proceedResult + } } def verifyTraceContextConsistency(incomingTraceContext: Option[TraceContext], storedTraceContext: Option[TraceContext]): Unit = { @@ -102,6 +103,12 @@ class ServerRequestInstrumentation { } + def recordHttpServerMetrics(response: HttpMessagePartWrapper, traceName: String, sprayExtension: SprayExtension): Unit = + response match { + case httpResponse: HttpResponse ⇒ sprayExtension.httpServerMetrics.recordResponse(traceName, httpResponse.status.intValue.toString) + case other ⇒ // Nothing to do then. + } + def includeTraceTokenIfPossible(response: HttpMessagePartWrapper, traceTokenHeaderName: String, token: String): HttpMessagePartWrapper = response match { case response: HttpResponse ⇒ response.withHeaders(response.headers ::: RawHeader(traceTokenHeaderName, token) :: Nil) |