Tracing Issue with Background Job in Effect and OpenTelemetry Setup

Hi everyone 👋

I've setup opentelemetry on my http server.
I get traces correctly, however I'm running into an issue for one case:
In one of my endpoints, I need to trigger the run of a "background job".
I don't want to wait for the result of this job to return a response to the http client.
The issue is that traces created with withSpan("..") inside this background job are not visible.

I've simplified my issue to this snippet of code:
import { NodeSdk } from "@effect/opentelemetry";
import { ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { Effect } from "effect";

const backgroundJob = Effect.gen(function* () {
  yield* Effect.sleep("1 seconds");
  yield* Effect.log(`BackgroundJob`);
}).pipe(Effect.withSpan("BackgroundJobSpan"));

const program = Effect.gen(function* () {
  yield* Effect.log("Parent");
  yield* backgroundJob.pipe(Effect.forkDaemon);
}).pipe(Effect.withSpan("ParentSpan"));

const NodeSdkLive = NodeSdk.layer(() => ({
  resource: { serviceName: "example" },
  spanProcessor: new SimpleSpanProcessor(new ConsoleSpanExporter()),
}));

Effect.runPromise(program.pipe(Effect.provide(NodeSdkLive), Effect.catchAllCause(Effect.logError)));


I'm a beginner with Effect and I've already spent a day trying to understand what I'm missing..
I'd really appreciate if someone can give me pointers to what I miss in my understanding.

Is it that layers provided to the parent fiber are not passed to the daemon fiber?
I'd like to find a solution where I can set the tracing layer just once.

Thanks !
Was this page helpful?