Skip to content
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

Implement mapK syntax for EntryPoint and Span #648

Merged
merged 3 commits into from
Nov 19, 2022
Merged
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
30 changes: 29 additions & 1 deletion modules/core/shared/src/main/scala/EntryPoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package natchez

import cats.~>
import cats.effect.MonadCancel
import cats.effect.Resource

/**
Expand All @@ -30,5 +32,31 @@ trait EntryPoint[F[_]] {
*/
def continueOrElseRoot(name: String, kernel: Kernel): Resource[F, Span[F]]


/**
* Converts this `EntryPoint[F]` to an `EntryPoint[G]` using an `F ~> G`.
*/
def mapK[G[_]](
f: F ~> G
)(implicit F: MonadCancel[F, _], G: MonadCancel[G, _]): EntryPoint[G] = {
val outer = this

def aux(r: Resource[F, Span[F]]): Resource[G, Span[G]] = r
.map(_.mapK(f))
.mapK(f)

new EntryPoint[G] {

override def root(name: String): Resource[G, Span[G]] = aux(outer.root(name))

override def continue(
name: String,
kernel: Kernel
): Resource[G, Span[G]] = aux(outer.continue(name, kernel))

override def continueOrElseRoot(
name: String,
kernel: Kernel
): Resource[G, Span[G]] = aux(outer.continueOrElseRoot(name, kernel))
}
}
}
30 changes: 28 additions & 2 deletions modules/core/shared/src/main/scala/Span.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

package natchez

import cats.{~>, Applicative}
import cats.syntax.applicative._
import cats.data.Kleisli
import cats.effect.MonadCancel
import cats.effect.Resource
import cats.effect.Resource.ExitCase
import cats.syntax.applicative._
import cats.{~>, Applicative}
import java.net.URI

/** An span that can be passed around and used to create child spans. */
Expand Down Expand Up @@ -44,6 +45,31 @@ trait Span[F[_]] {
*/
def traceUri: F[Option[URI]]

/** Converts this `Span[F]` to a `Span[G]` using a `F ~> G`. */
def mapK[G[_]](f: F ~> G)(
implicit F: MonadCancel[F, _],
G: MonadCancel[G, _]
): Span[G] = {
val outer = this
new Span[G] {
override def put(fields: (String, TraceValue)*): G[Unit] = f(
outer.put(fields: _*)
)

override def kernel: G[Kernel] = f(outer.kernel)

override def span(name: String): Resource[G, Span[G]] = outer
.span(name)
.map(_.mapK(f))
.mapK(f)

override def traceId: G[Option[String]] = f(outer.traceId)

override def spanId: G[Option[String]] = f(outer.spanId)

override def traceUri: G[Option[URI]] = f(outer.traceUri)
}
}
}

object Span {
Expand Down