Add auto-instrumentation via BraintrustTracingLayer#22
Add auto-instrumentation via BraintrustTracingLayer#22Stephen Belanger (Qard) wants to merge 1 commit intomainfrom
Conversation
0362da0 to
30e3af6
Compare
19f5959 to
c625768
Compare
714248a to
978d5fd
Compare
Abhijeet Prasad (AbhiPrasad)
left a comment
There was a problem hiding this comment.
I guess the tracing layer runs on every tracing event, like every call to tracing::info!. Can we avoid that given we only care about genai spans? It would help with overhead a lot in high throughput apps.
c149e34 to
17f0e9a
Compare
|
I think we still need to take care of the fact that the tracing layer runs on every tracing event. |
a1c1b8a to
bb25277
Compare
|
Ankur Goyal (@ankrgyl) Manu Goyal (@manugoyal) Any idea what AI SDKs should be prioritized for Rust auto-instrumentation? The idea here is to build on the excellent |
a41a188 to
e75127d
Compare
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
e75127d to
ab23f92
Compare
There was a problem hiding this comment.
I think we can clean this up a bit, we probably only need to document Approach 1 and 2. Might also make sense to move this into a docs folder.
| ); | ||
| return; | ||
| }; | ||
| handle.spawn(async move { |
There was a problem hiding this comment.
should we bound the number of tasks we spawn?
| let mut metadata = serde_json::Map::new(); | ||
| if let Some(provider) = data.provider_name { | ||
| metadata.insert("provider".to_string(), serde_json::json!(provider)); | ||
| } |
There was a problem hiding this comment.
This is quite a lot of boilerplate. Can we avoid doing this if we are just going to pass it into log_builder.metadata anyway? We can just make the builder take these different attributes, not caring if they are Option<>
| "gen_ai.provider.name" => { | ||
| if let Value::String(s) = value { | ||
| self.provider_name = Some(s); | ||
| } | ||
| } |
There was a problem hiding this comment.
Doesn't block, but maybe makes sense to use a macro for these calls.
Summary
This PR implements true auto-instrumentation for the Braintrust Rust SDK using the
tracingcrate and OpenTelemetry GenAI semantic conventions.With this change, users can install a tracing subscriber once and have all AI SDK calls automatically logged to Braintrust - zero manual code needed.
What's New
BraintrustTracingLayer
A new
tracing_subscriber::Layerthat:SpanLogentriesUsage
Implementation Details
Core Components
src/tracing_layer/mod.rs: Main layer implementationLayer<S>traiton_new_span,on_record,on_event,on_closegen_ai.*target prefixsrc/tracing_layer/data.rs:GenAISpanDatastorage structsrc/tracing_layer/visitor.rs:FieldVisitorfor field extractiontracing::field::Visitserde_json::ValueSupported SDKs
This works with any SDK that emits OpenTelemetry GenAI events:
instrumentationfeature)instrumentationfeature) - covers 11+ providersTesting
Dependencies
Added
tracing-subscriberwithregistryfeature:Documentation
examples/auto_instrumentation.rsdocs/auto-instrumentation.mdBenefits
Breaking Changes
None. This is a purely additive feature.
Related Work
This PR is part of a broader effort to bring auto-instrumentation to Rust AI applications. Future work includes:
Checklist
🤖 Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com