feral icon indicating copy to clipboard operation
feral copied to clipboard

TracedHandler should use an existing Local[F, Span[F]]

Open bpholt opened this issue 2 months ago • 1 comments

I have a Lambda with an initialization Resource that (simplified) looks like this:

for
  xray <- XRay.entryPoint[IO]()
  given Local[IO, Span[IO]] <- IO.local(Span.noop[IO]).toResource
  client <- EmberClientBuilder.default[F].build.map(NatchezMiddleware.client(_))
  region <- Env[IO].get("AWS_REGION").liftEitherT(new RuntimeException("missing AWS_REGION environment variable")).map(AwsRegion(_)).rethrowT.toResource
  awsEnv <- AwsEnvironment.default(client, region)
  kms <- AwsClient(KMS, awsEnv) // smithy4s AWS KMS client
yield implicit inv =>
  given KernelSource[CloudFormationCustomResourceRequest[Foo]] = KernelSource.emptyKernelSource

  TracedHandler(xray):
    CloudFormationCustomResource(client, new FooHandler(client, kms))

When I went to use TracedHandler.apply, I noticed that it was constructing its own IOLocal to hold the trace state. I don't think that will be in sync with the given Local[IO, Span[IO]] <- IO.local(Span.noop[IO]) built in the resource, and I want them to be in sync because I want the Client[F] to have the same trace state as the rest of the handler, so I think we need a new way to build a TracedHandler that can inherit the ambient Local[F, Span[F]].

I started off by adding this as a separate TracedHandler.local method, but I didn't think that was a particularly good name, so I ended up basically replacing TracedHandler.apply (in a binary-compatible way).

bpholt avatar Oct 14 '25 21:10 bpholt

~~This is failing because we're on an old Scala.js version. Hopefully we can merge #592, and then I'll rebase this to take advantage of the update. Until then, this can be reviewed for the concepts, but CI won't pass.~~

Rebased and now it successfully builds!

bpholt avatar Oct 14 '25 22:10 bpholt