Automatically initialize and instrument with require 'braintrust/setup'
#82
+1,047
−20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
This PR introduces a new entry point for automatic SDK initialization and LLM library instrumentation. Users can now simply
require "braintrust/setup"before their LLM libraries, and instrumentation happens automatically as libraries are loaded - no manualBraintrust.initcall needed.What Changed
Previous Approach (
Braintrust.init)This approach still works, but is less preferential to the...
New Approach (
braintrust/setup)Usage Examples
Ruby Application (without Bundler)
Rails Application (or applications with `require 'bundler/setup')
No additional configuration needed - the Railtie handles everything.
Environment Variables
BRAINTRUST_API_KEYBRAINTRUST_AUTO_INSTRUMENT"false"to disable (default:true)BRAINTRUST_INSTRUMENT_ONLY"openai,anthropic")BRAINTRUST_INSTRUMENT_EXCEPTArchitectural Overview
Key Components
lib/braintrust/setup.rb- User-facing entry pointBraintrust.initautomaticallyContrib::Setup.run!for library instrumentationlib/braintrust/contrib/setup.rb- Instrumentation orchestrationKernel#requireto intercept library loadslib/braintrust/contrib/rails/railtie.rb- Rails integrationconfig.after_initializehookBraintrust.auto_instrument!after all gems are loadedBRAINTRUST_INSTRUMENT_ONLY/BRAINTRUST_INSTRUMENT_EXCEPTlib/braintrust/contrib/registry.rb- Enhanced matchinggem_entry_point?method prevents false positivesrequire "anthropic")require "ruby_llm/providers/anthropic")Files Changed
lib/braintrust/setup.rblib/braintrust/contrib/setup.rblib/braintrust/contrib/rails/railtie.rblib/braintrust/contrib/registry.rbREADME.mdtest/braintrust/setup_test.rbtest/braintrust/contrib/setup_test.rbtest/braintrust/contrib/rails/railtie_test.rbexamples/setup/require.rbexamples/setup/require_minimal.rb