agentrail-python 0.2.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/PKG-INFO +72 -5
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/README.md +66 -1
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/integration/README.md +6 -5
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/pyproject.toml +15 -5
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/__init__.py +2 -5
- agentrail_python-0.4.0/src/agentrail_python/_cli.py +173 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_client.py +32 -22
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_handoff.py +10 -2
- agentrail_python-0.4.0/src/agentrail_python/_init.py +38 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_options.py +1 -5
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_outbound.py +39 -17
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_privacy.py +5 -0
- agentrail_python-0.4.0/src/agentrail_python/_processors.py +218 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_runtime.py +46 -6
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_version.py +1 -1
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/asgi.py +6 -3
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/httpx.py +19 -15
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/mcp.py +32 -15
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/requests.py +14 -9
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/wsgi.py +5 -4
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/README.md +9 -4
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/sdk-contract.json +707 -1
- agentrail_python-0.4.0/tests/coexistence.py +523 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/support.py +28 -12
- agentrail_python-0.4.0/tests/test_cli.py +130 -0
- agentrail_python-0.4.0/tests/test_coexistence.py +197 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_delivery.py +37 -13
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_evidence.py +12 -13
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_incoming.py +28 -4
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_mcp.py +22 -1
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_outbound.py +65 -11
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_privacy.py +14 -24
- agentrail_python-0.4.0/tests/test_setup.py +80 -0
- agentrail_python-0.2.0/src/agentrail_python/_init.py +0 -64
- agentrail_python-0.2.0/src/agentrail_python/_processors.py +0 -262
- agentrail_python-0.2.0/tests/test_composition.py +0 -174
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/.gitignore +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/LICENSE +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_performance.py +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_queue.py +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_suppression.py +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/_text.py +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/logging.py +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/src/agentrail_python/py.typed +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/interoperability/contract.json +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/interoperability/generate-performance.mjs +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/interoperability/javascript.json +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/interoperability/markers.json +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/testdata/interoperability/performance.json +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_interoperability.py +0 -0
- {agentrail_python-0.2.0 → agentrail_python-0.4.0}/tests/test_mcp_request_facts.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: agentrail-python
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Agentrail for Python backends: HTTP, identity, milestones, logs and MCP for agent journeys, over OpenTelemetry.
|
|
5
5
|
Project-URL: Homepage, https://www.agentrail.co
|
|
6
6
|
Project-URL: Documentation, https://www.agentrail.co/docs/guides/journeys-python
|
|
@@ -22,9 +22,11 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
22
22
|
Classifier: Topic :: System :: Monitoring
|
|
23
23
|
Classifier: Typing :: Typed
|
|
24
24
|
Requires-Python: >=3.10
|
|
25
|
-
Requires-Dist:
|
|
26
|
-
Requires-Dist: opentelemetry-
|
|
27
|
-
Requires-Dist: opentelemetry-
|
|
25
|
+
Requires-Dist: http-sf<2,>=1.5.0
|
|
26
|
+
Requires-Dist: opentelemetry-api<2,>=1.39
|
|
27
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-common<2,>=1.39
|
|
28
|
+
Requires-Dist: opentelemetry-sdk<2,>=1.39
|
|
29
|
+
Requires-Dist: requests<3,>=2.26
|
|
28
30
|
Provides-Extra: httpx
|
|
29
31
|
Requires-Dist: httpx<1,>=0.28; extra == 'httpx'
|
|
30
32
|
Provides-Extra: mcp
|
|
@@ -119,6 +121,42 @@ application = agentrail.wsgi(get_wsgi_application()) # Django (WSGI)
|
|
|
119
121
|
app.wsgi_app = agentrail.wsgi(app.wsgi_app) # Flask, any WSGI app
|
|
120
122
|
```
|
|
121
123
|
|
|
124
|
+
### Beside Sentry, Datadog or OpenTelemetry
|
|
125
|
+
|
|
126
|
+
The setup above is the same when your application already runs Sentry,
|
|
127
|
+
Datadog or OpenTelemetry, and there is nothing to change in theirs. They can
|
|
128
|
+
start before or after `init`, and their middleware can sit outside or inside
|
|
129
|
+
Agentrail's. Agentrail runs a tracer provider of its own and registers no
|
|
130
|
+
OpenTelemetry global: no tracer provider, logger provider, propagator or
|
|
131
|
+
context. Their spans never reach Agentrail, and Agentrail's never reach them.
|
|
132
|
+
|
|
133
|
+
Inside a request, your tracer's span stays current: your database and client
|
|
134
|
+
spans stay children of its server span, and its trace headers (`traceparent`,
|
|
135
|
+
`tracestate`, `baggage`, `sentry-trace`, `x-datadog-*`) go out on your calls as
|
|
136
|
+
it set them. Agentrail removes only its own headers,
|
|
137
|
+
`x-agentrail-trace-handoff` and `x-agentrail-cli`.
|
|
138
|
+
|
|
139
|
+
One limit: on a call to an origin in `outbound_origins`, Agentrail's signed
|
|
140
|
+
`traceparent` replaces your tracer's, and its `tracestate` is dropped with it.
|
|
141
|
+
A tracer that propagates by W3C `traceparent` alone, as OpenTelemetry does by
|
|
142
|
+
default, continues that hop under Agentrail's trace ID. Sentry and Datadog keep
|
|
143
|
+
their own trace there, since `sentry-trace`, `baggage` and `x-datadog-*` still
|
|
144
|
+
go out. OpenTelemetry's httpx instrumentation, installed with
|
|
145
|
+
`HTTPXClientInstrumentor().instrument()`, writes its `traceparent` beneath
|
|
146
|
+
Agentrail's transport, after Agentrail has signed: such a call reaches the
|
|
147
|
+
allowed origin with a `traceparent` the handoff does not name, and that service
|
|
148
|
+
records the caller as a link instead of continuing its trace.
|
|
149
|
+
|
|
150
|
+
Agentrail's capture and delivery take none of OpenTelemetry's `OTEL_*`
|
|
151
|
+
environment variables, which stay your exporters' and SDK's: your
|
|
152
|
+
`OTEL_EXPORTER_OTLP_*` headers, compression and certificates never reach
|
|
153
|
+
Agentrail's collector, and your attribute limits never cut its spans. The one
|
|
154
|
+
exception is `OTEL_SDK_DISABLED=true`, which also switches Agentrail off: it
|
|
155
|
+
records nothing and reports `entry_not_recording` to `on_error`.
|
|
156
|
+
|
|
157
|
+
With `DD_TRACE_OTEL_ENABLED` and `import ddtrace.auto`, import ddtrace before
|
|
158
|
+
`agentrail_python`, as Datadog asks anyway.
|
|
159
|
+
|
|
122
160
|
### MCP
|
|
123
161
|
|
|
124
162
|
With the official `mcp` SDK, add the middleware first. Each JSON-RPC message
|
|
@@ -175,15 +213,44 @@ cut side is flagged. A multi-round-trip `requestState` is never sent. A request
|
|
|
175
213
|
middleware runs (a malformed body, a wrong header, an unknown session) is no
|
|
176
214
|
MCP step; behind `agentrail.asgi` it is still recorded as its HTTP request.
|
|
177
215
|
|
|
216
|
+
When a customer's CLI uses [`agentrail-cli-sdk`](https://www.npmjs.com/package/agentrail-cli-sdk),
|
|
217
|
+
its requests say which command an agent ran, in which run and session. The
|
|
218
|
+
ASGI and WSGI middleware record that on the request's span, the session only as
|
|
219
|
+
a keyed hash, so the agent's CLI runs join its journey. There is nothing to
|
|
220
|
+
configure.
|
|
221
|
+
|
|
178
222
|
Agentrail never fails your request: anything it could not capture or send goes
|
|
179
223
|
to your `on_error` callback.
|
|
180
224
|
|
|
225
|
+
## Upgrading from 0.3
|
|
226
|
+
|
|
227
|
+
0.4 has one setup for everyone, and removes the one for an existing
|
|
228
|
+
OpenTelemetry provider:
|
|
229
|
+
|
|
230
|
+
- `create_processors` and `Processors` (its `bind`, `sampler`,
|
|
231
|
+
`span_processor`, `log_processor` and `propagator`) are gone: call `init`
|
|
232
|
+
instead, and leave your provider as it was.
|
|
233
|
+
- `Options.propagator` is gone: your propagator stays yours.
|
|
234
|
+
- `init` no longer refuses an existing tracer or logger provider, and sets
|
|
235
|
+
none.
|
|
236
|
+
- Your own spans inside a request, such as those of
|
|
237
|
+
`opentelemetry-instrumentation-*` packages, and logs written through an
|
|
238
|
+
OpenTelemetry logger provider no longer reach Agentrail. Send logs with
|
|
239
|
+
`agentrail.log` or `agentrail_python.logging.AgentrailHandler`.
|
|
240
|
+
- Calls made while answering a request keep your tracer's headers; only an
|
|
241
|
+
allowed origin's `traceparent` is replaced.
|
|
242
|
+
- Work in a thread you start yourself is part of the request only when the
|
|
243
|
+
thread runs in a copy of the request's context:
|
|
244
|
+
`contextvars.copy_context().run(...)` or `asyncio.to_thread` (Starlette's and
|
|
245
|
+
anyio's thread pools already do this). OpenTelemetry's threading
|
|
246
|
+
instrumentation no longer carries Agentrail's request.
|
|
247
|
+
|
|
181
248
|
## Documentation
|
|
182
249
|
|
|
183
250
|
| | |
|
|
184
251
|
| --- | --- |
|
|
185
252
|
| [Install the Python SDK](https://www.agentrail.co/docs/guides/journeys-python) | Setup for each framework, outgoing calls, identity, milestones, logs, MCP and location |
|
|
186
|
-
| [Python SDK reference](https://www.agentrail.co/docs/guides/journeys-python-reference) | Every option,
|
|
253
|
+
| [Python SDK reference](https://www.agentrail.co/docs/guides/journeys-python-reference) | Every option, running beside Sentry, Datadog or OpenTelemetry, delivery and error codes, framework notes |
|
|
187
254
|
| [Leave out your own servers](https://www.agentrail.co/docs/guides/journeys-own-servers) | Skip calls between your own services |
|
|
188
255
|
| [Plugin workflow tracking](https://www.agentrail.co/docs/guides/plugin-performance) | Report workflow attempts and plugin calls on milestones |
|
|
189
256
|
| [What Agentrail records](https://www.agentrail.co/docs/guides/journeys-data) | What is sent, what is hashed, and what never leaves your process |
|
|
@@ -84,6 +84,42 @@ application = agentrail.wsgi(get_wsgi_application()) # Django (WSGI)
|
|
|
84
84
|
app.wsgi_app = agentrail.wsgi(app.wsgi_app) # Flask, any WSGI app
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
+
### Beside Sentry, Datadog or OpenTelemetry
|
|
88
|
+
|
|
89
|
+
The setup above is the same when your application already runs Sentry,
|
|
90
|
+
Datadog or OpenTelemetry, and there is nothing to change in theirs. They can
|
|
91
|
+
start before or after `init`, and their middleware can sit outside or inside
|
|
92
|
+
Agentrail's. Agentrail runs a tracer provider of its own and registers no
|
|
93
|
+
OpenTelemetry global: no tracer provider, logger provider, propagator or
|
|
94
|
+
context. Their spans never reach Agentrail, and Agentrail's never reach them.
|
|
95
|
+
|
|
96
|
+
Inside a request, your tracer's span stays current: your database and client
|
|
97
|
+
spans stay children of its server span, and its trace headers (`traceparent`,
|
|
98
|
+
`tracestate`, `baggage`, `sentry-trace`, `x-datadog-*`) go out on your calls as
|
|
99
|
+
it set them. Agentrail removes only its own headers,
|
|
100
|
+
`x-agentrail-trace-handoff` and `x-agentrail-cli`.
|
|
101
|
+
|
|
102
|
+
One limit: on a call to an origin in `outbound_origins`, Agentrail's signed
|
|
103
|
+
`traceparent` replaces your tracer's, and its `tracestate` is dropped with it.
|
|
104
|
+
A tracer that propagates by W3C `traceparent` alone, as OpenTelemetry does by
|
|
105
|
+
default, continues that hop under Agentrail's trace ID. Sentry and Datadog keep
|
|
106
|
+
their own trace there, since `sentry-trace`, `baggage` and `x-datadog-*` still
|
|
107
|
+
go out. OpenTelemetry's httpx instrumentation, installed with
|
|
108
|
+
`HTTPXClientInstrumentor().instrument()`, writes its `traceparent` beneath
|
|
109
|
+
Agentrail's transport, after Agentrail has signed: such a call reaches the
|
|
110
|
+
allowed origin with a `traceparent` the handoff does not name, and that service
|
|
111
|
+
records the caller as a link instead of continuing its trace.
|
|
112
|
+
|
|
113
|
+
Agentrail's capture and delivery take none of OpenTelemetry's `OTEL_*`
|
|
114
|
+
environment variables, which stay your exporters' and SDK's: your
|
|
115
|
+
`OTEL_EXPORTER_OTLP_*` headers, compression and certificates never reach
|
|
116
|
+
Agentrail's collector, and your attribute limits never cut its spans. The one
|
|
117
|
+
exception is `OTEL_SDK_DISABLED=true`, which also switches Agentrail off: it
|
|
118
|
+
records nothing and reports `entry_not_recording` to `on_error`.
|
|
119
|
+
|
|
120
|
+
With `DD_TRACE_OTEL_ENABLED` and `import ddtrace.auto`, import ddtrace before
|
|
121
|
+
`agentrail_python`, as Datadog asks anyway.
|
|
122
|
+
|
|
87
123
|
### MCP
|
|
88
124
|
|
|
89
125
|
With the official `mcp` SDK, add the middleware first. Each JSON-RPC message
|
|
@@ -140,15 +176,44 @@ cut side is flagged. A multi-round-trip `requestState` is never sent. A request
|
|
|
140
176
|
middleware runs (a malformed body, a wrong header, an unknown session) is no
|
|
141
177
|
MCP step; behind `agentrail.asgi` it is still recorded as its HTTP request.
|
|
142
178
|
|
|
179
|
+
When a customer's CLI uses [`agentrail-cli-sdk`](https://www.npmjs.com/package/agentrail-cli-sdk),
|
|
180
|
+
its requests say which command an agent ran, in which run and session. The
|
|
181
|
+
ASGI and WSGI middleware record that on the request's span, the session only as
|
|
182
|
+
a keyed hash, so the agent's CLI runs join its journey. There is nothing to
|
|
183
|
+
configure.
|
|
184
|
+
|
|
143
185
|
Agentrail never fails your request: anything it could not capture or send goes
|
|
144
186
|
to your `on_error` callback.
|
|
145
187
|
|
|
188
|
+
## Upgrading from 0.3
|
|
189
|
+
|
|
190
|
+
0.4 has one setup for everyone, and removes the one for an existing
|
|
191
|
+
OpenTelemetry provider:
|
|
192
|
+
|
|
193
|
+
- `create_processors` and `Processors` (its `bind`, `sampler`,
|
|
194
|
+
`span_processor`, `log_processor` and `propagator`) are gone: call `init`
|
|
195
|
+
instead, and leave your provider as it was.
|
|
196
|
+
- `Options.propagator` is gone: your propagator stays yours.
|
|
197
|
+
- `init` no longer refuses an existing tracer or logger provider, and sets
|
|
198
|
+
none.
|
|
199
|
+
- Your own spans inside a request, such as those of
|
|
200
|
+
`opentelemetry-instrumentation-*` packages, and logs written through an
|
|
201
|
+
OpenTelemetry logger provider no longer reach Agentrail. Send logs with
|
|
202
|
+
`agentrail.log` or `agentrail_python.logging.AgentrailHandler`.
|
|
203
|
+
- Calls made while answering a request keep your tracer's headers; only an
|
|
204
|
+
allowed origin's `traceparent` is replaced.
|
|
205
|
+
- Work in a thread you start yourself is part of the request only when the
|
|
206
|
+
thread runs in a copy of the request's context:
|
|
207
|
+
`contextvars.copy_context().run(...)` or `asyncio.to_thread` (Starlette's and
|
|
208
|
+
anyio's thread pools already do this). OpenTelemetry's threading
|
|
209
|
+
instrumentation no longer carries Agentrail's request.
|
|
210
|
+
|
|
146
211
|
## Documentation
|
|
147
212
|
|
|
148
213
|
| | |
|
|
149
214
|
| --- | --- |
|
|
150
215
|
| [Install the Python SDK](https://www.agentrail.co/docs/guides/journeys-python) | Setup for each framework, outgoing calls, identity, milestones, logs, MCP and location |
|
|
151
|
-
| [Python SDK reference](https://www.agentrail.co/docs/guides/journeys-python-reference) | Every option,
|
|
216
|
+
| [Python SDK reference](https://www.agentrail.co/docs/guides/journeys-python-reference) | Every option, running beside Sentry, Datadog or OpenTelemetry, delivery and error codes, framework notes |
|
|
152
217
|
| [Leave out your own servers](https://www.agentrail.co/docs/guides/journeys-own-servers) | Skip calls between your own services |
|
|
153
218
|
| [Plugin workflow tracking](https://www.agentrail.co/docs/guides/plugin-performance) | Report workflow attempts and plugin calls on milestones |
|
|
154
219
|
| [What Agentrail records](https://www.agentrail.co/docs/guides/journeys-data) | What is sent, what is hashed, and what never leaves your process |
|
|
@@ -9,10 +9,11 @@ ARGUS_DIR=/absolute/path/to/argus ./integration/run.sh
|
|
|
9
9
|
|
|
10
10
|
`capture.py` uses the public package: a Starlette app behind the ASGI
|
|
11
11
|
middleware identifies a synthetic user, opens a custom milestone, writes two
|
|
12
|
-
correlated logs and makes an outgoing `httpx` call
|
|
13
|
-
|
|
14
|
-
exact OTLP protobuf bytes. There are no provider calls, real
|
|
15
|
-
production credentials; the collector token and hash key are
|
|
12
|
+
correlated logs and makes an outgoing `httpx` call to an allowed origin, the
|
|
13
|
+
milestone's supporting span, with a synthetic location hook. A local collector
|
|
14
|
+
keeps the exact OTLP protobuf bytes. There are no provider calls, real
|
|
15
|
+
identities or production credentials; the collector token and hash key are
|
|
16
|
+
synthetic.
|
|
16
17
|
|
|
17
18
|
The runner copies `argus-receiver.integration.test.ts` into a uniquely named
|
|
18
19
|
temporary Argus API test, then runs `pnpm --filter @argus/api test:file`.
|
|
@@ -21,7 +22,7 @@ database. The test creates a backend (`node`) journey source through the public
|
|
|
21
22
|
API, sends the captured bytes logs first and trace batches reversed, then
|
|
22
23
|
repeats every delivery unchanged. Fresh reads check that:
|
|
23
24
|
|
|
24
|
-
-
|
|
25
|
+
- three spans and two logs are stored once, through the retries;
|
|
25
26
|
- the resource keeps `agentrail-python`, its service identity and hash key ID;
|
|
26
27
|
- the HTTP entry and the milestone are both entries, with supporting spans
|
|
27
28
|
beneath the milestone;
|
|
@@ -25,11 +25,17 @@ classifiers = [
|
|
|
25
25
|
"Topic :: System :: Monitoring",
|
|
26
26
|
"Typing :: Typed",
|
|
27
27
|
]
|
|
28
|
-
# The
|
|
28
|
+
# The OpenTelemetry family from the oldest release the suite passes on (CI's
|
|
29
|
+
# lowest-opentelemetry job) to the next major. Its API, SDK and OTLP encoder
|
|
30
|
+
# versions move together.
|
|
29
31
|
dependencies = [
|
|
30
|
-
"opentelemetry-api>=1.
|
|
31
|
-
"opentelemetry-sdk>=1.
|
|
32
|
-
"opentelemetry-exporter-otlp-proto-
|
|
32
|
+
"opentelemetry-api>=1.39,<2",
|
|
33
|
+
"opentelemetry-sdk>=1.39,<2",
|
|
34
|
+
"opentelemetry-exporter-otlp-proto-common>=1.39,<2",
|
|
35
|
+
# Delivery to the collector; 2.26 is the oldest the suite passes on.
|
|
36
|
+
"requests>=2.26,<3",
|
|
37
|
+
# RFC 9651 Structured Fields, for the x-agentrail-cli header a customer's CLI sends.
|
|
38
|
+
"http-sf>=1.5.0,<2",
|
|
33
39
|
]
|
|
34
40
|
|
|
35
41
|
[project.optional-dependencies]
|
|
@@ -57,6 +63,10 @@ dev = [
|
|
|
57
63
|
"uvicorn>=0.38",
|
|
58
64
|
"a2wsgi>=1.10",
|
|
59
65
|
"trustme>=1.2",
|
|
66
|
+
# The tracers a customer already runs, for the tests that run beside them.
|
|
67
|
+
"sentry-sdk>=2.40",
|
|
68
|
+
"ddtrace>=4.16",
|
|
69
|
+
"opentelemetry-instrumentation-asgi>=0.60b0",
|
|
60
70
|
]
|
|
61
71
|
|
|
62
72
|
[tool.hatch.version]
|
|
@@ -70,7 +80,7 @@ include = ["src", "tests", "testdata", "README.md", "LICENSE", "pyproject.toml"]
|
|
|
70
80
|
|
|
71
81
|
[tool.uv]
|
|
72
82
|
# Resolve only releases at least two weeks old when the lock was written.
|
|
73
|
-
exclude-newer = "2026-
|
|
83
|
+
exclude-newer = "2026-11-11T00:00:00Z"
|
|
74
84
|
|
|
75
85
|
[tool.ruff]
|
|
76
86
|
line-length = 120
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Copyright 2026 Agentrail. Licensed under the Apache License, Version 2.0.
|
|
2
2
|
"""Agentrail for Python: backend journey evidence over OpenTelemetry.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
setup:
|
|
4
|
+
``agentrail = init(Options(...))``, the same with or without Sentry, Datadog or
|
|
5
|
+
an OpenTelemetry setup of your own: Agentrail registers no OpenTelemetry global.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
from ._client import Client, Diagnostics, MilestoneScope
|
|
@@ -30,7 +30,6 @@ from ._options import (
|
|
|
30
30
|
VerifiedIdentity,
|
|
31
31
|
)
|
|
32
32
|
from ._privacy import StructuredLog
|
|
33
|
-
from ._processors import Processors, create_processors
|
|
34
33
|
from ._queue import QueueStats
|
|
35
34
|
from ._version import VERSION
|
|
36
35
|
|
|
@@ -57,12 +56,10 @@ __all__ = [
|
|
|
57
56
|
"PerformanceContext",
|
|
58
57
|
"PluginInvocation",
|
|
59
58
|
"PrincipalKind",
|
|
60
|
-
"Processors",
|
|
61
59
|
"QueueOptions",
|
|
62
60
|
"QueueStats",
|
|
63
61
|
"StructuredLog",
|
|
64
62
|
"Surface",
|
|
65
63
|
"VerifiedIdentity",
|
|
66
|
-
"create_processors",
|
|
67
64
|
"init",
|
|
68
65
|
]
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# Copyright 2026 Agentrail. Licensed under the Apache License, Version 2.0.
|
|
2
|
+
"""The header a customer's CLI sends its own API, as a server reads it.
|
|
3
|
+
|
|
4
|
+
``agentrail-cli-sdk`` writes ``x-agentrail-cli``, an RFC 9651 Dictionary naming
|
|
5
|
+
the command an agent ran, the run, the session and the agent. This reads it as
|
|
6
|
+
agentrail-core's ``readCliHeader`` and agentrail-go do (the contract's
|
|
7
|
+
``cliHeader``), and turns it into the span attributes the collector reads
|
|
8
|
+
(``otlp.cliAttributes``), the session only as its keyed hash.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import re
|
|
14
|
+
from collections.abc import Callable, Mapping
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
|
|
17
|
+
import http_sf
|
|
18
|
+
|
|
19
|
+
from ._runtime import Attributes
|
|
20
|
+
|
|
21
|
+
CLI_HEADER = "x-agentrail-cli" # also in ``_handoff.AGENTRAIL_HEADERS``
|
|
22
|
+
|
|
23
|
+
_TEXT = re.compile(r"[\x20-\x7e]{1,64}")
|
|
24
|
+
_COMMAND = re.compile(r"[\x20-\x7e]{1,128}")
|
|
25
|
+
_RUN = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
|
|
26
|
+
_SESSION = re.compile(r"[0-9a-f]{32}")
|
|
27
|
+
_AGENT = re.compile(r"[a-z0-9._-]{1,64}")
|
|
28
|
+
_VARIABLE = re.compile(r"[A-Za-z_][A-Za-z0-9_]{0,63}")
|
|
29
|
+
_SESSION_SOURCES = ("agent", "ci", "local", "run")
|
|
30
|
+
_MODES = ("agent", "ci", "interactive", "non-interactive")
|
|
31
|
+
# The largest Integer RFC 9651 holds, and the most evidence a header names.
|
|
32
|
+
_MAX_INTEGER = 999_999_999_999_999
|
|
33
|
+
_MAX_EVIDENCE = 8
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class CliHeader:
|
|
38
|
+
sdk_version: str
|
|
39
|
+
cli: str
|
|
40
|
+
run: str
|
|
41
|
+
sequence: int
|
|
42
|
+
session: str
|
|
43
|
+
session_from: str
|
|
44
|
+
mode: str
|
|
45
|
+
cli_version: str | None = None
|
|
46
|
+
agent: str | None = None
|
|
47
|
+
agent_version: str | None = None
|
|
48
|
+
evidence: tuple[str, ...] | None = None
|
|
49
|
+
command: str | None = None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class _Unread(Exception):
|
|
53
|
+
"""A header no CLI SDK writes: it reads as nothing."""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _item(members: Mapping[str, object], key: str) -> object:
|
|
57
|
+
member = members.get(key)
|
|
58
|
+
if not isinstance(member, tuple) or len(member) != 2: # pyright: ignore[reportUnknownArgumentType]
|
|
59
|
+
raise _Unread(key)
|
|
60
|
+
return member[0] # pyright: ignore[reportUnknownVariableType]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _string(members: Mapping[str, object], key: str, pattern: re.Pattern[str]) -> str:
|
|
64
|
+
value = _item(members, key)
|
|
65
|
+
# A Token or a Display String is no String, though each holds text.
|
|
66
|
+
if type(value) is not str or pattern.fullmatch(value) is None:
|
|
67
|
+
raise _Unread(key)
|
|
68
|
+
return value
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _optional(members: Mapping[str, object], key: str, pattern: re.Pattern[str]) -> str | None:
|
|
72
|
+
return _string(members, key, pattern) if key in members else None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _token(members: Mapping[str, object], key: str, allowed: tuple[str, ...]) -> str:
|
|
76
|
+
value = _item(members, key)
|
|
77
|
+
if not isinstance(value, http_sf.Token) or str(value) not in allowed:
|
|
78
|
+
raise _Unread(key)
|
|
79
|
+
return str(value)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _integer(members: Mapping[str, object], key: str, low: int, high: int) -> int:
|
|
83
|
+
value = _item(members, key)
|
|
84
|
+
# bool is an int in Python; a Boolean is no Integer.
|
|
85
|
+
if type(value) is not int or not low <= value <= high:
|
|
86
|
+
raise _Unread(key)
|
|
87
|
+
return value
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _evidence(members: Mapping[str, object]) -> tuple[str, ...] | None:
|
|
91
|
+
if "ev" not in members:
|
|
92
|
+
return None
|
|
93
|
+
items = _item(members, "ev")
|
|
94
|
+
if not isinstance(items, list):
|
|
95
|
+
raise _Unread("ev")
|
|
96
|
+
entries: list[object] = items # pyright: ignore[reportUnknownVariableType]
|
|
97
|
+
if not 1 <= len(entries) <= _MAX_EVIDENCE:
|
|
98
|
+
raise _Unread("ev")
|
|
99
|
+
names: list[str] = []
|
|
100
|
+
for entry in entries:
|
|
101
|
+
if not isinstance(entry, tuple):
|
|
102
|
+
raise _Unread("ev")
|
|
103
|
+
parts: tuple[object, ...] = entry # pyright: ignore[reportUnknownVariableType]
|
|
104
|
+
name = parts[0]
|
|
105
|
+
if type(name) is not str or _VARIABLE.fullmatch(name) is None:
|
|
106
|
+
raise _Unread("ev")
|
|
107
|
+
names.append(name)
|
|
108
|
+
return tuple(names)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def read_cli_header(value: str | None) -> CliHeader | None:
|
|
112
|
+
"""What the header says, or None for none, or one that is not a CLI header of
|
|
113
|
+
any version: not a Dictionary, a member missing, one outside the contract's
|
|
114
|
+
rules, or one given twice, as two header lines a server joined are. Any ``v``
|
|
115
|
+
from 1 is read, and members this version does not know are ignored."""
|
|
116
|
+
if value is None:
|
|
117
|
+
return None
|
|
118
|
+
repeated: list[str] = []
|
|
119
|
+
try:
|
|
120
|
+
members = http_sf.parse(
|
|
121
|
+
value.encode("latin-1"),
|
|
122
|
+
tltype="dictionary",
|
|
123
|
+
on_duplicate_key=lambda key, _context: repeated.append(key),
|
|
124
|
+
)
|
|
125
|
+
except (http_sf.StructuredFieldError, UnicodeEncodeError):
|
|
126
|
+
return None
|
|
127
|
+
if repeated or not isinstance(members, dict):
|
|
128
|
+
return None
|
|
129
|
+
fields: Mapping[str, object] = members
|
|
130
|
+
try:
|
|
131
|
+
_integer(fields, "v", 1, _MAX_INTEGER)
|
|
132
|
+
return CliHeader(
|
|
133
|
+
sdk_version=_string(fields, "sdk", _TEXT),
|
|
134
|
+
cli=_string(fields, "cli", _TEXT),
|
|
135
|
+
run=_string(fields, "run", _RUN),
|
|
136
|
+
sequence=_integer(fields, "seq", 1, _MAX_INTEGER),
|
|
137
|
+
session=_string(fields, "ses", _SESSION),
|
|
138
|
+
session_from=_token(fields, "sesfrom", _SESSION_SOURCES),
|
|
139
|
+
mode=_token(fields, "mode", _MODES),
|
|
140
|
+
cli_version=_optional(fields, "cliv", _TEXT),
|
|
141
|
+
agent=_optional(fields, "agent", _AGENT),
|
|
142
|
+
agent_version=_optional(fields, "agentv", _TEXT),
|
|
143
|
+
evidence=_evidence(fields),
|
|
144
|
+
command=_optional(fields, "cmd", _COMMAND),
|
|
145
|
+
)
|
|
146
|
+
except _Unread:
|
|
147
|
+
return None
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def cli_attributes(value: str | None, keyed_hash: Callable[[str], str]) -> Attributes:
|
|
151
|
+
"""The request's CLI facts as span attributes; empty for a header that does not read."""
|
|
152
|
+
header = read_cli_header(value)
|
|
153
|
+
if header is None:
|
|
154
|
+
return {}
|
|
155
|
+
attributes: Attributes = {
|
|
156
|
+
"agentrail.cli.name": header.cli,
|
|
157
|
+
"agentrail.cli.run_id": header.run,
|
|
158
|
+
"agentrail.cli.sequence": header.sequence,
|
|
159
|
+
"agentrail.cli.mode": header.mode,
|
|
160
|
+
"agentrail.cli.session.id_hash": keyed_hash(header.session),
|
|
161
|
+
"agentrail.cli.session.source": header.session_from,
|
|
162
|
+
"agentrail.cli.sdk.version": header.sdk_version,
|
|
163
|
+
}
|
|
164
|
+
for key, optional in (
|
|
165
|
+
("agentrail.cli.version", header.cli_version),
|
|
166
|
+
("agentrail.cli.command", header.command),
|
|
167
|
+
("agentrail.cli.agent.name", header.agent),
|
|
168
|
+
("agentrail.cli.agent.version", header.agent_version),
|
|
169
|
+
("agentrail.cli.agent.evidence", None if header.evidence is None else ",".join(header.evidence)),
|
|
170
|
+
):
|
|
171
|
+
if optional is not None:
|
|
172
|
+
attributes[key] = optional
|
|
173
|
+
return attributes
|
|
@@ -9,6 +9,7 @@ import re
|
|
|
9
9
|
import threading
|
|
10
10
|
import time
|
|
11
11
|
from collections.abc import Callable
|
|
12
|
+
from contextvars import Token
|
|
12
13
|
from dataclasses import dataclass
|
|
13
14
|
from types import TracebackType
|
|
14
15
|
from typing import TYPE_CHECKING, Any, TypeVar, cast
|
|
@@ -17,6 +18,7 @@ from opentelemetry import context as otel_context
|
|
|
17
18
|
from opentelemetry import trace
|
|
18
19
|
from opentelemetry.trace import Link, NonRecordingSpan, Span, SpanContext, SpanKind, StatusCode, TraceFlags
|
|
19
20
|
|
|
21
|
+
from ._cli import CLI_HEADER, cli_attributes
|
|
20
22
|
from ._handoff import HANDOFF_HEADER, Refused, keyed_hash, parse_traceparent, verify
|
|
21
23
|
from ._options import (
|
|
22
24
|
LOCATION_SOURCES,
|
|
@@ -32,7 +34,7 @@ from ._options import (
|
|
|
32
34
|
from ._performance import performance_attributes
|
|
33
35
|
from ._privacy import StructuredLog, clean_context, copy_log
|
|
34
36
|
from ._queue import ExportQueue, QueueStats
|
|
35
|
-
from ._runtime import Attributes, Mark, McpEvidence, RequestState, Runtime
|
|
37
|
+
from ._runtime import Attributes, Mark, McpEvidence, RequestState, Runtime, attach, current, current_span, detach
|
|
36
38
|
from ._suppression import suppressed
|
|
37
39
|
from ._text import bounded, valid_text
|
|
38
40
|
from ._version import VERSION
|
|
@@ -61,7 +63,8 @@ def _error_type(error: BaseException) -> str:
|
|
|
61
63
|
class MilestoneScope:
|
|
62
64
|
"""A milestone around a block: ``with`` or ``async with``.
|
|
63
65
|
|
|
64
|
-
The block's exception is recorded and re-raised unchanged.
|
|
66
|
+
The block's exception is recorded and re-raised unchanged. The
|
|
67
|
+
application's own current span stays current inside it.
|
|
65
68
|
"""
|
|
66
69
|
|
|
67
70
|
def __init__(
|
|
@@ -72,6 +75,7 @@ class MilestoneScope:
|
|
|
72
75
|
*,
|
|
73
76
|
step: bool = True,
|
|
74
77
|
mcp: McpEvidence | None = None,
|
|
78
|
+
link: SpanContext | None = None,
|
|
75
79
|
) -> None:
|
|
76
80
|
client._validate_milestone(name, details) # pyright: ignore[reportPrivateUsage]
|
|
77
81
|
self._client = client
|
|
@@ -79,15 +83,16 @@ class MilestoneScope:
|
|
|
79
83
|
self._details = details
|
|
80
84
|
self._step = step
|
|
81
85
|
self._mcp = mcp
|
|
86
|
+
self._link = link
|
|
82
87
|
self._span: Span | None = None
|
|
83
|
-
self._token:
|
|
88
|
+
self._token: Token[otel_context.Context] | None = None
|
|
84
89
|
|
|
85
90
|
def __enter__(self) -> MilestoneScope:
|
|
86
91
|
span, ctx = self._client._start( # pyright: ignore[reportPrivateUsage]
|
|
87
|
-
self._name, self._details, step=self._step, mcp=self._mcp
|
|
92
|
+
self._name, self._details, step=self._step, mcp=self._mcp, link=self._link
|
|
88
93
|
)
|
|
89
94
|
self._span = span
|
|
90
|
-
self._token =
|
|
95
|
+
self._token = attach(ctx)
|
|
91
96
|
return self
|
|
92
97
|
|
|
93
98
|
def __exit__(
|
|
@@ -106,7 +111,7 @@ class MilestoneScope:
|
|
|
106
111
|
span.set_attribute("error.type", _error_type(exc))
|
|
107
112
|
span.end()
|
|
108
113
|
finally:
|
|
109
|
-
|
|
114
|
+
detach(token)
|
|
110
115
|
|
|
111
116
|
async def __aenter__(self) -> MilestoneScope:
|
|
112
117
|
return self.__enter__()
|
|
@@ -121,9 +126,10 @@ class MilestoneScope:
|
|
|
121
126
|
|
|
122
127
|
|
|
123
128
|
class Client:
|
|
124
|
-
"""Records backend evidence with
|
|
129
|
+
"""Records backend evidence with Agentrail's own tracer provider.
|
|
125
130
|
|
|
126
|
-
Get one from ``init
|
|
131
|
+
Get one from ``init``. It registers no OpenTelemetry global and never
|
|
132
|
+
changes the application's current span.
|
|
127
133
|
"""
|
|
128
134
|
|
|
129
135
|
def __init__(
|
|
@@ -221,33 +227,36 @@ class Client:
|
|
|
221
227
|
*,
|
|
222
228
|
step: bool = True,
|
|
223
229
|
mcp: McpEvidence | None = None,
|
|
230
|
+
link: SpanContext | None = None,
|
|
224
231
|
) -> tuple[Span, otel_context.Context]:
|
|
232
|
+
"""A milestone span parented on Agentrail's current span, and the context to make it current in."""
|
|
225
233
|
performance = None
|
|
226
234
|
if details.performance is not None:
|
|
227
235
|
try:
|
|
228
236
|
performance = performance_attributes(details.performance, self._runtime.options.input.hash_key)
|
|
229
237
|
except ConfigurationError:
|
|
230
238
|
self._runtime.report("invalid_performance", "context")
|
|
231
|
-
base =
|
|
239
|
+
base = current()
|
|
232
240
|
state = self._runtime.state(base)
|
|
233
|
-
continued: list[Link] = []
|
|
234
|
-
if state is not None and state.entry is not None:
|
|
235
|
-
current = trace.get_current_span(base).get_span_context()
|
|
236
|
-
if current.is_valid and (current.is_remote or current.trace_id != state.entry.trace_id):
|
|
237
|
-
# A trace continued from unsigned input inside the request (the MCP
|
|
238
|
-
# SDK continues a client's _meta traceparent) is a link, never the parent.
|
|
239
|
-
continued.append(Link(clean_context(current)))
|
|
240
|
-
base = trace.set_span_in_context(NonRecordingSpan(state.entry), base)
|
|
241
241
|
if state is None:
|
|
242
|
-
|
|
243
|
-
|
|
242
|
+
# Its own managed scope: the application's span it runs in (its trace,
|
|
243
|
+
# an unrelated caller's) is a link, never the parent. When the message
|
|
244
|
+
# names a trace, a span on that trace is the library that continued it
|
|
245
|
+
# (the MCP SDK continues _meta in OpenTelemetry's context): one link.
|
|
246
|
+
external = trace.get_current_span().get_span_context()
|
|
247
|
+
continued = link is not None and external.trace_id == link.trace_id
|
|
248
|
+
links = [Link(clean_context(external))] if external.is_valid and not continued else []
|
|
244
249
|
state = RequestState(links=links)
|
|
245
|
-
# Its own managed scope: an unrelated caller's trace is a link, never the parent.
|
|
246
250
|
base = otel_context.set_value(self._runtime.state_key, state, otel_context.Context())
|
|
247
251
|
mark = Mark(name=name, details=details, performance=performance, identity=identity, step=step, mcp=mcp)
|
|
248
252
|
marked = otel_context.set_value(self._runtime.mark_key, mark, base)
|
|
249
253
|
parented = trace.get_current_span(base).get_span_context().is_valid
|
|
250
|
-
|
|
254
|
+
links = [] if parented else list(state.links)
|
|
255
|
+
if link is not None:
|
|
256
|
+
# A trace the message itself names (an MCP client's _meta traceparent)
|
|
257
|
+
# is a link, never the parent.
|
|
258
|
+
links.append(Link(clean_context(link)))
|
|
259
|
+
span = self._tracer.start_span(name, context=marked, links=links)
|
|
251
260
|
if not span.is_recording():
|
|
252
261
|
self._runtime.report("entry_not_recording", "traces")
|
|
253
262
|
return span, trace.set_span_in_context(span, base)
|
|
@@ -263,7 +272,7 @@ class Client:
|
|
|
263
272
|
return
|
|
264
273
|
span = None
|
|
265
274
|
if state is not None:
|
|
266
|
-
context =
|
|
275
|
+
context = current_span().get_span_context()
|
|
267
276
|
span = context if context.is_valid else None
|
|
268
277
|
now = time.time_ns()
|
|
269
278
|
self._logs.add(copy_log(entry, now, now, span, self._runtime))
|
|
@@ -345,6 +354,7 @@ class Client:
|
|
|
345
354
|
remote_context = SpanContext(remote.trace_id, remote.span_id, True, TraceFlags(remote.flags & 1))
|
|
346
355
|
ctx = trace.set_span_in_context(NonRecordingSpan(remote_context), ctx)
|
|
347
356
|
state.location = self._location(request)
|
|
357
|
+
state.cli = cli_attributes(request.headers.get(CLI_HEADER), self._keyed_hash)
|
|
348
358
|
ctx = otel_context.set_value(self._runtime.state_key, state, ctx)
|
|
349
359
|
attributes: Attributes = {
|
|
350
360
|
"http.request.method": request.method,
|
|
@@ -17,8 +17,16 @@ from dataclasses import dataclass
|
|
|
17
17
|
from ._text import js_json, well_formed
|
|
18
18
|
|
|
19
19
|
HANDOFF_HEADER = "x-agentrail-trace-handoff"
|
|
20
|
-
#
|
|
21
|
-
|
|
20
|
+
# Agentrail's own headers, which a managed call never sends on as the server
|
|
21
|
+
# received them: a handoff binds one request, and a CLI's x-agentrail-cli names
|
|
22
|
+
# its run and session to this server alone. Every other header is the
|
|
23
|
+
# application's, its tracers' included, and is left alone.
|
|
24
|
+
AGENTRAIL_HEADERS = (HANDOFF_HEADER, "x-agentrail-cli")
|
|
25
|
+
# What a call to an allowed origin loses with the application's traceparent,
|
|
26
|
+
# which the signed one replaces: the vendor state that belonged to it.
|
|
27
|
+
SIGNED_REPLACES = ("traceparent", "tracestate")
|
|
28
|
+
# What a managed call to the collector itself never carries.
|
|
29
|
+
COLLECTOR_STRIPPED = ("traceparent", "tracestate", "baggage", "sentry-trace", *AGENTRAIL_HEADERS)
|
|
22
30
|
MAX_AGE_MS = 60_000
|
|
23
31
|
_MAX_SAFE_INTEGER = 9_007_199_254_740_991
|
|
24
32
|
|