From b0e4cb2ccdf64f095d2e08562e220e2b89ea051c Mon Sep 17 00:00:00 2001 From: Maksym Ochenashko Date: Sun, 15 Dec 2024 12:30:48 +0200 Subject: [PATCH] core-common: remove public 'localForIOLocal' instance --- .../otel4s/context/LocalProvider.scala | 46 ++++++++----------- .../otel4s/instances/AllInstances.scala | 19 -------- .../otel4s/instances/LocalInstances.scala | 42 ----------------- .../typelevel/otel4s/instances/package.scala | 25 ---------- .../otel4s/context/LocalProviderSuite.scala | 4 +- .../instances/LocalInstancesSuite.scala | 35 -------------- docs/tracing-context-propagation.md | 3 +- .../oteljava/trace/TraceScopeImplSuite.scala | 3 +- .../sdk/trace/SdkSpanBuilderSuite.scala | 3 +- .../otel4s/sdk/trace/SdkTraceScopeSuite.scala | 3 +- 10 files changed, 24 insertions(+), 159 deletions(-) delete mode 100644 core/common/src/main/scala/org/typelevel/otel4s/instances/AllInstances.scala delete mode 100644 core/common/src/main/scala/org/typelevel/otel4s/instances/LocalInstances.scala delete mode 100644 core/common/src/main/scala/org/typelevel/otel4s/instances/package.scala delete mode 100644 core/common/src/test/scala/org/typelevel/otel4s/instances/LocalInstancesSuite.scala diff --git a/core/common/src/main/scala/org/typelevel/otel4s/context/LocalProvider.scala b/core/common/src/main/scala/org/typelevel/otel4s/context/LocalProvider.scala index 688a1b522..5749c36c8 100644 --- a/core/common/src/main/scala/org/typelevel/otel4s/context/LocalProvider.scala +++ b/core/common/src/main/scala/org/typelevel/otel4s/context/LocalProvider.scala @@ -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]]. * @@ -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" } @@ -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" @@ -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" @@ -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 } diff --git a/core/common/src/main/scala/org/typelevel/otel4s/instances/AllInstances.scala b/core/common/src/main/scala/org/typelevel/otel4s/instances/AllInstances.scala deleted file mode 100644 index 83702f62e..000000000 --- a/core/common/src/main/scala/org/typelevel/otel4s/instances/AllInstances.scala +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2022 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.typelevel.otel4s.instances - -trait AllInstances extends LocalInstances diff --git a/core/common/src/main/scala/org/typelevel/otel4s/instances/LocalInstances.scala b/core/common/src/main/scala/org/typelevel/otel4s/instances/LocalInstances.scala deleted file mode 100644 index 1c48dc03a..000000000 --- a/core/common/src/main/scala/org/typelevel/otel4s/instances/LocalInstances.scala +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2022 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.typelevel.otel4s.instances - -import cats.Applicative -import cats.Functor -import cats.effect.IOLocal -import cats.effect.LiftIO -import cats.effect.MonadCancelThrow -import cats.mtl.Local - -trait LocalInstances { - - // We hope this instance is moved into Cats Effect. - // See https://github.com/typelevel/cats-effect/pull/3429 - implicit final def localForIOLocal[F[_]: MonadCancelThrow: LiftIO, E](implicit - ioLocal: IOLocal[E] - ): Local[F, E] = - new Local[F, E] { - def applicative: Applicative[F] = - Applicative[F] - def ask[E2 >: E]: F[E2] = - Functor[F].widen[E, E2](ioLocal.get.to[F]) - def local[A](fa: F[A])(f: E => E): F[A] = - MonadCancelThrow[F].bracket(ioLocal.modify(e => (f(e), e)).to[F])(_ => fa)(ioLocal.set(_).to[F]) - } - -} diff --git a/core/common/src/main/scala/org/typelevel/otel4s/instances/package.scala b/core/common/src/main/scala/org/typelevel/otel4s/instances/package.scala deleted file mode 100644 index 97ac89a29..000000000 --- a/core/common/src/main/scala/org/typelevel/otel4s/instances/package.scala +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2022 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.typelevel.otel4s - -package object instances { - - object all extends AllInstances - - object local extends LocalInstances - -} diff --git a/core/common/src/test/scala/org/typelevel/otel4s/context/LocalProviderSuite.scala b/core/common/src/test/scala/org/typelevel/otel4s/context/LocalProviderSuite.scala index e85259df0..b7e326f4d 100644 --- a/core/common/src/test/scala/org/typelevel/otel4s/context/LocalProviderSuite.scala +++ b/core/common/src/test/scala/org/typelevel/otel4s/context/LocalProviderSuite.scala @@ -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, diff --git a/core/common/src/test/scala/org/typelevel/otel4s/instances/LocalInstancesSuite.scala b/core/common/src/test/scala/org/typelevel/otel4s/instances/LocalInstancesSuite.scala deleted file mode 100644 index ab79a734a..000000000 --- a/core/common/src/test/scala/org/typelevel/otel4s/instances/LocalInstancesSuite.scala +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2022 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.typelevel.otel4s.instances - -import cats.effect.IO -import cats.effect.IOLocal -import cats.effect.testkit.TestInstances -import cats.mtl.laws.discipline.LocalTests -import munit.DisciplineSuite -import org.scalacheck.Arbitrary.arbString -import org.typelevel.otel4s.instances.local._ - -class LocalInstancesSuite extends DisciplineSuite with TestInstances { - implicit val ticker: Ticker = Ticker() - - unsafeRun { - IOLocal("").map { implicit ioLocal => - checkAll("IOLocal.LocalLaws", LocalTests[IO, String].local[String, Int]) - } - } -} diff --git a/docs/tracing-context-propagation.md b/docs/tracing-context-propagation.md index e401a1cf4..3da35e4b0 100644 --- a/docs/tracing-context-propagation.md +++ b/docs/tracing-context-propagation.md @@ -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 @@ -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)) } ``` diff --git a/oteljava/trace/src/test/scala/org/typelevel/otel4s/oteljava/trace/TraceScopeImplSuite.scala b/oteljava/trace/src/test/scala/org/typelevel/otel4s/oteljava/trace/TraceScopeImplSuite.scala index c3aa06d73..dfcb0db30 100644 --- a/oteljava/trace/src/test/scala/org/typelevel/otel4s/oteljava/trace/TraceScopeImplSuite.scala +++ b/oteljava/trace/src/test/scala/org/typelevel/otel4s/oteljava/trace/TraceScopeImplSuite.scala @@ -18,7 +18,6 @@ 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 @@ -26,7 +25,7 @@ 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] } diff --git a/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBuilderSuite.scala b/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBuilderSuite.scala index 5ff4d4e27..960ac3ca3 100644 --- a/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBuilderSuite.scala +++ b/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBuilderSuite.scala @@ -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 @@ -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] } diff --git a/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkTraceScopeSuite.scala b/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkTraceScopeSuite.scala index 7df55e65b..afe70a953 100644 --- a/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkTraceScopeSuite.scala +++ b/sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/SdkTraceScopeSuite.scala @@ -18,7 +18,6 @@ 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 @@ -26,7 +25,7 @@ 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] }