otel4s icon indicating copy to clipboard operation
otel4s copied to clipboard

Utility functions for integrating with Java code

Open jatcwang opened this issue 1 year ago • 5 comments

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:

  • delayWithContext (Sync.delay equivalent)
  • blockingWithContext (Sync.blocking equivalent)
  • asyncWithContext (Async.async equivalent)

jatcwang avatar Mar 04 '24 20:03 jatcwang

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.

iRevive avatar Mar 06 '24 09:03 iRevive

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.

jatcwang avatar Mar 06 '24 10:03 jatcwang

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

iRevive avatar Mar 07 '24 16:03 iRevive

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

jatcwang avatar Mar 07 '24 17:03 jatcwang

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

It would be awesome, thanks in advance!

iRevive avatar Mar 07 '24 17:03 iRevive