ant_ai.observer.obs
obs
module-attribute
The global observability singleton.
Import and use this directly to emit events, record exceptions, and open spans.
ObservabilitySingleton
Global observability singleton.
Thread-safe and async-safe: contextvar state is task-local and restored
on exit from bind(), so concurrent invocations never bleed into each other.
Source code in src/ant_ai/observer/obs.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
configure
configure(sink: Any) -> None
Set the active sink. Pass None to disable observability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sink
|
Any
|
An |
required |
Source code in src/ant_ai/observer/obs.py
24 25 26 27 28 29 30 | |
bind
bind(**fields: Any)
Merge fields into the current task's observability context.
The context is restored on exit, so calls nest safely and concurrent
invocations stay independent. All event and span calls made inside
the block automatically include these fields.
Leaving the block from a task other than the one that entered it (an async generator closed elsewhere) restores the leaving task's context; the entering task's context is out of reach and is left as it was.
Restoring by value rather than via ContextVar.reset(token) is what
makes that safe: a token is only valid in the Context that created it,
so resetting it from another task's (copied) Context raises ValueError.
Setting the saved value has no such restriction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Any
|
Key-value pairs to add to the current context. |
{}
|
Source code in src/ant_ai/observer/obs.py
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 | |
event
async
event(name: str, **fields: Any) -> None
Emit a named lifecycle event with structured metadata.
Context fields bound via bind() are merged in automatically.
Never raises — sink errors are swallowed to protect the runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dot-namespaced event name (e.g. |
required |
**fields
|
Any
|
Additional metadata to attach to the event. |
{}
|
Source code in src/ant_ai/observer/obs.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
exception
async
exception(
name: str, error: Exception, **fields: Any
) -> None
Emit a named error event carrying the exception instance.
Context fields bound via bind() are merged in automatically.
Never raises — sink errors are swallowed to protect the runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dot-namespaced event name (e.g. |
required |
error
|
Exception
|
The exception to record. |
required |
**fields
|
Any
|
Additional metadata to attach to the event. |
{}
|
Source code in src/ant_ai/observer/obs.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
span
span(
name: str, **attrs: Any
) -> AbstractAsyncContextManager[Any]
Return an async context manager representing a unit of work.
Returns a no-op context manager when no sink is configured.
Context fields bound via bind() are merged into the span attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Operation name for the span (e.g. |
required |
**attrs
|
Any
|
Additional attributes to attach to the span. |
{}
|
Returns:
| Type | Description |
|---|---|
AbstractAsyncContextManager[Any]
|
An async context manager that opens and closes the span. |
Source code in src/ant_ai/observer/obs.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
propagation_headers
propagation_headers() -> dict[str, str]
Return headers encoding the current trace context for outbound requests.
Source code in src/ant_ai/observer/obs.py
107 108 109 110 111 112 113 | |
attach_propagation_context
attach_propagation_context(
headers: dict[str, str],
) -> Iterator[None]
Context manager that restores a remote trace context for the duration of the block.
Source code in src/ant_ai/observer/obs.py
115 116 117 118 119 120 121 122 123 124 125 126 127 | |