Commit c7e3b52
authored
feat: otel compatible distributed tracing (#496)
- use otel-friendly IDs by default in the SDK
- add inject/extract methods for distributed tracing instead of parent
slug methods
- implements: https://www.w3.org/TR/trace-context/
OLD WAY:
https://www.braintrust.dev/docs/instrument/advanced-tracing#trace-distributed-systems
```python
from braintrust import current_span, init_logger, start_span, traced
logger = init_logger(project="my-project")
# Client: Export the span
@Traced
def process_request(request):
return requests.post(
"/api/process",
json=request,
headers={"X-Trace-ID": current_span().export()},
)
# Server: Resume the trace
def handle_request(req):
trace_id = req.headers.get("X-Trace-ID")
with start_span(parent=trace_id) as span:
result = process_data(req.body)
span.log(input=req.body, output=result)
return result
```
NOTE: the "old way" is still supported and backwards-compatible. Native
SDKs can continue to link to each other in the manner, even if one is
upgraded and the other is not. However if users with to link to otel
sdks, they will have to change their code to use the new apis.
NEW WAY:
```python
from braintrust import current_span, init_logger, start_span, traced
logger = init_logger(project="my-project")
# Client: Export the span
@Traced
def process_request(request):
return requests.post(
"/api/process",
json=request,
headers=current_span().inject({}), # <--- new inject api
)
# Server: Resume the trace
def handle_request(req):
with start_span(parent=extract_trace_context(req.headers) as span: # <--- new extract api
result = process_data(req.body)
span.log(input=req.body, output=result)
return result
```
# Other notes
- confusing naming: this has the unfortunate effect of the
`root_span_id` field essentially becoming the trace id rather than the
id of the root span (i.e. `root_span_id` != `rootSpan.id`). This sucks,
but it's already the case for otel sdks and py otel compat mode. So at
least we're consistent.
- legacy id flag: the sdk currently maintains two paths for id
generation. it defaults to UUIDs and does hex IDs when we're in in otel
compat mode. This changes the default to use hex IDs and has a flag to
revert to the old behavior. We could just rip out the old uuid logic
entirely, but this approach seems like the least risky
- to support passing through tracestate and flags, an additional field
was added to SpanImpl (propagated_state)
# Examples
-
https://www.braintrust.dev/app/Braintrust%20SDKs/p/distributed-tracing-test/logs?range=%221h%22&r=021fb6471e30c686a667ffdfbba15ee6&s=67123a9c35899f93
-
https://www.braintrust.dev/app/Braintrust%20SDKs/p/distributed-tracing-test/logs?range=%221h%22&r=5be69e7ec10f257b8671417162bcf075&s=050480159793924b1 parent eb674a6 commit c7e3b52
16 files changed
Lines changed: 1927 additions & 154 deletions
File tree
- py/src/braintrust
- integrations
- claude_agent_sdk
- huggingface_hub
- langchain
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
| |||
150 | 154 | | |
151 | 155 | | |
152 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
153 | 198 | | |
154 | 199 | | |
155 | 200 | | |
| |||
163 | 208 | | |
164 | 209 | | |
165 | 210 | | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
13 | 16 | | |
14 | | - | |
15 | | - | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| |||
Lines changed: 14 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
214 | 217 | | |
215 | | - | |
216 | | - | |
| 218 | + | |
| 219 | + | |
217 | 220 | | |
218 | 221 | | |
219 | | - | |
| 222 | + | |
220 | 223 | | |
221 | 224 | | |
222 | 225 | | |
| |||
454 | 457 | | |
455 | 458 | | |
456 | 459 | | |
457 | | - | |
| 460 | + | |
| 461 | + | |
458 | 462 | | |
459 | 463 | | |
460 | 464 | | |
| |||
546 | 550 | | |
547 | 551 | | |
548 | 552 | | |
549 | | - | |
| 553 | + | |
| 554 | + | |
550 | 555 | | |
551 | 556 | | |
552 | 557 | | |
| |||
681 | 686 | | |
682 | 687 | | |
683 | 688 | | |
684 | | - | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
685 | 692 | | |
686 | 693 | | |
687 | 694 | | |
| |||
Lines changed: 0 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
252 | | - | |
253 | 252 | | |
254 | 253 | | |
255 | 254 | | |
| |||
317 | 316 | | |
318 | 317 | | |
319 | 318 | | |
320 | | - | |
321 | 319 | | |
322 | 320 | | |
323 | 321 | | |
| |||
476 | 474 | | |
477 | 475 | | |
478 | 476 | | |
479 | | - | |
480 | 477 | | |
481 | 478 | | |
482 | 479 | | |
| |||
Lines changed: 22 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
65 | 67 | | |
| 68 | + | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
| |||
81 | 84 | | |
82 | 85 | | |
83 | 86 | | |
84 | | - | |
| 87 | + | |
85 | 88 | | |
86 | 89 | | |
87 | 90 | | |
| |||
97 | 100 | | |
98 | 101 | | |
99 | 102 | | |
100 | | - | |
| 103 | + | |
101 | 104 | | |
102 | 105 | | |
103 | 106 | | |
| |||
144 | 147 | | |
145 | 148 | | |
146 | 149 | | |
147 | | - | |
| 150 | + | |
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
| |||
171 | 174 | | |
172 | 175 | | |
173 | 176 | | |
| 177 | + | |
174 | 178 | | |
175 | 179 | | |
176 | 180 | | |
| |||
189 | 193 | | |
190 | 194 | | |
191 | 195 | | |
192 | | - | |
| 196 | + | |
193 | 197 | | |
194 | 198 | | |
195 | 199 | | |
| |||
205 | 209 | | |
206 | 210 | | |
207 | 211 | | |
208 | | - | |
| 212 | + | |
209 | 213 | | |
210 | 214 | | |
211 | 215 | | |
| |||
252 | 256 | | |
253 | 257 | | |
254 | 258 | | |
255 | | - | |
| 259 | + | |
256 | 260 | | |
257 | 261 | | |
258 | 262 | | |
| |||
301 | 305 | | |
302 | 306 | | |
303 | 307 | | |
| 308 | + | |
304 | 309 | | |
305 | 310 | | |
306 | 311 | | |
307 | 312 | | |
308 | 313 | | |
309 | 314 | | |
310 | | - | |
| 315 | + | |
311 | 316 | | |
312 | 317 | | |
313 | 318 | | |
| |||
640 | 645 | | |
641 | 646 | | |
642 | 647 | | |
643 | | - | |
| 648 | + | |
644 | 649 | | |
645 | 650 | | |
646 | 651 | | |
647 | 652 | | |
648 | 653 | | |
649 | | - | |
| 654 | + | |
650 | 655 | | |
651 | 656 | | |
652 | 657 | | |
| |||
721 | 726 | | |
722 | 727 | | |
723 | 728 | | |
| 729 | + | |
| 730 | + | |
724 | 731 | | |
| 732 | + | |
725 | 733 | | |
726 | 734 | | |
727 | 735 | | |
| |||
751 | 759 | | |
752 | 760 | | |
753 | 761 | | |
754 | | - | |
| 762 | + | |
755 | 763 | | |
756 | 764 | | |
757 | 765 | | |
| |||
765 | 773 | | |
766 | 774 | | |
767 | 775 | | |
768 | | - | |
| 776 | + | |
769 | 777 | | |
770 | 778 | | |
771 | 779 | | |
| |||
781 | 789 | | |
782 | 790 | | |
783 | 791 | | |
784 | | - | |
| 792 | + | |
785 | 793 | | |
786 | 794 | | |
787 | 795 | | |
| |||
797 | 805 | | |
798 | 806 | | |
799 | 807 | | |
800 | | - | |
| 808 | + | |
801 | 809 | | |
802 | 810 | | |
803 | 811 | | |
| |||
813 | 821 | | |
814 | 822 | | |
815 | 823 | | |
816 | | - | |
| 824 | + | |
817 | 825 | | |
818 | 826 | | |
819 | 827 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
66 | 68 | | |
| 69 | + | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| |||
83 | 86 | | |
84 | 87 | | |
85 | 88 | | |
86 | | - | |
| 89 | + | |
87 | 90 | | |
88 | 91 | | |
89 | 92 | | |
| |||
99 | 102 | | |
100 | 103 | | |
101 | 104 | | |
102 | | - | |
| 105 | + | |
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
| |||
146 | 149 | | |
147 | 150 | | |
148 | 151 | | |
149 | | - | |
| 152 | + | |
150 | 153 | | |
151 | 154 | | |
152 | 155 | | |
| |||
0 commit comments