Attaching Next JS OpenTelemetry with Effect OpenTelemetry

I have an API built using the HttpApi from @effect/platform and I configure this OpenTelemetry layer:

export const TracingLayer = Layer.unwrapEffect(
  Effect.gen(function* () {
    const serviceName = yield* Config.string("OTEL_SERVICE_NAME")
    const url = yield* Config.string("OTEL_EXPORTER_OTLP_ENDPOINT")
    const headersFromConfig = yield* Config.string("OTEL_EXPORTER_OTLP_HEADERS").pipe(Config.withDefault(""))
    const headers = yield* Schema.decodeUnknown(HeadersSchema)(headersFromConfig)

    return NodeSdk.layer(() => ({
      resource: { serviceName },
      spanProcessor: new BatchSpanProcessor(new OTLPTraceExporter({ url, headers })),
    }))
  })
)


It works but all the tracers are being exported without a root span. NextJS generates a parent span when using @vercel/otel.

Previously, I used this code to attach some parts when we were incrementally adopting effect.

const getParentSpan = () => {
  const span = trace.getActiveSpan()
  return span && Tracer.makeExternalSpan(span.spanContext())
}

export const effectWithActiveSpan = (name: string, attributes?: Record<string, unknown>) => {
  const parent = getParentSpan()
  return parent
    ? flow(
        Effect.withSpan(name, { attributes }),
        Effect.withParentSpan(parent)
      )
    : flow(
        Effect.withSpan(name, { attributes })
      )
}


Now, I need to do something similar but to attach it to all the routes defined on the HttpApi without going one by one.

Is this currently possible?
image.png
Was this page helpful?