blob: a3ef307c4aadab1a7b30bb21f5acab0516a76be6 (
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
|
package xyz.driver.tracing
import java.util.UUID
import java.time.{Instant, ZoneId}
import java.time.format.DateTimeFormatter
import spray.json._
class LoggingTracer(logger: String => Unit) extends Tracer {
import LoggingTracer.JsonProtocol._
override def submit(span: Span): Unit = logger(span.toJson.compactPrint)
}
object LoggingTracer {
object JsonProtocol extends DefaultJsonProtocol {
implicit def uuidFormat: JsonFormat[UUID] = jsonFormat(
{ (js: JsValue) =>
js match {
case JsString(str) => UUID.fromString(str)
case other => deserializationError("expected string as UUID")
}
}, { (uuid: UUID) =>
JsString(uuid.toString)
}
)
private val timeFormatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.withZone(ZoneId.of("UTC"))
implicit def instantFormat: JsonFormat[Instant] = jsonFormat(
{ (js: JsValue) =>
js match {
case JsString(str) => Instant.parse(str)
case other => deserializationError(s"`$other` is not a valid instant")
}
}, { (instant: Instant) =>
JsString(timeFormatter.format(instant))
}
)
implicit def spanFormat: RootJsonFormat[Span] = jsonFormat7(Span.apply)
}
}
|