Skip to content

Commit

Permalink
core-common: remove public 'localForIOLocal' instance
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Dec 17, 2024
1 parent 9f8366b commit b0e4cb2
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import cats.effect.IOLocal
import cats.effect.LiftIO
import cats.effect.MonadCancelThrow
import cats.mtl.Local
import org.typelevel.otel4s.instances.local._

/** A utility class to simplify the creation of the [[cats.mtl.Local Local]].
*
Expand Down Expand Up @@ -73,15 +72,9 @@ object LocalProvider extends LocalProviderLowPriority {
* @tparam Ctx
* the type of the context
*/
def fromIOLocal[F[_]: MonadCancelThrow: LiftIO, Ctx](
ioLocal: IOLocal[Ctx]
): LocalProvider[F, Ctx] =
def fromIOLocal[F[_]: MonadCancelThrow: LiftIO, Ctx](ioLocal: IOLocal[Ctx]): LocalProvider[F, Ctx] =
new LocalProvider[F, Ctx] {
val local: F[Local[F, Ctx]] =
MonadCancelThrow[F].pure(
localForIOLocal[F, Ctx](implicitly, implicitly, ioLocal)
)

val local: F[Local[F, Ctx]] = MonadCancelThrow[F].pure(localForIOLocal[F, Ctx](ioLocal))
override def toString: String = "LocalProvider.fromIOLocal"
}

Expand All @@ -96,9 +89,7 @@ object LocalProvider extends LocalProviderLowPriority {
* @tparam Ctx
* the type of the context
*/
def fromLocal[F[_]: Applicative, Ctx](
l: Local[F, Ctx]
): LocalProvider[F, Ctx] =
def fromLocal[F[_]: Applicative, Ctx](l: Local[F, Ctx]): LocalProvider[F, Ctx] =
new LocalProvider[F, Ctx] {
val local: F[Local[F, Ctx]] = Applicative[F].pure(l)
override def toString: String = "LocalProvider.fromLocal"
Expand All @@ -118,16 +109,11 @@ object LocalProvider extends LocalProviderLowPriority {
* @tparam Ctx
* the type of the context
*/
def fromLiftIO[
F[_]: MonadCancelThrow: LiftIO,
Ctx: Contextual
]: LocalProvider[F, Ctx] =
def fromLiftIO[F[_]: MonadCancelThrow: LiftIO, Ctx: Contextual]: LocalProvider[F, Ctx] =
new LocalProvider[F, Ctx] {
def local: F[Local[F, Ctx]] =
IOLocal(Contextual[Ctx].root)
.map { implicit ioLocal: IOLocal[Ctx] =>
localForIOLocal[F, Ctx](implicitly, implicitly, ioLocal)
}
.map(ioLocal => localForIOLocal[F, Ctx](ioLocal))
.to[F]

override def toString: String = "LocalProvider.fromLiftIO"
Expand All @@ -139,19 +125,25 @@ object LocalProvider extends LocalProviderLowPriority {
](implicit ioLocal: IOLocal[Ctx]): LocalProvider[F, Ctx] =
LocalProvider.fromIOLocal(ioLocal)

implicit def liftFromLocal[
F[_]: Applicative,
Ctx
](implicit local: Local[F, Ctx]): LocalProvider[F, Ctx] =
implicit def liftFromLocal[F[_]: Applicative, Ctx](implicit local: Local[F, Ctx]): LocalProvider[F, Ctx] =
LocalProvider.fromLocal(local)

/** Cats Effect 3.6 introduced `IOLocal#asLocal`. However, we need a variation for a polymorphic type.
*/
private def localForIOLocal[F[_]: MonadCancelThrow: LiftIO, Ctx](ioLocal: IOLocal[Ctx]): Local[F, Ctx] =
new Local[F, Ctx] {
def applicative: Applicative[F] =
Applicative[F]
def ask[E2 >: Ctx]: F[E2] =
MonadCancelThrow[F].widen[Ctx, E2](ioLocal.get.to[F])
def local[A](fa: F[A])(f: Ctx => Ctx): F[A] =
MonadCancelThrow[F].bracket(ioLocal.modify(e => (f(e), e)).to[F])(_ => fa)(ioLocal.set(_).to[F])
}
}

sealed trait LocalProviderLowPriority { self: LocalProvider.type =>

implicit def liftFromLiftIO[
F[_]: MonadCancelThrow: LiftIO,
Ctx: Contextual
]: LocalProvider[F, Ctx] =
implicit def liftFromLiftIO[F[_]: MonadCancelThrow: LiftIO, Ctx: Contextual]: LocalProvider[F, Ctx] =
LocalProvider.fromLiftIO

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class LocalProviderSuite extends CatsEffectSuite {

test("lift LocalProvider from implicit Local (1)") {
IOLocal(VaultContext.root).map { ioLocal =>
import org.typelevel.otel4s.instances.local.localForIOLocal
implicit val local: Local[IO, VaultContext] =
localForIOLocal(implicitly, implicitly, ioLocal)
implicit val local: Local[IO, VaultContext] = ioLocal.asLocal

assertEquals(
LocalProvider[IO, VaultContext].toString,
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions docs/tracing-context-propagation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ You can find both examples below and choose which one suits your requirements.
import cats.effect._
import cats.mtl.Local
import cats.syntax.flatMap._
import org.typelevel.otel4s.instances.local._ // brings Local derived from IOLocal
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.oteljava.OtelJava
import io.opentelemetry.api.GlobalOpenTelemetry
Expand All @@ -47,7 +46,7 @@ def program[F[_]: Async](otel4s: OtelJava[F]): F[Unit] = {
}

val run: IO[Unit] =
IOLocal(Context.root).flatMap { implicit ioLocal: IOLocal[Context] =>
IOLocal(Context.root).map(_.asLocal).flatMap { implicit local: Local[IO, Context] =>
createOtel4s[IO].flatMap(otel4s => program(otel4s))
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ package org.typelevel.otel4s.oteljava.trace

import cats.effect.IO
import cats.effect.IOLocal
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.trace.TraceScope
import org.typelevel.otel4s.trace.TraceScopeSuite

class TraceScopeImplSuite extends TraceScopeSuite[Context, Context.Key] {

protected def createTraceScope: IO[TraceScope[IO, Context]] =
IOLocal(Context.root).map { implicit ioLocal =>
IOLocal(Context.root).map(_.asLocal).map { implicit local =>
TraceScopeImpl.fromLocal[IO]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.scalacheck.Gen
import org.scalacheck.Test
import org.scalacheck.effect.PropF
import org.typelevel.otel4s.Attributes
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.sdk.TelemetryResource
import org.typelevel.otel4s.sdk.common.InstrumentationScope
import org.typelevel.otel4s.sdk.context.Context
Expand Down Expand Up @@ -146,7 +145,7 @@ class SdkSpanBuilderSuite extends CatsEffectSuite with ScalaCheckEffectSuite {
}

private def createTraceScope: IO[TraceScope[IO, Context]] =
IOLocal(Context.root).map { implicit ioLocal =>
IOLocal(Context.root).map(_.asLocal).map { implicit local =>
SdkTraceScope.fromLocal[IO]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ package org.typelevel.otel4s.sdk.trace

import cats.effect.IO
import cats.effect.IOLocal
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.sdk.context.Context
import org.typelevel.otel4s.trace.TraceScope
import org.typelevel.otel4s.trace.TraceScopeSuite

class SdkTraceScopeSuite extends TraceScopeSuite[Context, Context.Key] {

protected def createTraceScope: IO[TraceScope[IO, Context]] =
IOLocal(Context.root).map { implicit ioLocal =>
IOLocal(Context.root).map(_.asLocal).map { implicit local =>
SdkTraceScope.fromLocal[IO]
}

Expand Down

0 comments on commit b0e4cb2

Please sign in to comment.