Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility functions for integrating with Java code #534

Open
jatcwang opened this issue Mar 4, 2024 · 5 comments
Open

Utility functions for integrating with Java code #534

jatcwang opened this issue Mar 4, 2024 · 5 comments

Comments

@jatcwang
Copy link

jatcwang commented Mar 4, 2024

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)
@iRevive
Copy link
Contributor

iRevive commented Mar 6, 2024

We've been exploring these utility functions: #340 (review). 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
Copy link
Author

jatcwang commented Mar 6, 2024

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
Copy link
Contributor

iRevive commented Mar 7, 2024

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

@jatcwang
Copy link
Author

jatcwang commented Mar 7, 2024

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

@iRevive
Copy link
Contributor

iRevive commented Mar 7, 2024

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

It would be awesome, thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants