Skip to content

Commit

Permalink
Merge pull request #685 from tpolecat/pr/673
Browse files Browse the repository at this point in the history
Allow non-uuid trace ids from kernel
  • Loading branch information
mpilquist authored Dec 17, 2022
2 parents 71b8367 + ba5fae7 commit a3fc7ef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
36 changes: 18 additions & 18 deletions modules/log/shared/src/main/scala/LogSpan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ 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]],
spanCreationPolicy: Span.Options.SpanCreationPolicy
) 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]] =
Expand All @@ -47,8 +47,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]

Expand Down Expand Up @@ -90,18 +90,18 @@ 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)

}

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]
}
Expand Down Expand Up @@ -173,10 +173,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,
Expand All @@ -189,17 +189,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,
Expand All @@ -212,19 +212,19 @@ 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])
children <- Ref[F].of(List.empty[JsonObject])
} 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,
Expand Down
19 changes: 19 additions & 0 deletions modules/log/shared/src/test/scala/LogSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ 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"))
}
}

}

0 comments on commit a3fc7ef

Please sign in to comment.