typelevel / otel4s

An OpenTelemetry library for Scala based on Cats-Effect
https://typelevel.org/otel4s
Apache License 2.0
168 stars 34 forks source link

Utility functions for integrating with Java code #534

Open jatcwang opened 6 months ago

jatcwang commented 6 months ago

Thanks for the detailed docs on integrating with libs that rely on java OTel!

Are you open to adding some standard utility functions for integrating with Java code relying on Java OTel?

I'm thinking of:

iRevive commented 6 months ago

We've been exploring these utility functions: https://github.com/typelevel/otel4s/pull/340#pullrequestreview-1686131253. We may add utility methods in the future, but we should consider all benefits and drawbacks.


With the upcoming changes in the 0.5.0 (you can try 0.5.0-RC1), the example can be slightly simplified:

Before:

def createOtel4s[F[_]: Async](implicit L: Local[F, Context]): F[OtelJava[F]] =
  Async[F].delay(GlobalOpenTelemetry.get).map(OtelJava.local[F])

def program[F[_]: Async](otel4s: OtelJava[F])(implicit L: Local[F, Context]): F[Unit] = {
  val _ = (otel4s, L) // both OtelJava and Local[F, Context] are available here
  Async[F].unit
}

val run: IO[Unit] =
  IOLocal(Context.root).flatMap { implicit ioLocal: IOLocal[Context] =>
    createOtel4s[IO].flatMap(otel4s => program(otel4s))
  }

After:

def program[F[_]: Async](otel4s: OtelJava[F]): F[Unit] = {
  val local: Local[IO, Context] = otel4s.localContext
  Async[F].unit
}

def run: IO[Unit] = 
  OtelJava.autoConfigured[IO]() { otel4s =>
    program(otel4s)
  }

P.S. I will update an example on the site.

jatcwang commented 6 months ago

Thanks @iRevive. I can see that we're trying to provide an even easier integration with java code relying on ThreadLocal context propagation.

In that case, how about we add the suggested methods to the documentation? In particular I think asyncWithContext has some nuance which will be good to document.

iRevive commented 6 months ago

Yeah, that sounds good. I guess we can provide more detailed and enhanced examples.

jatcwang commented 6 months ago

Happy to do the PR for the doc update :) (including the simplified setup code snippet you provide)

iRevive commented 6 months ago

Happy to do the PR for the doc update :) (including the simplified setup code snippet you provide)

It would be awesome, thanks in advance!