From a178592098a2bc07fcb7749eaf148debf02a5e63 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 29 Aug 2018 16:09:37 -0700 Subject: Fix trace ID generation and improve trace naming scheme --- src/main/scala/xyz/driver/core/init/HttpApi.scala | 17 ++++++++++------- .../xyz/driver/core/reporting/GoogleReporter.scala | 6 +++--- src/main/scala/xyz/driver/core/reporting/Reporter.scala | 11 +++++++---- .../scala/xyz/driver/core/reporting/SpanContext.scala | 4 ++-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/scala/xyz/driver/core/init/HttpApi.scala b/src/main/scala/xyz/driver/core/init/HttpApi.scala index 6ea3d51..a2312ce 100644 --- a/src/main/scala/xyz/driver/core/init/HttpApi.scala +++ b/src/main/scala/xyz/driver/core/init/HttpApi.scala @@ -60,18 +60,21 @@ trait HttpApi extends CloudServices with Directives with SprayJsonSupport { self val tags = Map( "service_name" -> name, "service_version" -> version.getOrElse(""), - "http_user_agent" -> ctx.request.header[`User-Agent`].map(_.value).getOrElse(""), + "http_path" -> ctx.request.uri.path.toString, + "http_method" -> ctx.request.method.value.toString, "http_uri" -> ctx.request.uri.toString, - "http_path" -> ctx.request.uri.path.toString + "http_user_agent" -> ctx.request.header[`User-Agent`].map(_.value).getOrElse("") ) val parent = ctx.request.header[Traceparent].map { p => SpanContext(p.traceId, p.spanId) -> CausalRelation.Child } - reporter.traceWithOptionalParentAsync("handle_service_request", tags, parent) { sctx => - val header = Traceparent(sctx.traceId, sctx.spanId) - val withHeader = ctx.withRequest(ctx.request.withHeaders(header)) - inner(withHeader) - } + reporter + .traceWithOptionalParentAsync(s"${ctx.request.method.value.toLowerCase}_${ctx.request.uri.path}", tags, parent) { + sctx => + val header = Traceparent(sctx.traceId, sctx.spanId) + val withHeader = ctx.withRequest(ctx.request.withHeaders(header)) + inner(withHeader) + } } /** Extended route. */ diff --git a/src/main/scala/xyz/driver/core/reporting/GoogleReporter.scala b/src/main/scala/xyz/driver/core/reporting/GoogleReporter.scala index 40cb1e5..2f889f5 100644 --- a/src/main/scala/xyz/driver/core/reporting/GoogleReporter.scala +++ b/src/main/scala/xyz/driver/core/reporting/GoogleReporter.scala @@ -111,7 +111,7 @@ class GoogleReporter( parentSpanId: Option[String], displayName: String, attributes: Map[String, String]) = Span( - s"project/${credentials.getProjectId}/traces/$traceId/spans/$spanId", + s"projects/${credentials.getProjectId}/traces/$traceId/spans/$spanId", spanId, parentSpanId, TruncatableString(displayName), @@ -125,7 +125,7 @@ class GoogleReporter( tags: Map[String, String], parent: Option[(SpanContext, CausalRelation)])(operation: SpanContext => A): A = { val child = parent match { - case Some((p, _)) => SpanContext(p.traceId, f"${Random.nextLong()}%02x") + case Some((p, _)) => SpanContext(p.traceId, f"${Random.nextLong()}%016x") case None => SpanContext.fresh() } val span = startSpan(child.traceId, child.spanId, parent.map(_._1.spanId), operationName, tags) @@ -140,7 +140,7 @@ class GoogleReporter( tags: Map[String, String], parent: Option[(SpanContext, CausalRelation)])(operation: SpanContext => Future[A]): Future[A] = { val child = parent match { - case Some((p, _)) => SpanContext(p.traceId, f"${Random.nextLong()}%02x") + case Some((p, _)) => SpanContext(p.traceId, f"${Random.nextLong()}%016x") case None => SpanContext.fresh() } val span = startSpan(child.traceId, child.spanId, parent.map(_._1.spanId), operationName, tags) diff --git a/src/main/scala/xyz/driver/core/reporting/Reporter.scala b/src/main/scala/xyz/driver/core/reporting/Reporter.scala index 2425044..9649ada 100644 --- a/src/main/scala/xyz/driver/core/reporting/Reporter.scala +++ b/src/main/scala/xyz/driver/core/reporting/Reporter.scala @@ -66,7 +66,7 @@ trait Reporter { * * $rootWarning */ - def traceRoot[A](name: String, tags: Map[String, String])(op: SpanContext => A): A = + def traceRoot[A](name: String, tags: Map[String, String] = Map.empty)(op: SpanContext => A): A = traceWithOptionalParent( name, tags, @@ -79,7 +79,7 @@ trait Reporter { * * @see traceRoot */ - def traceRootAsync[A](name: String, tags: Map[String, String])(op: SpanContext => Future[A]): Future[A] = + def traceRootAsync[A](name: String, tags: Map[String, String] = Map.empty)(op: SpanContext => Future[A]): Future[A] = traceWithOptionalParentAsync( name, tags, @@ -102,7 +102,7 @@ trait Reporter { * @tparam A Return type of the operation. * @return The value of the child operation. */ - def trace[A](name: String, tags: Map[String, String], relation: CausalRelation = CausalRelation.Child)( + def trace[A](name: String, tags: Map[String, String] = Map.empty, relation: CausalRelation = CausalRelation.Child)( op: /* implicit (gotta wait for Scala 3) */ SpanContext => A)(implicit ctx: SpanContext): A = traceWithOptionalParent( name, @@ -117,7 +117,10 @@ trait Reporter { * * @see trace */ - def traceAsync[A](name: String, tags: Map[String, String], relation: CausalRelation = CausalRelation.Child)( + def traceAsync[A]( + name: String, + tags: Map[String, String] = Map.empty, + relation: CausalRelation = CausalRelation.Child)( op: /* implicit (gotta wait for Scala 3) */ SpanContext => Future[A])(implicit ctx: SpanContext): Future[A] = traceWithOptionalParentAsync( name, diff --git a/src/main/scala/xyz/driver/core/reporting/SpanContext.scala b/src/main/scala/xyz/driver/core/reporting/SpanContext.scala index 58ab973..ecc2ba3 100644 --- a/src/main/scala/xyz/driver/core/reporting/SpanContext.scala +++ b/src/main/scala/xyz/driver/core/reporting/SpanContext.scala @@ -5,7 +5,7 @@ import scala.util.Random case class SpanContext private[core] (traceId: String, spanId: String) object SpanContext { def fresh() = SpanContext( - f"${Random.nextLong()}%02x${Random.nextLong()}%02x", - f"${Random.nextLong()}%02x" + f"${Random.nextLong()}%016x${Random.nextLong()}%016x", + f"${Random.nextLong()}%016x" ) } -- cgit v1.2.3