evanderkoogh / otel-cf-workers

An OpenTelemetry compatible library for instrumenting and exporting traces for Cloudflare Workers
BSD 3-Clause "New" or "Revised" License
208 stars 39 forks source link

Help with `trace.getActiveSpan()` and `startActiveSpan()` #138

Closed mw10013 closed 1 month ago

mw10013 commented 1 month ago

Ask for help with trace.getActiveSpan() and startActiveSpan().

I updated the dependencies of examples/worker and switched the config to baselime instead of honeycomb. Able to run locally and get traces and events to appear in baselime. However, I don't see any of the custom attributes set with trace.getActiveSpan()?.setAttribute().

https://github.com/evanderkoogh/otel-cf-workers/blob/6f1c79056776024fd3e816b9e3991527e7217510/examples/worker/src/handler.ts#L17-L29

Adding console.log's and an explicit startActiveSpan() shows that getActiveSpan() seems to return undefined. Also don't know what to make of NonREcordingSpan2 with zeroed traceId and spanId.

const handleRest = async (request: Request, env: Env, ctx: ExecutionContext): Promise<Response> => {
    trace.getActiveSpan()?.setAttribute('http.route', '/*')
    console.log('handleRest: active span: %o', trace.getActiveSpan())

    const tracer = trace.getTracer('my_own_tracer_name')
    tracer.startActiveSpan('name', (span) => {
        span.setAttribute('foo', 'bar')
        console.log('handleRest: start active span: %o', span)
        span.end()
    })
handleRest: active span: undefined
handleRest: start active span: NonRecordingSpan2 {
  _spanContext: {
    traceId: '00000000000000000000000000000000',
    spanId: '0000000000000000',
    traceFlags: 0
  }
}

Would appreciate any guidance on how I can get an actual span from getActiveSpan() and get span within startActiveSpan() to show up in baselime. Thanks.

DaniFoldi commented 1 month ago

Duplicate of https://github.com/evanderkoogh/otel-cf-workers/issues/115#issuecomment-2041365703, please make sure your direct opentelemetry dependencies can be satisfied by versions used by the library.

mw10013 commented 1 month ago

Thanks. That worked for me.