natchez icon indicating copy to clipboard operation
natchez copied to clipboard

Add a variant of `Trace.ioTrace` that takes an existing IOLocal

Open kubukoz opened this issue 1 year ago • 5 comments

In absence of a built-in IOLocal[A] => Local[IO, A] which would allow using natchezMtlTraceForLocal (might eventually be available if https://github.com/typelevel/cats-effect/pull/3429 ever gets merged and released), this would allow for some useful Java interop in a style similar to what otel4s suggests.

The ability to directly create (and thus, read afterwards) the IOLocal that holds the current span, lets some Java interop apply the following process:

  • capture the current value in an effect
  • downcast to a particular Span implementation, e.g. DDSpan
  • extract the underlying java Span, e.g. the opentracing Span
  • in a delay block, set the current span of the global Tracer instance to the span it just found.

I acknowledge this might not be a popular usecase (given nobody requested it by now), but it'd be quite helpful to my team at the moment.

kubukoz avatar Sep 11 '24 01:09 kubukoz

Kindly requesting a look @mpilquist 🥺

kubukoz avatar Sep 13 '24 14:09 kubukoz

I was going to suggest copying catsMtlEffectLocalForIO into natchez-mtl until it's adding to cats-effect, but maybe this is fine instead.

My main hesitation is that we'd want to be careful (both now and in the future) that this Trace[IO] doesn't do anything that can't be encoded using Local[IO, Span[IO]] semantics, since I think it's been concluded that Local semantics are safe, but using IOLocal directly can be dangerous in some scenarios? (See https://github.com/typelevel/cats-effect/issues/3385 for some additional discussion on this point. And to be clear, I'm sure @kubukoz has seen that, I'm just noting it for the record here.)

bpholt avatar Oct 04 '24 17:10 bpholt

yeah, I would've liked to have Local[IO, E] at the ready and to implement this in terms of Local, but I'm hopeful that we can get https://github.com/typelevel/cats-effect/pull/3429 (or similar, but indeed a solution for https://github.com/typelevel/cats-effect/issues/3385) soon, and I wouldn't want there to be an implicit conflict. I guess we could keep the instance, and then un-implicit it when CE gets one of their own?

kubukoz avatar Oct 04 '24 20:10 kubukoz

If we didn't want to publicly expose a copy of catsMtlEffectLocalForIO (although I do think including it here and then making it no longer implicit once it's available in cats-effect should be fine), one compromise would be to make a private copy and use that to implement def ioTraceLocal(local: IOLocal[Span[IO]]): Trace[IO]. Just brainstorming, really. 🤔

bpholt avatar Oct 15 '24 22:10 bpholt

Sorry for the long wait, it's a busy time at work and I have no puter time left. I think you're right, we can always hide that implicit when there's an alternative with the same behavior.

kubukoz avatar Oct 17 '24 21:10 kubukoz

With the release of Cats Effect 3.6, do we still need this PR @kubukoz?

bpholt avatar May 01 '25 15:05 bpholt

no, think it's enough to use IOLocal + https://github.com/typelevel/natchez/blob/main/modules/mtl/shared/src/main/scala/package.scala, thanks for the ping

kubukoz avatar May 01 '25 19:05 kubukoz