Skip to content

LiftTrace concept #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ lazy val mtlJS = mtl.js.dependsOn(coreJS)
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)),
)

// Noop srcs haved move to core, but keeping artifact for compatibility
lazy val noop = crossProject(JSPlatform, JVMPlatform)
.in(file("modules/noop"))
.dependsOn(core)
Expand Down
39 changes: 39 additions & 0 deletions modules/core/shared/src/main/scala/LiftTrace.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2019-2020 by Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package natchez

import cats.arrow.FunctionK
import cats.data.Kleisli
import cats.effect.IO
import cats.effect.kernel.MonadCancelThrow
import cats.~>
import natchez.noop.NoopSpan

trait LiftTrace[F[_], G[_]] {
def run[A](span: Span[F])(f: Trace[G] => G[A]): F[A]
def liftK: F ~> G
def lowerK: G ~> F
}

object LiftTrace extends LiftTraceLowPriority {
implicit val ioInstance: LiftTrace[IO, IO] = new LiftTrace[IO, IO] {
def run[A](span: Span[IO])(f: Trace[IO] => IO[A]): IO[A] =
Trace.ioTrace(span).flatMap(f)
def liftK: IO ~> IO = FunctionK.id
def lowerK: IO ~> IO = FunctionK.id
}
}

private[natchez] trait LiftTraceLowPriority {
implicit def kleisliInstance[F[_]: MonadCancelThrow]: LiftTrace[F, Kleisli[F, Span[F], *]] =
new LiftTrace[F, Kleisli[F, Span[F], *]] {
def run[A](span: Span[F])(f: Trace[Kleisli[F, Span[F], *]] => Kleisli[F, Span[F], A]): F[A] =
f(Trace.kleisliInstance).run(span)
def liftK: F ~> Kleisli[F, Span[F], *] = Kleisli.liftK
def lowerK: Kleisli[F, Span[F], *] ~> F = new (Kleisli[F, Span[F], *] ~> F) {
def apply[A](ga: Kleisli[F, Span[F], A]): F[A] = ga(NoopSpan[F]())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final case class NoopSpan[F[_]: Applicative]() extends Span[F] {
Applicative[F].pure(Kernel(Map.empty))

override def span(name: String): Resource[F, Span[F]] =
Resource.eval(NoopSpan[F]().pure[F])
Resource.pure(this)

// TODO
def traceId: F[Option[String]] = none.pure[F]
Expand Down