Propagate
This module provides a thin wrapper around the OpenTelemetry propagate API to allow the OpenTelemetry contexts (and therefore Logfire contexts) to be transferred between different code running in different threads, processes or even services.
In general, you should not need to use this module since Logfire will automatically
patch ThreadPoolExecutor
and
ProcessPoolExecutor
to carry over the context.
And existing plugins exist to propagate the context with
requests and
httpx.
get_context ¶
get_context() -> ContextCarrier
Create a new empty carrier dict and inject context into it.
Returns:
Type | Description |
---|---|
ContextCarrier
|
A new dict with the context injected into it. |
Usage:
from logfire.propagate import get_context, attach_context
logfire_context = get_context()
...
# later on in another thread, process or service
with attach_context(logfire_context):
...
You could also inject context into an existing mapping like headers with:
from logfire.propagate import get_context
existing_headers = {'X-Foobar': 'baz'}
existing_headers.update(get_context())
...
Source code in logfire/propagate.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
attach_context ¶
Attach a context as generated by get_context
to the current execution context.
Since attach_context
is a context manager, it restores the previous context when exiting.
Set third_party
to True
if using this inside a library intended to be used by others.
This will respect the distributed_tracing
argument of logfire.configure()
,
so users will be warned about unintentional distributed tracing by default and they can suppress it.
See Unintentional Distributed Tracing for more information.
Source code in logfire/propagate.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|