blob: c41f700447b2950f923c4bb7a9b4e30b468d4f7b (
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
|
/* =========================================================================================
* 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 ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.classic.{ AsyncAppender, LoggerContext }
import ch.qos.logback.core.read.ListAppender
import ch.qos.logback.core.status.NopStatusListener
import kamon.trace.TraceLocal
import org.scalatest.BeforeAndAfter
import org.scalatestplus.play._
import org.slf4j
import play.api.LoggerLike
import play.api.mvc.Results.Ok
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test._
import scala.concurrent.Future
class LoggerLikeInstrumentationSpec extends PlaySpec with OneServerPerSuite with BeforeAndAfter {
System.setProperty("config.file", "./kamon-play/src/test/resources/conf/application.conf")
val executor = scala.concurrent.ExecutionContext.Implicits.global
val infoMessage = "Info Message"
val headerValue = "My header value"
val otherValue = "My other value"
case class LocalStorageValue(header: String, other: String)
object TraceLocalKey extends TraceLocal.TraceLocalKey {
type ValueType = LocalStorageValue
}
before {
LoggingHandler.startLogging()
}
after {
LoggingHandler.stopLogging()
}
implicit override lazy val app = FakeApplication(withRoutes = {
case ("GET", "/logging") ⇒
Action.async {
Future {
TraceLocal.store(TraceLocalKey)(LocalStorageValue(headerValue, otherValue))
LoggingHandler.info(infoMessage)
Ok("OK")
}(executor)
}
})
"the LoggerLike instrumentation" should {
"be put the properties of TraceLocal into the MDC as key -> value in a request" in {
LoggingHandler.appenderStart()
val Some(result) = route(FakeRequest(GET, "/logging"))
Thread.sleep(500) // wait to complete the future
TraceLocal.retrieve(TraceLocalKey) must be(Some(LocalStorageValue(headerValue, otherValue)))
LoggingHandler.appenderStop()
headerValue must be(LoggingHandler.getValueFromMDC("header"))
otherValue must be(LoggingHandler.getValueFromMDC("other"))
}
}
}
object LoggingHandler extends LoggerLike {
val loggerContext = new LoggerContext()
val rootLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME)
val asyncAppender = new AsyncAppender()
val listAppender = new ListAppender[ILoggingEvent]()
val nopStatusListener = new NopStatusListener()
override val logger: slf4j.Logger = rootLogger
def startLogging(): Unit = {
loggerContext.getStatusManager().add(nopStatusListener)
asyncAppender.setContext(loggerContext)
listAppender.setContext(loggerContext)
listAppender.setName("list")
listAppender.start()
}
def stopLogging(): Unit = {
listAppender.stop()
}
def appenderStart(): Unit = {
asyncAppender.addAppender(listAppender)
asyncAppender.start()
rootLogger.addAppender(asyncAppender)
}
def appenderStop(): Unit = {
asyncAppender.stop()
}
def getValueFromMDC(key: String): String = {
listAppender.list.get(0).getMDCPropertyMap.get(key)
}
}
|