diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2015-08-28 13:02:09 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2015-08-28 13:02:09 +0200 |
commit | 110fff9745a0c1f154ee3d7a5149cd9f162a879f (patch) | |
tree | 689dc6f94cc73f7156569bf6cb69c44cda4c2c8c /kamon-spray/src/main/scala/kamon/spray | |
parent | aa1c38de58d692f90275867fdfda437b99bd8dcc (diff) | |
download | Kamon-110fff9745a0c1f154ee3d7a5149cd9f162a879f.tar.gz Kamon-110fff9745a0c1f154ee3d7a5149cd9f162a879f.tar.bz2 Kamon-110fff9745a0c1f154ee3d7a5149cd9f162a879f.zip |
wip, core/akka/spray are kind of migrated.
Diffstat (limited to 'kamon-spray/src/main/scala/kamon/spray')
4 files changed, 33 insertions, 53 deletions
diff --git a/kamon-spray/src/main/scala/kamon/spray/SprayExtension.scala b/kamon-spray/src/main/scala/kamon/spray/SprayExtension.scala index 044d2884..10f44bbe 100644 --- a/kamon-spray/src/main/scala/kamon/spray/SprayExtension.scala +++ b/kamon-spray/src/main/scala/kamon/spray/SprayExtension.scala @@ -22,28 +22,14 @@ import akka.event.{ Logging, LoggingAdapter } import kamon.Kamon import kamon.util.http.HttpServerMetrics import kamon.metric.Entity +import org.slf4j.LoggerFactory import spray.http.HttpHeaders.Host import spray.http.HttpRequest -object Spray extends ExtensionId[SprayExtension] with ExtensionIdProvider { - def lookup(): ExtensionId[_ <: actor.Extension] = Spray - def createExtension(system: ExtendedActorSystem): SprayExtension = new SprayExtensionImpl(system) - +object SprayExtension { + val log = LoggerFactory.getLogger("kamon.spray.SprayExtension") + val settings = SprayExtensionSettings(Kamon.config) val SegmentLibraryName = "spray-client" -} - -trait SprayExtension extends Kamon.Extension { - def settings: SprayExtensionSettings - def log: LoggingAdapter - def httpServerMetrics: HttpServerMetrics - def generateTraceName(request: HttpRequest): String - def generateRequestLevelApiSegmentName(request: HttpRequest): String - def generateHostLevelApiSegmentName(request: HttpRequest): String -} - -class SprayExtensionImpl(system: ExtendedActorSystem) extends SprayExtension { - val settings = SprayExtensionSettings(system) - val log = Logging(system, "SprayExtension") val httpServerMetrics = { val entity = Entity("spray-server", HttpServerMetrics.category) diff --git a/kamon-spray/src/main/scala/kamon/spray/SprayExtensionSettings.scala b/kamon-spray/src/main/scala/kamon/spray/SprayExtensionSettings.scala index 627d5176..9f4e70b3 100644 --- a/kamon-spray/src/main/scala/kamon/spray/SprayExtensionSettings.scala +++ b/kamon-spray/src/main/scala/kamon/spray/SprayExtensionSettings.scala @@ -1,6 +1,7 @@ package kamon.spray -import akka.actor.ExtendedActorSystem +import akka.actor.ReflectiveDynamicAccess +import com.typesafe.config.Config case class SprayExtensionSettings( includeTraceTokenHeader: Boolean, @@ -9,16 +10,17 @@ case class SprayExtensionSettings( clientInstrumentationLevel: ClientInstrumentationLevel.Level) object SprayExtensionSettings { - def apply(system: ExtendedActorSystem): SprayExtensionSettings = { - val config = system.settings.config.getConfig("kamon.spray") + def apply(config: Config): SprayExtensionSettings = { + val sprayConfig = config.getConfig("kamon.spray") - val includeTraceTokenHeader: Boolean = config.getBoolean("automatic-trace-token-propagation") - val traceTokenHeaderName: String = config.getString("trace-token-header-name") + val includeTraceTokenHeader: Boolean = sprayConfig.getBoolean("automatic-trace-token-propagation") + val traceTokenHeaderName: String = sprayConfig.getString("trace-token-header-name") - val nameGeneratorFQN = config.getString("name-generator") - val nameGenerator: NameGenerator = system.dynamicAccess.createInstanceFor[NameGenerator](nameGeneratorFQN, Nil).get // let's bubble up any problems. + val nameGeneratorFQN = sprayConfig.getString("name-generator") + val nameGenerator: NameGenerator = new ReflectiveDynamicAccess(getClass.getClassLoader) + .createInstanceFor[NameGenerator](nameGeneratorFQN, Nil).get // let's bubble up any problems. - val clientInstrumentationLevel: ClientInstrumentationLevel.Level = config.getString("client.instrumentation-level") match { + val clientInstrumentationLevel: ClientInstrumentationLevel.Level = sprayConfig.getString("client.instrumentation-level") match { case "request-level" ⇒ ClientInstrumentationLevel.RequestLevelAPI case "host-level" ⇒ ClientInstrumentationLevel.HostLevelAPI case other ⇒ sys.error(s"Invalid client instrumentation level [$other] found in configuration.") diff --git a/kamon-spray/src/main/scala/kamon/spray/instrumentation/ClientRequestInstrumentation.scala b/kamon-spray/src/main/scala/kamon/spray/instrumentation/ClientRequestInstrumentation.scala index 1950787d..cf7e207b 100644 --- a/kamon-spray/src/main/scala/kamon/spray/instrumentation/ClientRequestInstrumentation.scala +++ b/kamon-spray/src/main/scala/kamon/spray/instrumentation/ClientRequestInstrumentation.scala @@ -16,14 +16,13 @@ package spray.can.client -import kamon.Kamon import kamon.util.SameThreadExecutionContext import org.aspectj.lang.annotation._ import org.aspectj.lang.ProceedingJoinPoint import spray.http._ import spray.http.HttpHeaders.RawHeader import kamon.trace._ -import kamon.spray.{ ClientInstrumentationLevel, Spray } +import kamon.spray.{ SprayExtension, ClientInstrumentationLevel } import akka.actor.{ ActorRef, Status } import scala.concurrent.{ Future, ExecutionContext } import akka.util.Timeout @@ -49,12 +48,10 @@ class ClientRequestInstrumentation { requestContext.traceContext Tracer.currentContext.collect { ctx ⇒ - val sprayExtension = Kamon.extension(Spray) - - if (sprayExtension.settings.clientInstrumentationLevel == ClientInstrumentationLevel.HostLevelAPI) { + if (SprayExtension.settings.clientInstrumentationLevel == ClientInstrumentationLevel.HostLevelAPI) { if (requestContext.segment.isEmpty) { - val clientRequestName = sprayExtension.generateHostLevelApiSegmentName(request) - val segment = ctx.startSegment(clientRequestName, SegmentCategory.HttpClient, Spray.SegmentLibraryName) + val clientRequestName = SprayExtension.generateHostLevelApiSegmentName(request) + val segment = ctx.startSegment(clientRequestName, SegmentCategory.HttpClient, SprayExtension.SegmentLibraryName) requestContext.segment = segment } @@ -64,7 +61,7 @@ class ClientRequestInstrumentation { // name again here is that when the request was initially sent it might not have the Host information available // and it might be important to decide a proper segment name. - val clientRequestName = sprayExtension.generateHostLevelApiSegmentName(request) + val clientRequestName = SprayExtension.generateHostLevelApiSegmentName(request) request.asInstanceOf[SegmentAware].segment.rename(clientRequestName) } } @@ -114,10 +111,9 @@ class ClientRequestInstrumentation { (request: HttpRequest) ⇒ { Tracer.currentContext.collect { ctx ⇒ - val sprayExtension = Kamon.extension(Spray) val segment = - if (sprayExtension.settings.clientInstrumentationLevel == ClientInstrumentationLevel.RequestLevelAPI) - ctx.startSegment(sprayExtension.generateRequestLevelApiSegmentName(request), SegmentCategory.HttpClient, Spray.SegmentLibraryName) + if (SprayExtension.settings.clientInstrumentationLevel == ClientInstrumentationLevel.RequestLevelAPI) + ctx.startSegment(SprayExtension.generateRequestLevelApiSegmentName(request), SegmentCategory.HttpClient, SprayExtension.SegmentLibraryName) else EmptyTraceContext.EmptySegment @@ -138,9 +134,8 @@ class ClientRequestInstrumentation { def aroundIncludingDefaultHeadersAtHttpHostConnector(pjp: ProceedingJoinPoint, request: HttpMessage, defaultHeaders: List[HttpHeader]): Any = { val modifiedHeaders = Tracer.currentContext.collect { ctx ⇒ - val sprayExtension = Kamon.extension(Spray) - if (sprayExtension.settings.includeTraceTokenHeader) - RawHeader(sprayExtension.settings.traceTokenHeaderName, ctx.token) :: defaultHeaders + if (SprayExtension.settings.includeTraceTokenHeader) + RawHeader(SprayExtension.settings.traceTokenHeaderName, ctx.token) :: defaultHeaders else defaultHeaders diff --git a/kamon-spray/src/main/scala/kamon/spray/instrumentation/ServerRequestInstrumentation.scala b/kamon-spray/src/main/scala/kamon/spray/instrumentation/ServerRequestInstrumentation.scala index d773ecd8..d1031962 100644 --- a/kamon-spray/src/main/scala/kamon/spray/instrumentation/ServerRequestInstrumentation.scala +++ b/kamon-spray/src/main/scala/kamon/spray/instrumentation/ServerRequestInstrumentation.scala @@ -19,7 +19,7 @@ import org.aspectj.lang.annotation._ import kamon.trace._ import spray.http.{ HttpResponse, HttpMessagePartWrapper, HttpRequest } import kamon.Kamon -import kamon.spray.{ SprayExtension, Spray } +import kamon.spray.SprayExtension import org.aspectj.lang.ProceedingJoinPoint import spray.http.HttpHeaders.RawHeader @@ -35,11 +35,10 @@ class ServerRequestInstrumentation { @After("openRequestInit(openRequest, request)") def afterInit(openRequest: TraceContextAware, request: HttpRequest): Unit = { import Kamon.tracer - val sprayExtension = Kamon(Spray) - val defaultTraceName = sprayExtension.generateTraceName(request) - val token = if (sprayExtension.settings.includeTraceTokenHeader) { - request.headers.find(_.name == sprayExtension.settings.traceTokenHeaderName).map(_.value) + val defaultTraceName = SprayExtension.generateTraceName(request) + val token = if (SprayExtension.settings.includeTraceTokenHeader) { + request.headers.find(_.name == SprayExtension.settings.traceTokenHeaderName).map(_.value) } else None val newContext = tracer.newContext(defaultTraceName, token) @@ -71,17 +70,15 @@ class ServerRequestInstrumentation { if (incomingContext.isEmpty) pjp.proceed() else { - val sprayExtension = Kamon(Spray) - - val proceedResult = if (sprayExtension.settings.includeTraceTokenHeader) { - val responseWithHeader = includeTraceTokenIfPossible(response, sprayExtension.settings.traceTokenHeaderName, incomingContext.token) + val proceedResult = if (SprayExtension.settings.includeTraceTokenHeader) { + val responseWithHeader = includeTraceTokenIfPossible(response, SprayExtension.settings.traceTokenHeaderName, incomingContext.token) pjp.proceed(Array(openRequest, responseWithHeader)) } else pjp.proceed Tracer.currentContext.finish() - recordHttpServerMetrics(response, incomingContext.name, sprayExtension) + recordHttpServerMetrics(response, incomingContext.name) proceedResult } @@ -89,7 +86,7 @@ class ServerRequestInstrumentation { def verifyTraceContextConsistency(incomingTraceContext: TraceContext, storedTraceContext: TraceContext): Unit = { def publishWarning(text: String): Unit = - Kamon(Spray).log.warning(text) + SprayExtension.log.warn(text) if (incomingTraceContext.nonEmpty) { if (incomingTraceContext.token != storedTraceContext.token) @@ -98,9 +95,9 @@ class ServerRequestInstrumentation { publishWarning(s"EmptyTraceContext present while closing the trace with token [${storedTraceContext.token}]") } - def recordHttpServerMetrics(response: HttpMessagePartWrapper, traceName: String, sprayExtension: SprayExtension): Unit = + def recordHttpServerMetrics(response: HttpMessagePartWrapper, traceName: String): Unit = response match { - case httpResponse: HttpResponse ⇒ sprayExtension.httpServerMetrics.recordResponse(traceName, httpResponse.status.intValue.toString) + case httpResponse: HttpResponse ⇒ SprayExtension.httpServerMetrics.recordResponse(traceName, httpResponse.status.intValue.toString) case other ⇒ // Nothing to do then. } |