From 783746400b089703a1110dd22eba80bdab0b1898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Wed, 11 Sep 2024 03:11:28 +0200 Subject: [PATCH] Add a variant of `Trace.ioTrace` that takes an existing IOLocal --- .../core/shared/src/main/scala/Trace.scala | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/modules/core/shared/src/main/scala/Trace.scala b/modules/core/shared/src/main/scala/Trace.scala index 51027ef4..04a3cc2e 100644 --- a/modules/core/shared/src/main/scala/Trace.scala +++ b/modules/core/shared/src/main/scala/Trace.scala @@ -66,51 +66,51 @@ object Trace { /** A `Trace` instance that uses `IOLocal` internally. */ def ioTrace(rootSpan: Span[IO]): IO[Trace[IO]] = - IOLocal(rootSpan).map { local => - new Trace[IO] { - - override def put(fields: (String, TraceValue)*): IO[Unit] = - local.get.flatMap(_.put(fields: _*)) - - override def attachError(err: Throwable, fields: (String, TraceValue)*): IO[Unit] = - local.get.flatMap(_.attachError(err, fields: _*)) - - override def log(fields: (String, TraceValue)*): IO[Unit] = - local.get.flatMap(_.log(fields: _*)) - - override def log(event: String): IO[Unit] = - local.get.flatMap(_.log(event)) - - override def kernel: IO[Kernel] = - local.get.flatMap(_.kernel) - - override def spanR(name: String, options: Span.Options): Resource[IO, IO ~> IO] = - for { - parent <- Resource.eval(local.get) - child <- parent.span(name, options) - } yield new (IO ~> IO) { - def apply[A](fa: IO[A]): IO[A] = - local.get.flatMap { old => - local - .set(child) - .bracket(_ => fa.onError { case e => child.attachError(e) })(_ => local.set(old)) - } + IOLocal(rootSpan).map(ioTraceLocal) - } + def ioTraceLocal(local: IOLocal[Span[IO]]): Trace[IO] = new Trace[IO] { - override def span[A](name: String, options: Span.Options)(k: IO[A]): IO[A] = - spanR(name, options).use(_(k)) + override def put(fields: (String, TraceValue)*): IO[Unit] = + local.get.flatMap(_.put(fields: _*)) + + override def attachError(err: Throwable, fields: (String, TraceValue)*): IO[Unit] = + local.get.flatMap(_.attachError(err, fields: _*)) - override def traceId: IO[Option[String]] = - local.get.flatMap(_.traceId) + override def log(fields: (String, TraceValue)*): IO[Unit] = + local.get.flatMap(_.log(fields: _*)) - override def spanId(implicit F: Applicative[IO]): IO[Option[String]] = - local.get.flatMap(_.spanId) + override def log(event: String): IO[Unit] = + local.get.flatMap(_.log(event)) + + override def kernel: IO[Kernel] = + local.get.flatMap(_.kernel) + + override def spanR(name: String, options: Span.Options): Resource[IO, IO ~> IO] = + for { + parent <- Resource.eval(local.get) + child <- parent.span(name, options) + } yield new (IO ~> IO) { + def apply[A](fa: IO[A]): IO[A] = + local.get.flatMap { old => + local + .set(child) + .bracket(_ => fa.onError { case e => child.attachError(e) })(_ => local.set(old)) + } - override def traceUri: IO[Option[URI]] = - local.get.flatMap(_.traceUri) } - } + + override def span[A](name: String, options: Span.Options)(k: IO[A]): IO[A] = + spanR(name, options).use(_(k)) + + override def traceId: IO[Option[String]] = + local.get.flatMap(_.traceId) + + override def spanId(implicit F: Applicative[IO]): IO[Option[String]] = + local.get.flatMap(_.spanId) + + override def traceUri: IO[Option[URI]] = + local.get.flatMap(_.traceUri) + } /** A `Trace` instance that uses `IOLocal` internally. Span creation delegates to the supplied entry point. */ def ioTraceForEntryPoint(ep: EntryPoint[IO]): IO[Trace[IO]] =