From b5f5bfe1984c67c1c934400e19bd803e33e838c8 Mon Sep 17 00:00:00 2001 From: Magnus Valle Date: Fri, 9 Dec 2022 11:13:48 +0000 Subject: [PATCH 1/2] #673 Allow non-uuid trace ids from kernel --- .../log/shared/src/main/scala/LogSpan.scala | 36 +++++++++---------- .../log/shared/src/test/scala/LogSuite.scala | 12 +++++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/modules/log/shared/src/main/scala/LogSpan.scala b/modules/log/shared/src/main/scala/LogSpan.scala index 4841ff76..25bf66f8 100644 --- a/modules/log/shared/src/main/scala/LogSpan.scala +++ b/modules/log/shared/src/main/scala/LogSpan.scala @@ -26,10 +26,10 @@ import java.net.URI private[log] final case class LogSpan[F[_]: Sync: Logger]( service: String, name: String, - sid: UUID, - parent: Option[Either[UUID, LogSpan[F]]], + sid: String, + parent: Option[Either[String, LogSpan[F]]], parentKernel: Option[Kernel], - traceUUID: UUID, + traceID: String, timestamp: Instant, fields: Ref[F, Map[String, Json]], children: Ref[F, List[JsonObject]], @@ -37,7 +37,7 @@ private[log] final case class LogSpan[F[_]: Sync: Logger]( ) extends Span.Default[F] { import LogSpan._ - def parentId: Option[UUID] = + def parentId: Option[String] = parent.map(_.fold(identity, _.sid)) def get(key: String): F[Option[Json]] = @@ -46,8 +46,8 @@ private[log] final case class LogSpan[F[_]: Sync: Logger]( def kernel: F[Kernel] = Kernel( Map( - Headers.TraceId -> traceUUID.toString, - Headers.SpanId -> sid.toString + Headers.TraceId -> traceID, + Headers.SpanId -> sid ) ).pure[F] @@ -89,7 +89,7 @@ private[log] final case class LogSpan[F[_]: Sync: Logger]( "duration_ms" -> (finish.toEpochMilli - timestamp.toEpochMilli).asJson, "trace.span_id" -> sid.asJson, "trace.parent_id" -> parentId.asJson, - "trace.trace_id" -> traceUUID.asJson + "trace.trace_id" -> traceID.asJson ) ++ fs ++ List("children" -> cs.reverse.map(Json.fromJsonObject).asJson) JsonObject.fromIterable(fields) @@ -97,10 +97,10 @@ private[log] final case class LogSpan[F[_]: Sync: Logger]( } def traceId: F[Option[String]] = - traceUUID.toString.some.pure[F] + traceID.some.pure[F] def spanId: F[Option[String]] = - sid.toString.some.pure[F] + sid.some.pure[F] def traceUri: F[Option[URI]] = none.pure[F] } @@ -172,10 +172,10 @@ private[log] object LogSpan { } yield LogSpan( service = parent.service, name = name, - sid = spanId, + sid = spanId.toString, parent = Some(Right(parent)), parentKernel = options.parentKernel, - traceUUID = parent.traceUUID, + traceID = parent.traceID, timestamp = timestamp, fields = fields, children = children, @@ -188,17 +188,17 @@ private[log] object LogSpan { ): F[LogSpan[F]] = for { spanId <- uuid[F] - traceUUID <- uuid[F] + traceID <- uuid[F] timestamp <- now[F] fields <- Ref[F].of(Map.empty[String, Json]) children <- Ref[F].of(List.empty[JsonObject]) } yield LogSpan( service = service, name = name, - sid = spanId, + sid = spanId.toString, parent = None, parentKernel = None, - traceUUID = traceUUID, + traceID = traceID.toString, timestamp = timestamp, fields = fields, children = children, @@ -211,8 +211,8 @@ private[log] object LogSpan { kernel: Kernel ): F[LogSpan[F]] = for { - traceUUID <- Sync[F].catchNonFatal(UUID.fromString(kernel.toHeaders(Headers.TraceId))) - parentId <- Sync[F].catchNonFatal(UUID.fromString(kernel.toHeaders(Headers.SpanId))) + traceID <- Sync[F].catchNonFatal(kernel.toHeaders(Headers.TraceId)) + parentId <- Sync[F].catchNonFatal(kernel.toHeaders(Headers.SpanId)) spanId <- uuid[F] timestamp <- now[F] fields <- Ref[F].of(Map.empty[String, Json]) @@ -220,10 +220,10 @@ private[log] object LogSpan { } yield LogSpan( service = service, name = name, - sid = spanId, + sid = spanId.toString, parent = Some(Left(parentId)), parentKernel = None, - traceUUID = traceUUID, + traceID = traceID, timestamp = timestamp, fields = fields, children = children, diff --git a/modules/log/shared/src/test/scala/LogSuite.scala b/modules/log/shared/src/test/scala/LogSuite.scala index 9e4a8da4..207b606b 100644 --- a/modules/log/shared/src/test/scala/LogSuite.scala +++ b/modules/log/shared/src/test/scala/LogSuite.scala @@ -61,4 +61,16 @@ class LogSuite extends CatsEffectSuite { } } + test("continueOrElseRoot should preserve non-uuid trace from kernel") { + MockLogger.newInstance[IO]("test").flatMap { implicit log => + val kernel = Kernel(Map(LogSpan.Headers.TraceId -> "non-uuid-trace-id", LogSpan.Headers.SpanId -> "non-uuid-parent-span-id")) + val traceId = Log.entryPoint[IO]("service", filter(_).spaces2) + .continueOrElseRoot("root span", kernel).use { root => + root.traceId + } + + assertIO(traceId, Some("non-uuid-trace-id")) + } + } + } From ba5fae77745ac4f60205c681c43f6d5ee6687216 Mon Sep 17 00:00:00 2001 From: mpilquist Date: Sat, 17 Dec 2022 09:48:48 -0500 Subject: [PATCH 2/2] Scalafmt --- .../log/shared/src/test/scala/LogSuite.scala | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/log/shared/src/test/scala/LogSuite.scala b/modules/log/shared/src/test/scala/LogSuite.scala index 207b606b..5a3391fb 100644 --- a/modules/log/shared/src/test/scala/LogSuite.scala +++ b/modules/log/shared/src/test/scala/LogSuite.scala @@ -63,11 +63,18 @@ class LogSuite extends CatsEffectSuite { test("continueOrElseRoot should preserve non-uuid trace from kernel") { MockLogger.newInstance[IO]("test").flatMap { implicit log => - val kernel = Kernel(Map(LogSpan.Headers.TraceId -> "non-uuid-trace-id", LogSpan.Headers.SpanId -> "non-uuid-parent-span-id")) - val traceId = Log.entryPoint[IO]("service", filter(_).spaces2) - .continueOrElseRoot("root span", kernel).use { root => - root.traceId - } + val kernel = Kernel( + Map( + LogSpan.Headers.TraceId -> "non-uuid-trace-id", + LogSpan.Headers.SpanId -> "non-uuid-parent-span-id" + ) + ) + val traceId = Log + .entryPoint[IO]("service", filter(_).spaces2) + .continueOrElseRoot("root span", kernel) + .use { root => + root.traceId + } assertIO(traceId, Some("non-uuid-trace-id")) }