getContextValue()
Get a value from the trace context by key
Overview
getContextValue() retrieves a value that was previously stored in the trace context. Useful for passing data between spans without function parameters.
Signature
Basic Usage
Sharing Data Across Spans
Request Context Propagation
Feature Flags
Default Values
Parameters
key
The key to retrieve from the context
Return Value
Returns the value associated with the key, or undefined if the key doesn’t exist.
Best Practices
- Use context to share data between spans without passing parameters
- Store request metadata, user info, or feature flags in context
- Provide default values when retrieving optional context data
- Context is scoped to the current trace and its child spans
- Values must be set with
setContextValue()before retrieval - Context is useful for cross-cutting concerns like request IDs