aboutsummaryrefslogtreecommitdiff
path: root/kamon-play/src/test/scala/kamon/play/RequestInstrumentationSpec.scala
blob: 976e7bc0b8f8e4689bde9dfd348a6974aa7b1eb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* =========================================================================================
 * Copyright © 2013-2014 the kamon project <http://kamon.io/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 * =========================================================================================
 */

package kamon.play

import kamon.Kamon
import kamon.http.HttpServerMetrics
import kamon.metric.{ CollectionContext, Metrics, TraceMetrics }
import kamon.play.action.TraceName
import kamon.trace.{ TraceLocal, TraceRecorder }
import org.scalatestplus.play._
import play.api.DefaultGlobal
import play.api.http.Writeable
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.ws.WS
import play.api.mvc.Results.Ok
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test._
import play.core.Router.{ HandlerDef, Route, Routes }
import play.core.{ DynamicPart, PathPattern, Router, StaticPart }
import play.libs.Akka

import scala.concurrent.duration._
import scala.concurrent.{ Await, Future }

class RequestInstrumentationSpec extends PlaySpec with OneServerPerSuite {

  System.setProperty("config.file", "./kamon-play/src/test/resources/conf/application.conf")

  val executor = scala.concurrent.ExecutionContext.Implicits.global

  implicit override lazy val app = FakeApplication(withGlobal = Some(MockGlobalTest), withRoutes = {

    case ("GET", "/async") 
      Action.async {
        Future {
          Ok("Async.async")
        }(executor)
      }
    case ("GET", "/notFound") 
      Action {
        Results.NotFound
      }
    case ("GET", "/error") 
      Action {
        throw new Exception("This page generates an error!")
        Ok("This page will generate an error!")
      }
    case ("GET", "/redirect") 
      Action {
        Results.Redirect("/redirected", MOVED_PERMANENTLY)
      }
    case ("GET", "/default") 
      Action {
        Ok("default")
      }
    case ("GET", "/async-renamed") 
      TraceName("renamed-trace") {
        Action.async {
          Future {
            Ok("Async.async")
          }(executor)
        }
      }
    case ("GET", "/retrieve") 
      Action {
        Ok("retrieve from TraceLocal")
      }
  }, additionalConfiguration = Map(
    ("application.router", "kamon.play.Routes"),
    ("logger.root", "OFF"),
    ("logger.play", "OFF"),
    ("logger.application", "OFF")))

  private val traceTokenValue = "kamon-trace-token-test"
  private val traceTokenHeaderName = "X-Trace-Token"
  private val expectedToken = Some(traceTokenValue)
  private val traceTokenHeader = traceTokenHeaderName -> traceTokenValue
  private val traceLocalStorageValue = "localStorageValue"
  private val traceLocalStorageKey = "localStorageKey"
  private val traceLocalStorageHeader = traceLocalStorageKey -> traceLocalStorageValue

  "the Request instrumentation" should {
    "respond to the Async Action with X-Trace-Token" in {
      val Some(result) = route(FakeRequest(GET, "/async").withHeaders(traceTokenHeader, traceLocalStorageHeader))
      header(traceTokenHeaderName, result) must be(expectedToken)
    }

    "respond to the NotFound Action with X-Trace-Token" in {
      val Some(result) = route(FakeRequest(GET, "/notFound").withHeaders(traceTokenHeader))
      header(traceTokenHeaderName, result) must be(expectedToken)
    }

    "respond to the Default Action with X-Trace-Token" in {
      val Some(result) = route(FakeRequest(GET, "/default").withHeaders(traceTokenHeader))
      header(traceTokenHeaderName, result) must be(expectedToken)
    }

    "respond to the Redirect Action with X-Trace-Token" in {
      val Some(result) = route(FakeRequest(GET, "/redirect").withHeaders(traceTokenHeader))
      header("Location", result) must be(Some("/redirected"))
      header(traceTokenHeaderName, result) must be(expectedToken)
    }

    "respond to the Async Action with X-Trace-Token and the renamed trace" in {
      val result = Await.result(route(FakeRequest(GET, "/async-renamed").withHeaders(traceTokenHeader)).get, 10 seconds)
      TraceRecorder.currentContext.name must be("renamed-trace")
      Some(result.header.headers(traceTokenHeaderName)) must be(expectedToken)
    }

    "propagate the TraceContext and LocalStorage through of filters in the current request" in {
      val Some(result) = route(FakeRequest(GET, "/retrieve").withHeaders(traceTokenHeader, traceLocalStorageHeader))
      TraceLocal.retrieve(TraceLocalKey).get must be(traceLocalStorageValue)
    }

    "response to the getRouted Action and normalise the current TraceContext name" in {
      Await.result(WS.url("http://localhost:19001/getRouted").get, 10 seconds)
      Kamon(Metrics)(Akka.system()).storage.get(TraceMetrics("getRouted.get")) must not be (empty)
    }

    "response to the postRouted Action and normalise the current TraceContext name" in {
      Await.result(WS.url("http://localhost:19001/postRouted").post("content"), 10 seconds)
      Kamon(Metrics)(Akka.system()).storage.get(TraceMetrics("postRouted.post")) must not be (empty)
    }

    "response to the showRouted Action and normalise the current TraceContext name" in {
      Await.result(WS.url("http://localhost:19001/showRouted/2").get, 10 seconds)
      Kamon(Metrics)(Akka.system()).storage.get(TraceMetrics("show.some.id.get")) must not be (empty)
    }

    "record http server metrics for all processed requests" in {
      val collectionContext = CollectionContext(100)
      Kamon(Metrics)(Akka.system()).register(HttpServerMetrics, HttpServerMetrics.Factory).get.collect(collectionContext)

      for (repetition  1 to 10) {
        Await.result(route(FakeRequest(GET, "/default").withHeaders(traceTokenHeader)).get, 10 seconds)
      }

      for (repetition  1 to 5) {
        Await.result(route(FakeRequest(GET, "/notFound").withHeaders(traceTokenHeader)).get, 10 seconds)
      }

      for (repetition  1 to 5) {
        Await.result(routeWithOnError(FakeRequest(GET, "/error").withHeaders(traceTokenHeader)).get, 10 seconds)
      }

      val snapshot = Kamon(Metrics)(Akka.system()).register(HttpServerMetrics, HttpServerMetrics.Factory).get.collect(collectionContext)
      snapshot.countsPerTraceAndStatusCode("GET: /default")("200").count must be(10)
      snapshot.countsPerTraceAndStatusCode("GET: /notFound")("404").count must be(5)
      snapshot.countsPerTraceAndStatusCode("GET: /error")("500").count must be(5)
      snapshot.countsPerStatusCode("200").count must be(10)
      snapshot.countsPerStatusCode("404").count must be(5)
      snapshot.countsPerStatusCode("500").count must be(5)
    }
  }

  object MockGlobalTest extends WithFilters(TraceLocalFilter)

  object TraceLocalKey extends TraceLocal.TraceLocalKey {
    type ValueType = String
  }

  object TraceLocalFilter extends Filter {
    override def apply(next: (RequestHeader)  Future[SimpleResult])(header: RequestHeader): Future[SimpleResult] = {
      TraceRecorder.withTraceContext(TraceRecorder.currentContext) {

        TraceLocal.store(TraceLocalKey)(header.headers.get(traceLocalStorageKey).getOrElse("unknown"))

        next(header).map {
          result  result.withHeaders((traceLocalStorageKey -> TraceLocal.retrieve(TraceLocalKey).get))
        }
      }
    }
  }

  def routeWithOnError[T](req: Request[T])(implicit w: Writeable[T]): Option[Future[Result]] = {
    route(req).map { result 
      result.recoverWith {
        case t: Throwable  DefaultGlobal.onError(req, t)
      }
    }
  }
}

object Routes extends Router.Routes {
  private var _prefix = "/"

  def setPrefix(prefix: String) {
    _prefix = prefix
    List[(String, Routes)]().foreach {
      case (p, router)  router.setPrefix(prefix + (if (prefix.endsWith("/")) "" else "/") + p)
    }
  }

  def prefix = _prefix

  lazy val defaultPrefix = {
    if (Routes.prefix.endsWith("/")) "" else "/"
  }
  // Gets
  private[this] lazy val Application_getRouted =
    Route("GET", PathPattern(List(StaticPart(Routes.prefix), StaticPart(Routes.defaultPrefix), StaticPart("getRouted"))))

  private[this] lazy val Application_show =
    Route("GET", PathPattern(List(StaticPart(Routes.prefix), StaticPart(Routes.defaultPrefix), StaticPart("showRouted/"), DynamicPart("id", """[^/]+""", true))))

  //Posts
  private[this] lazy val Application_postRouted =
    Route("POST", PathPattern(List(StaticPart(Routes.prefix), StaticPart(Routes.defaultPrefix), StaticPart("postRouted"))))

  def documentation = Nil // Documentation not needed for tests

  def routes: PartialFunction[RequestHeader, Handler] = {
    case Application_getRouted(params)  call {
      invokeHandler(controllers.Application.getRouted,
        HandlerDef(this, "controllers.Application", "getRouted", Nil, "GET", """some comment""", Routes.prefix + """getRouted"""))
    }
    case Application_postRouted(params)  call {
      invokeHandler(controllers.Application.postRouted,
        HandlerDef(this, "controllers.Application", "postRouted", Nil, "POST", """some comment""", Routes.prefix + """postRouted"""))
    }
    case Application_show(params)  call(params.fromPath[Int]("id", None)) { (id) 
      invokeHandler(controllers.Application.showRouted(id),
        HandlerDef(this, "controllers.Application", "showRouted", Seq(classOf[Int]), "GET", """some comment""", Routes.prefix + """show/some/$id<[^/]+>"""))
    }
  }
}

object controllers {
  import play.api.mvc._

  object Application extends Controller {
    val postRouted = Action {
      Ok("invoked postRouted")
    }
    val getRouted = Action {
      Ok("invoked getRouted")
    }
    def showRouted(id: Int) = Action {
      Ok("invoked show with: " + id)
    }
  }
}