diff options
author | Diego <diegolparra@gmail.com> | 2014-09-09 00:58:03 -0300 |
---|---|---|
committer | Diego <diegolparra@gmail.com> | 2014-09-09 01:05:42 -0300 |
commit | 6884c0f3f7bf9376e9eaf4f330d7622c142399e3 (patch) | |
tree | 5953082053452a644b0f3323f4216c1808ee3ccb /kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala | |
parent | 23785a41dc3a0e9651ba87bc9dc255932ea64bd6 (diff) | |
download | Kamon-6884c0f3f7bf9376e9eaf4f330d7622c142399e3.tar.gz Kamon-6884c0f3f7bf9376e9eaf4f330d7622c142399e3.tar.bz2 Kamon-6884c0f3f7bf9376e9eaf4f330d7622c142399e3.zip |
= play:
* remove from publishErrorMessage method
* refactor onError method in RequestInstrumentation
* refactor WSInstrumentation in order to propagate the TraceContext when a WS call is executed outside an Action
* improve tests
* closes #33
Diffstat (limited to 'kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala')
-rw-r--r-- | kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala | 92 |
1 files changed, 43 insertions, 49 deletions
diff --git a/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala b/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala index 775d3e26..cbd95db3 100644 --- a/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala +++ b/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala @@ -16,70 +16,64 @@ package kamon.play +import kamon.Kamon +import kamon.metric.TraceMetrics.{ HttpClientRequest, TraceMetricsSnapshot } +import kamon.metric.{ Metrics, TraceMetrics } +import org.scalatest.{ Matchers, WordSpecLike } +import org.scalatestplus.play.OneServerPerSuite +import play.api.libs.ws.WS import play.api.mvc.Action import play.api.mvc.Results.Ok -import play.api.libs.ws.WS -import org.scalatestplus.play.OneServerPerSuite -import play.api.test._ import play.api.test.Helpers._ -import akka.actor.ActorSystem -import akka.testkit.{ TestKitBase, TestProbe } +import play.api.test._ +import play.libs.Akka -import com.typesafe.config.ConfigFactory -import org.scalatest.{ Matchers, WordSpecLike } -import kamon.Kamon -import kamon.metric.{ TraceMetrics, Metrics } -import kamon.metric.Subscriptions.TickMetricSnapshot -import kamon.metric.TraceMetrics.ElapsedTime +import scala.concurrent.Await +import scala.concurrent.duration._ -class WSInstrumentationSpec extends TestKitBase with WordSpecLike with Matchers with OneServerPerSuite { +class WSInstrumentationSpec extends WordSpecLike with Matchers with OneServerPerSuite { System.setProperty("config.file", "./kamon-play/src/test/resources/conf/application.conf") - import scala.collection.immutable.StringLike._ - implicit lazy val system: ActorSystem = ActorSystem("play-ws-instrumentation-spec", ConfigFactory.parseString( - """ - |akka { - | loglevel = ERROR - |} - | - |kamon { - | metrics { - | tick-interval = 2 seconds - | - | filters = [ - | { - | trace { - | includes = [ "*" ] - | excludes = [] - | } - | } - | ] - | } - |} - """.stripMargin)) - implicit override lazy val app = FakeApplication(withRoutes = { - case ("GET", "/async") ⇒ Action { Ok("ok") } + case ("GET", "/async") ⇒ Action { Ok("ok") } + case ("GET", "/outside") ⇒ Action { Ok("ok") } + case ("GET", "/inside") ⇒ callWSinsideController("http://localhost:19001/async") }) "the WS instrumentation" should { - "respond to the Async Action and complete the WS request" in { + "propagate the TraceContext outside an Action and complete the WS request" in { + Await.result(WS.url("http://localhost:19001/outside").get(), 10 seconds) + + val snapshot = takeSnapshotOf("http://localhost:19001/outside") + snapshot.elapsedTime.numberOfMeasurements should be(1) + snapshot.segments.size should be(1) + snapshot.segments(HttpClientRequest("http://localhost:19001/outside")).numberOfMeasurements should be(1) + } - val metricListener = TestProbe() - Kamon(Metrics)(system).subscribe(TraceMetrics, "*", metricListener.ref, permanently = true) - metricListener.expectMsgType[TickMetricSnapshot] + "propagate the TraceContext inside an Action and complete the WS request" in { + Await.result(route(FakeRequest(GET, "/inside")).get, 10 seconds) + + val snapshot = takeSnapshotOf("GET: /inside") + snapshot.elapsedTime.numberOfMeasurements should be(2) + snapshot.segments.size should be(1) + snapshot.segments(HttpClientRequest("http://localhost:19001/async")).numberOfMeasurements should be(1) + } + + } + + def takeSnapshotOf(traceName: String): TraceMetricsSnapshot = { + val recorder = Kamon(Metrics)(Akka.system()).register(TraceMetrics(traceName), TraceMetrics.Factory) + val collectionContext = Kamon(Metrics)(Akka.system()).buildDefaultCollectionContext + recorder.get.collect(collectionContext) + } - val response = await(WS.url("http://localhost:19001/async").get()) - response.status should be(OK) + def callWSinsideController(url: String) = Action.async { + import play.api.libs.concurrent.Execution.Implicits.defaultContext + import play.api.Play.current - // val tickSnapshot = metricListener.expectMsgType[TickMetricSnapshot] - // val traceMetrics = tickSnapshot.metrics.find { case (k, v) ⇒ k.name.contains("async") } map (_._2.metrics) - // traceMetrics should not be empty - // - // traceMetrics map { metrics ⇒ - // metrics(ElapsedTime).numberOfMeasurements should be(1L) - // } + WS.url(url).get().map { response ⇒ + Ok("Ok") } } }
\ No newline at end of file |