-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from drpacman/master
Added Spray client tracing support
- Loading branch information
Showing
5 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
spray/src/main/scala/com/github/levkhomich/akka/tracing/http/TracedSprayPipeline.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.levkhomich.akka.tracing.http | ||
|
||
import spray.http.{ HttpResponse, HttpRequest } | ||
import spray.client.pipelining._ | ||
import akka.actor.ActorSystem | ||
import com.github.levkhomich.akka.tracing._ | ||
|
||
case class TracedClientRequest() extends TracingSupport | ||
|
||
trait TracedSprayPipeline { | ||
|
||
val system: ActorSystem | ||
def sendAndReceive: SendReceive | ||
|
||
implicit lazy val trace: TracingExtensionImpl = TracingExtension(system) | ||
import system.dispatcher | ||
|
||
def tracedPipeline[T](trigger: BaseTracingSupport) = { | ||
val clientReq = TracedClientRequest().asChildOf(trigger) | ||
addHeader("X-B3-TraceId", Span.asString(clientReq.tracingId)) | ||
addHeader("X-B3-Sampled", trace.getId(clientReq.tracingId).isDefined.toString) ~> | ||
startTrace(clientReq) ~> | ||
sendAndReceive ~> | ||
completeTrace(clientReq) | ||
} | ||
|
||
def startTrace(clientReq: BaseTracingSupport)(req: HttpRequest): HttpRequest = { | ||
trace.record(clientReq, thrift.zipkinConstants.CLIENT_SEND) | ||
trace.record(clientReq, s"requesting to ${req.uri}") | ||
req | ||
} | ||
|
||
def completeTrace(clientReq: BaseTracingSupport)(response: HttpResponse): HttpResponse = { | ||
trace.record(clientReq, s"response code ${response.status}") | ||
trace.finishChildRequest(clientReq) | ||
response | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
spray/src/test/scala/com/github/levkhomich/akka/tracing/http/TracedPipelineSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.levkhomich.akka.tracing.http | ||
|
||
import java.util.concurrent.TimeoutException | ||
import scala.concurrent.duration._ | ||
|
||
import spray.http._ | ||
import spray.client.pipelining._ | ||
import scala.concurrent.Future | ||
import com.github.levkhomich.akka.tracing._ | ||
import org.specs2.mutable.Specification | ||
import org.specs2.matcher.FutureMatchers | ||
|
||
class TracedPipelineSpec extends Specification with FutureMatchers with TracingTestCommons with TracingTestActorSystem with MockCollector { self => | ||
|
||
sequential | ||
|
||
def mockedPipeline(mockResponse: HttpResponse) = new TracedSprayPipeline { | ||
val system = self.system | ||
override def sendAndReceive = { | ||
case x: HttpRequest => | ||
Future.successful(mockResponse) | ||
} | ||
} | ||
|
||
val bodyEntity = spray.http.HttpEntity("test") | ||
|
||
"tracedPipeline" should { | ||
"Generate a sampled span when pipeline is used" in { | ||
val mockResponse = HttpResponse(StatusCodes.OK, bodyEntity) | ||
val pipeline = mockedPipeline(mockResponse) | ||
val mockTrigger = nextRandomMessage | ||
trace.forcedSample(mockTrigger, "test trace") | ||
pipeline.tracedPipeline[String](mockTrigger)(Get("http://test.com")) | ||
trace.finish(mockTrigger) | ||
expectSpans(2) | ||
} | ||
} | ||
|
||
step { | ||
shutdown() | ||
} | ||
} | ||
|