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

Add support for Baggage #431

Closed
fugafree opened this issue Jan 13, 2024 · 12 comments
Closed

Add support for Baggage #431

fugafree opened this issue Jan 13, 2024 · 12 comments

Comments

@fugafree
Copy link

It would be nice to have baggage support in the library for getting and setting baggage on the context.
In Java there is a baggage builder and the baggage can be set on the context (jContext.`with`(Baggage.builder().build()))

@fugafree
Copy link
Author

fugafree commented Jan 13, 2024

I tried to find some easy workaround, but I failed to do so. The only one I could come up with is to manipulate the underlying context but that would be problematic as I don't know how to get easily the Local[IO, Context]. In "Tracing - interop with Java-instrumented libraries", you suggest to use OtelJava.local, but that would mean I would lose the ability to use OtelJava.resource and the already existing asyncFromCompletableResultCode, which is sad.

Can you help me how to manipulate the context more easily? Why is the Context hidden in Otel4s? It would be nice to have something like this: otel4s.getContext or otel4s.changeContext(ctx => ctx).

@fugafree
Copy link
Author

Also let me note "Tracing - interop with Java-instrumented libraries" mentions these imports:

import org.typelevel.otel4s.instances.local._ // brings Local derived from IOLocal
import org.typelevel.otel4s.oteljava.OtelJava

None of them exists in 0.4.0.

@iRevive
Copy link
Contributor

iRevive commented Jan 15, 2024

Hello @fugafree.

Also let me note "Tracing - interop with Java-instrumented libraries" mentions these imports:

import org.typelevel.otel4s.instances.local._ // brings Local derived from IOLocal
import org.typelevel.otel4s.oteljava.OtelJava

None of them exists in 0.4.0.

The site is built from the most recent main branch, and these changes have not been released yet.

In the 0.4.0 you need to use:

import org.typelevel.otel4s.java.instances._ // brings Local derived from IOLocal
import org.typelevel.otel4s.java.OtelJava

Here is an example of how to run an effect with custom baggage.
You should use Local[F, Context].local(...) to modify the context.

//> using scala 2
//> using toolkit typelevel:latest
//> using lib org.typelevel::otel4s-java:0.4.0
//> using lib "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.34.1"

import cats.effect._
import cats.mtl.syntax.local._
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.baggage.Baggage
import org.typelevel.otel4s.java.OtelJava
import org.typelevel.otel4s.java.context.Context
import org.typelevel.otel4s.java.instances._ // bring Local for IOLocal

object BaggageExample extends IOApp.Simple {
  def run: IO[Unit] = {
    IOLocal(Context.root).flatMap { implicit ioLocal: IOLocal[Context] =>
      val otel = OtelJava.local[IO](GlobalOpenTelemetry.get)
      otel.tracerProvider.get("tracer").flatMap { implicit tracer =>
        val baggage = Baggage.builder().put("key", "value").build()

        tracer.span("my-span")
          .use(span => IO.println("my span: " + span))
          .local[Context](ctx => Context.wrap(ctx.underlying.`with`(baggage))) // add the baggage
      }
    }
  }
}

@iRevive
Copy link
Contributor

iRevive commented Jan 15, 2024

Can you help me how to manipulate the context more easily? Why is the Context hidden in Otel4s? It would be nice to have something like this: otel4s.getContext or otel4s.changeContext(ctx => ctx).

We may expose utility methods eventually. Currently, I'm actively working on the Scala implementation of the tracing SDK module. Once we have it in place, we can make a proper decision on how to extend the API to make sure both implementations support it.

@NthPortal
Copy link
Contributor

I opine that OtelJava should retain and expose a direct reference to its LocalContext, to make things easier

@fugafree
Copy link
Author

Hello @iRevive, thank you for the answers, and that you already started to work on it. In the mentioned pull request you summarised the challenge pretty good.
Can't wait the result!

@NthPortal
Copy link
Contributor

see #464 for exposing it from OtelJava

@fugafree
Copy link
Author

Related: #452, adds Baggage to sdk-common

@iRevive
Copy link
Contributor

iRevive commented Feb 27, 2024

With the upcoming release you can inject the baggage in the following way:

val otel4s: OtelJava[F] = ???
val program: F[Unit] = ??? 
val baggage = Baggage.builder().put("key", "value").build()

otel4s.localContext.local(program)(ctx => Context.wrap(ctx.underlying.`with`(baggage)))

@fugafree fugafree reopened this Feb 27, 2024
@fugafree
Copy link
Author

Thank you! Much more easier now. Sadly the Baggage Java API is still a mess, and modifying a value is still a paint, but at least doable.
(Sorry for the accidental ticket close.)

@iRevive
Copy link
Contributor

iRevive commented Feb 27, 2024

In some sense, it's done, so we can close it off 🙂

@iRevive iRevive closed this as completed Feb 27, 2024
@fugafree
Copy link
Author

Exposing the underlying Java context via local is indeed done, but I would rather call that a workaround than a solution.
Originally I was thinking about something like

otel4s.baggage.add("key", "value"): IO[Unit]
otel4s.baggage.get("key"): IO[Option[String]]

Since next to SpanContext Baggage is the other main signal in OpenTelemetry, maybe it would deserve a proper API.

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

3 participants