Skip to content

Commit

Permalink
Merge pull request #414 from iRevive/move-io-local-instance
Browse files Browse the repository at this point in the history
Move `instances` to the `core-common` package
  • Loading branch information
iRevive authored Dec 26, 2023
2 parents 30aae3c + 9c9b56c commit 1411b0a
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 35 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ lazy val `core-common` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
"org.typelevel" %%% "cats-mtl" % CatsMtlVersion,
"org.typelevel" %%% "vault" % VaultVersion % Test,
"org.typelevel" %%% "cats-laws" % CatsVersion % Test,
"org.typelevel" %%% "cats-mtl-laws" % CatsMtlVersion % Test,
"org.typelevel" %%% "cats-effect-testkit" % CatsEffectVersion % Test,
"org.typelevel" %%% "discipline-munit" % MUnitDisciplineVersion % Test,
"lgbt.princess" %%% "platform" % PlatformVersion % Test
)
Expand Down Expand Up @@ -247,8 +249,6 @@ lazy val `oteljava-common` = project
"org.typelevel" %%% "cats-effect" % CatsEffectVersion,
"org.typelevel" %%% "cats-mtl" % CatsMtlVersion,
"io.opentelemetry" % "opentelemetry-api" % OpenTelemetryVersion,
"org.typelevel" %%% "discipline-munit" % MUnitDisciplineVersion % Test,
"org.typelevel" %%% "cats-mtl-laws" % CatsMtlVersion % Test,
"org.typelevel" %%% "cats-effect-testkit" % CatsEffectVersion % Test,
"io.opentelemetry" % "opentelemetry-sdk-testing" % OpenTelemetryVersion % Test
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.typelevel.otel4s.oteljava
package org.typelevel.otel4s.instances

import cats.Applicative
import cats.Functor
Expand All @@ -23,19 +23,22 @@ import cats.effect.LiftIO
import cats.effect.MonadCancelThrow
import cats.mtl.Local

object instances {
trait LocalInstances {

// We hope this instance is moved into Cats Effect.
implicit def localForIoLocal[F[_]: MonadCancelThrow: LiftIO, E](implicit
// 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 =
def applicative: Applicative[F] =
Applicative[F]
def ask[E2 >: E] =
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])
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

package org.typelevel.otel4s.oteljava
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.oteljava.instances._
import org.typelevel.otel4s.instances.local._

class InstancesSuite extends DisciplineSuite with TestInstances {
class LocalInstancesSuite extends DisciplineSuite with TestInstances {
implicit val ticker: Ticker = Ticker()

unsafeRun {
Expand Down
2 changes: 1 addition & 1 deletion docs/instrumentation/tracing-java-interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ It can be constructed in the following way:
import cats.effect._
import cats.mtl.Local
import cats.syntax.functor._
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 org.typelevel.otel4s.oteljava.instances._ // brings Local derived from IOLocal
import io.opentelemetry.api.GlobalOpenTelemetry

def createOtel4s[F[_]: Async](implicit L: Local[F, Context]): F[OtelJava[F]] =
Expand Down
2 changes: 1 addition & 1 deletion docs/tracing-context-propagation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ You can find both examples below and choose which one suits your requirements.
import cats.effect._
import cats.mtl.Local
import cats.syntax.functor._
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 org.typelevel.otel4s.oteljava.instances._ // brings Local derived from IOLocal
import io.opentelemetry.api.GlobalOpenTelemetry

def createOtel4s[F[_]: Async](implicit L: Local[F, Context]): F[OtelJava[F]] =
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/scala/PekkoHttpExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import org.apache.pekko.http.scaladsl.server.Directives._
import org.apache.pekko.http.scaladsl.server.Route
import org.apache.pekko.util.ByteString
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.oteljava.OtelJava
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.oteljava.instances._
import org.typelevel.otel4s.trace.Tracer

import scala.concurrent.Future
Expand Down Expand Up @@ -78,7 +78,7 @@ object PekkoHttpExample extends IOApp.Simple {

def run: IO[Unit] =
IOLocal(Context.root).flatMap { implicit ioLocal: IOLocal[Context] =>
implicit val local: Local[IO, Context] = localForIoLocal
implicit val local: Local[IO, Context] = localForIOLocal
val otelJava: OtelJava[IO] = OtelJava.local(GlobalOpenTelemetry.get())

otelJava.tracerProvider.get("com.example").flatMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import io.opentelemetry.sdk.{OpenTelemetrySdk => JOpenTelemetrySdk}
import io.opentelemetry.sdk.common.CompletableResultCode
import org.typelevel.otel4s.Otel4s
import org.typelevel.otel4s.context.propagation.ContextPropagators
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.metrics.MeterProvider
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.oteljava.context.LocalContext
import org.typelevel.otel4s.oteljava.context.propagation.PropagatorConverters._
import org.typelevel.otel4s.oteljava.instances._
import org.typelevel.otel4s.oteljava.metrics.Metrics
import org.typelevel.otel4s.oteljava.trace.Traces
import org.typelevel.otel4s.trace.TracerProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import cats.effect.LiftIO
import cats.effect.Sync
import io.opentelemetry.api.{OpenTelemetry => JOpenTelemetry}
import org.typelevel.otel4s.context.propagation.ContextPropagators
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.oteljava.context.LocalContext
import org.typelevel.otel4s.oteljava.instances._
import org.typelevel.otel4s.trace.TracerProvider

trait Traces[F[_]] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import org.typelevel.otel4s.context.propagation.{
PassThroughPropagator => SPassThroughPropagator
}
import org.typelevel.otel4s.context.propagation.ContextPropagators
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.oteljava.context.Context
import org.typelevel.otel4s.oteljava.context.propagation.PropagatorConverters._
import org.typelevel.otel4s.oteljava.instances._
import org.typelevel.otel4s.trace.Span
import org.typelevel.otel4s.trace.Tracer
import org.typelevel.otel4s.trace.TracerProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

package org.typelevel.otel4s.sdk.trace

import cats.Applicative
import cats.Functor
import cats.effect.IO
import cats.effect.IOLocal
import cats.effect.LiftIO
import cats.effect.MonadCancelThrow
import cats.mtl.Local
import munit.CatsEffectSuite
import munit.ScalaCheckEffectSuite
import org.scalacheck.Arbitrary
import org.scalacheck.effect.PropF
import org.typelevel.otel4s.instances.local._
import org.typelevel.otel4s.sdk.context.Context
import org.typelevel.otel4s.trace.SpanContext

Expand Down Expand Up @@ -294,17 +290,4 @@ class SdkTraceScopeSuite extends CatsEffectSuite with ScalaCheckEffectSuite {
SdkTraceScope.fromLocal[IO]
}

private implicit def localForIoLocal[F[_]: MonadCancelThrow: LiftIO, E](
implicit ioLocal: IOLocal[E]
): Local[F, E] =
new Local[F, E] {
def applicative =
Applicative[F]
def ask[E2 >: E] =
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])
}
}

0 comments on commit 1411b0a

Please sign in to comment.