aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jakob@driver.xyz>2017-10-01 23:15:12 -0700
committerJakob Odersky <jakob@driver.xyz>2017-10-01 23:15:12 -0700
commit774864100676b7dbcf332f40c833cfa65dcd7b5a (patch)
tree820462a4b57aaa7647075b0a9495f05443672a0f
parent2c08b51411be5b0cce57f876377fcd52bee99990 (diff)
downloadtracing-774864100676b7dbcf332f40c833cfa65dcd7b5a.tar.gz
tracing-774864100676b7dbcf332f40c833cfa65dcd7b5a.tar.bz2
tracing-774864100676b7dbcf332f40c833cfa65dcd7b5a.zip
Fix google tracing long and date format
-rw-r--r--src/main/scala/GoogleTracer.scala3
-rw-r--r--src/main/scala/TracingDirectives.scala52
-rw-r--r--src/main/scala/google/api.scala15
3 files changed, 42 insertions, 28 deletions
diff --git a/src/main/scala/GoogleTracer.scala b/src/main/scala/GoogleTracer.scala
index 5b3c9df..7ffd356 100644
--- a/src/main/scala/GoogleTracer.scala
+++ b/src/main/scala/GoogleTracer.scala
@@ -47,10 +47,8 @@ class GoogleTracer(projectId: String,
lazy val queue: SourceQueueWithComplete[Span] = {
Source
.queue[Span](bufferSize, OverflowStrategy.dropNew)
- .log("debug")
.viaMat(batchingPipeline)(Keep.left)
.mapAsync(concurrentConnections) { (traces: Traces) =>
- println(traces)
Marshal(traces).to[RequestEntity].map { entity =>
HttpRequest(
method = HttpMethods.PATCH,
@@ -72,7 +70,6 @@ class GoogleTracer(projectId: String,
.mapError {
case NonFatal(e) =>
system.log.error(s"Exception encountered while submitting trace", e)
- e.printStackTrace
e
}
.to(Sink.ignore)
diff --git a/src/main/scala/TracingDirectives.scala b/src/main/scala/TracingDirectives.scala
index c55ac40..d0cbfd2 100644
--- a/src/main/scala/TracingDirectives.scala
+++ b/src/main/scala/TracingDirectives.scala
@@ -21,29 +21,35 @@ trait TracingDirectives {
def withTraceContext(ctx: TraceContext): Directive0 =
mapRequest(req => req.withHeaders(ctx.headers))
- def trace(name: String, labels: Map[String, String] = Map.empty)(
- implicit tracer: Tracer): Directive0 =
- optionalTraceContext.flatMap {
- case parent =>
- val span: Span = parent match {
- case None => // no parent span, create new trace
- Span(
- name = name,
- labels = labels
- )
- case Some(TraceContext(traceId, parentSpanId)) =>
- Span(
- name = name,
- traceId = traceId,
- parentSpanId = parentSpanId,
- labels = labels
- )
- }
+ def trace(tracer: Tracer, name: String, extraLabels: Map[String, String] = Map.empty): Directive0 =
+ extractRequest.flatMap { request =>
+ val labels = Map(
+ "/http/user_agent" -> "driver-tracer",
+ "/http/method" -> request.method.name,
+ "/http/url" -> request.uri.path.toString,
+ "/http/host" -> request.uri.authority.host.toString
+ ) ++ extraLabels
+
+ val span: Span = TraceContext.fromHeaders(request.headers) match {
+ case None => // no parent span, create new trace
+ Span(
+ name = name,
+ labels = labels
+ )
+ case Some(TraceContext(traceId, parentSpanId)) =>
+ Span(
+ name = name,
+ traceId = traceId,
+ parentSpanId = parentSpanId,
+ labels = labels
+ )
+ }
+
+ withTraceContext(TraceContext.fromSpan(span)) & mapRouteResult { res =>
+ tracer.submit(span.end())
+ res
+ }
- withTraceContext(TraceContext.fromSpan(span)) & mapRouteResult { res =>
- tracer.submit(span.end())
- res
- }
}
/*
@@ -57,7 +63,7 @@ trait TracingDirectives {
}
-object TracingDirectives {
+object TracingDirectives extends TracingDirectives {
case class TraceContext(traceId: UUID, parentSpanId: Option[UUID]) {
import TraceContext._
diff --git a/src/main/scala/google/api.scala b/src/main/scala/google/api.scala
index 122b695..de4c82b 100644
--- a/src/main/scala/google/api.scala
+++ b/src/main/scala/google/api.scala
@@ -2,7 +2,7 @@ package xyz.driver.tracing
package google
import spray.json._
-import spray.json.DefaultJsonProtocol._
+import spray.json.DefaultJsonProtocol.{LongJsonFormat => _, _}
import java.util.UUID
import java.nio.ByteBuffer
import java.time._
@@ -47,7 +47,7 @@ object TraceSpan {
implicit val instantFormat = new JsonFormat[Instant] {
val formatter = DateTimeFormatter
- .ofPattern("yyyy-MM-dd'T'HH:mm:ssXXXZ")
+ .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.withZone(ZoneId.of("UTC"))
override def write(x: Instant): JsValue = JsString(formatter.format(x))
override def read(x: JsValue): Instant = x match {
@@ -57,6 +57,17 @@ object TraceSpan {
}
}
+ implicit val longFormat: JsonFormat[Long] = new JsonFormat[Long] {
+ override def write(x: Long): JsValue = {
+ JsString(java.lang.Long.toUnsignedString(x))
+ }
+ override def read(x: JsValue): Long = x match {
+ case JsString(num) => num.toLong
+ case other =>
+ spray.json.deserializationError("expected long")
+ }
+ }
+
implicit val format: JsonFormat[TraceSpan] = jsonFormat7(TraceSpan.apply)
def fromSpan(span: Span) = TraceSpan(