Options
1) withSession (recommended for endpoint-based agents)
Scopes the session to a single function (request handler, task), without ending the session.2) withLucidic (one-shot convenience)
Runs a full session lifecycle for short tasks (init → context → run → end → clear).autoEnd
.
3) Manual (advanced)
Explicitly set/clear the session for detached async roots (queues, timers, workers) or custom flows.Guidance
- Prefer
withSession
for endpoint-based/persistent sessions. - Use
withLucidic
for one-shot tasks that should end when work finishes. - For background jobs or new async roots, call
setActiveSession(sessionId)
in that root or wrap the work withwithSession(sessionId, fn)
. - The exporter prefers per-span-stamped
lucidic.session_id
(from ALS) and falls back to the global session id for compatibility.
Flushing & AutoEnd
- When
autoEnd: true
(default), the SDK ends the session on process exit and shuts down the tracer provider gracefully. - Even if
autoEnd: false
, the SDK still force-flushes spans onbeforeExit
/signals to reduce lost spans. Use explicitendSession
when appropriate.