InvergeDocs
PythonAPI reference

Context

Ambient telemetry context — who and what the current work belongs to.

from nexus import context

Ambient telemetry context — who and what the current work belongs to.

Every event, log line and error carries identity (distinct_id), a session (session_key) and device/app fields. Threading those through every call by hand is miserable, so the SDK keeps them in a contextvars variable that is correct per-thread and per-async-task: bind once at the edge of a request, and everything captured underneath inherits it.

from nexus import context

with context.scope(distinct_id="user_123", url="/checkout"):
    nexus.events.track("order_placed", {"total": 42})   # carries user_123

The framework integrations bind this for you on every request.

SymbolKindSummary
TelemetryContextclassIdentity + device fields attached to telemetry captured in this scope.
current()functionThe context in force for this thread / async task.
bind()functionMerge values into the current context.
unbind()functionRestore the context captured by bind.
scope()functionBind values for the duration of the with block.
identify()functionSet the current user (and optionally more fields) for this context.
clear()functionDrop the whole context — no user, no session, no device.
reset()functionForget the user and start a new session — call this on logout.
new_session_key()functionA fresh opaque session key.
new_device_key()functionA fresh opaque device key.

TelemetryContext

class

Identity + device fields attached to telemetry captured in this scope.

View source

merged()

method
def merged(**changes) -> 'TelemetryContext'

A copy with the non-None changes applied.

Parameters

Prop

Type

Returns 'TelemetryContext'

View source

as_dict()

method
def as_dict() -> Dict[str, Any]

Snake_case dict of the set fields (properties included).

Returns Dict[str, Any]

View source

wire()

method
def wire() -> Dict[str, Any]

camelCase dict of the set identity fields, ready for the API.

Returns Dict[str, Any]

View source

Functions

current()

function
def current() -> TelemetryContext

The context in force for this thread / async task.

Returns TelemetryContext

View source

bind()

function
def bind(**values) -> 'contextvars.Token[TelemetryContext]'

Merge values into the current context.

Returns a token; pass it to unbind to restore the previous state. Prefer scope, which does that for you.

Parameters

Prop

Type

Returns 'contextvars.Token[TelemetryContext]'

View source

unbind()

function
def unbind(token) -> None

Restore the context captured by bind.

Parameters

Prop

Type

View source

scope()

function
def scope(**values) -> Iterator[TelemetryContext]

Bind values for the duration of the with block.

with context.scope(distinct_id="u_1", url=request.path):
    ...

Parameters

Prop

Type

Returns Iterator[TelemetryContext]

View source

identify()

function
def identify(distinct_id, **values) -> None

Set the current user (and optionally more fields) for this context.

This only sets local context. To also upsert the person server-side, call client.sessions.identify(...) (or client.identify(...), which does both).

Parameters

Prop

Type

View source

clear()

function
def clear() -> None

Drop the whole context — no user, no session, no device.

reset is what you want on logout (it keeps the device and starts a fresh session); this is for tests and worker teardown.

View source

reset()

function
def reset(*, keep_device=True) -> None

Forget the user and start a new session — call this on logout.

Parameters

Prop

Type

View source

new_session_key()

function
def new_session_key() -> str

A fresh opaque session key.

Returns str

View source

new_device_key()

function
def new_device_key() -> str

A fresh opaque device key.

Returns str

View source

On this page