Migrated RubyLLM instrumentation to new integration API #79
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 migrates the RubyLLM instrumentation from the legacy monolithic implementation to the new modular Integration API framework. The refactoring improves maintainability, enables consistent auto-instrumentation patterns, adds support for instance-level tracing, and enables it for auto-instrumentation.
Key changes:
target:optionArchitectural Overview
The new implementation follows the three-tier Integration API structure:
Integration Layer (
integration.rb)Handles gem detection, version validation, and the high-level
instrument!entry point. Defines metadata likeGEM_NAMES,REQUIRE_PATHS, andMINIMUM_VERSION(1.8.0).Patcher Layer (
patcher.rb)ChatPatchermanages thread-safe patching ofRubyLLM::Chat. Supports both class-level patching (all instances) and instance-level patching (specific chat objects via singleton class).Instrumentation Modules (
instrumentation/)chat.rb- Wrapscomplete()andexecute_tool()methods usingModule#prependto create OpenTelemetry spanscommon.rb- Token usage normalization utilities shared across streaming/non-streaming flowsBackward Compatibility (
deprecated.rb)Legacy
Braintrust::Trace::Contrib::Github::Crmne::RubyLLM.wrap()API is preserved with deprecation warnings, delegating to the newBraintrust.instrument!()API.Usage Examples
Class-Level Instrumentation
Instrument all RubyLLM chat instances automatically:
Instance-Level Instrumentation
Instrument only specific chat instances:
Streaming Support
Streaming responses are automatically traced with chunk aggregation:
Tool Usage
Tool calls create nested spans automatically:
What Changed
Braintrust::Trace::Contrib::Github::Crmne::RubyLLM.wrap(chat)Braintrust.instrument!(:ruby_llm, target: chat)Braintrust::Trace::Contrib::Github::Crmne::RubyLLM.wrap()Braintrust.instrument!(:ruby_llm)on_tool_call/on_tool_resultcallbacksexecute_tool()method wrappingMigration Path
The legacy API continues to work but emits deprecation warnings: