instrument_context (instrument v0.6.1)
View SourceContext propagation for OpenTelemetry-compatible instrumentation.
This module provides context propagation using the process dictionary. Context is immutable; operations return new context values.
The context can hold arbitrary key-value pairs and is used to propagate trace context, baggage, and other cross-cutting concerns.
Summary
Functions
Attaches a context to the current process. Returns a token that must be used with detach/1 to restore the previous context.
Returns the current context from the process dictionary. If no context is attached, returns an empty context.
Detaches the current context and restores the previous one. The token must match the one returned by attach/1.
Gets a value from the context. Returns undefined if the key is not found.
Gets a value from the context with a default.
Creates a new empty context.
Removes a value from the context. Returns a new context with the value removed.
Sets the current context without storing history. Use this for processes that don't need to restore previous context, such as spawned processes that inherit context from their parent. This avoids memory leaks from accumulating context history entries.
Sets a value in the context. Returns a new context with the value set.
Spawns a linked process with the current context propagated.
Spawns a linked process in a module with the current context propagated.
Spawns a process with the current context propagated.
Spawns a process in a module with the current context propagated.
Executes a function with the given context attached. The previous context is restored after the function returns.
Types
Functions
Attaches a context to the current process. Returns a token that must be used with detach/1 to restore the previous context.
-spec current() -> context().
Returns the current context from the process dictionary. If no context is attached, returns an empty context.
-spec detach(token()) -> ok.
Detaches the current context and restores the previous one. The token must match the one returned by attach/1.
Gets a value from the context. Returns undefined if the key is not found.
Gets a value from the context with a default.
-spec new() -> context().
Creates a new empty context.
Removes a value from the context. Returns a new context with the value removed.
-spec set_current(context()) -> ok.
Sets the current context without storing history. Use this for processes that don't need to restore previous context, such as spawned processes that inherit context from their parent. This avoids memory leaks from accumulating context history entries.
Sets a value in the context. Returns a new context with the value set.
Spawns a linked process with the current context propagated.
Spawns a linked process in a module with the current context propagated.
Spawns a process with the current context propagated.
Spawns a process in a module with the current context propagated.
Executes a function with the given context attached. The previous context is restored after the function returns.