Repository navigation
Commit 8f85b89
authored
fix(otel): don't log an error when a span ends in another context (#1071)
> 🤖 **This text was AI-generated.**
With `BRAINTRUST_OTEL_COMPAT=true`, async frameworks such as LangGraph
can start a span in one asyncio task and end it in another. Each task
runs in its own `contextvars.Context`, so the token saved when the span
became current belongs to a different Context than the one ending it:
```
task A (Context 1) task B (Context 2)
on_chain_start on_chain_end
span.set_current() span.unset_current()
token = attach(ctx) context.detach(token)
-> ValueError (other Context)
-> OTel logs it at ERROR
```
OpenTelemetry's `context.detach()` catches that `ValueError` itself and
logs "Failed to detach context" with a traceback. Nothing reaches
Braintrust, so the `except ValueError` guard in the LangChain callback
handler never ran. Every such run printed an ERROR, even though span
parenting was correct (#1062).
The OTel `ContextManager` now resets OTel's default contextvars token
directly instead of calling `context.detach()`:
```python
try:
token.var.reset(token)
except ValueError:
# token came from another Context; leave both contexts as-is
log.debug(...)
except Exception:
log.exception("Failed to detach context")
```
- A token from another Context is logged at DEBUG. Neither context is
changed, so an unrelated span that is active where the run ends stays
current.
- Other failures, such as reusing a token, are still logged at ERROR and
not raised, same as before.
- Tokens from other OTel runtime implementations still go through
`context.detach()`.
The dead `try/except ValueError` around `span.unset_current()` in the
LangChain handler is removed, since `unset_current()` never raises.
Limitation: the Context where the span started still treats the ended
span as current. `BraintrustContextManager` skips ended spans when it
looks up the current one; the OTel manager does not yet.
Fixes #10621 parent 3f5b3d4 commit 8f85b89
4 files changed
Lines changed: 93 additions & 12 deletions
File tree
- py
- src/braintrust
- integrations/langchain
- otel
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
929 | 929 | | |
930 | 930 | | |
931 | 931 | | |
932 | | - | |
| 932 | + | |
933 | 933 | | |
934 | 934 | | |
935 | 935 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
| 249 | + | |
258 | 250 | | |
259 | 251 | | |
260 | 252 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
93 | | - | |
94 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
95 | 100 | | |
96 | 101 | | |
97 | 102 | | |
| |||
108 | 113 | | |
109 | 114 | | |
110 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
111 | 131 | | |
112 | 132 | | |
113 | 133 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
387 | 389 | | |
388 | 390 | | |
389 | 391 | | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
390 | 459 | | |
391 | 460 | | |
0 commit comments