Migrated openai instrumentation to new integration API #76
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 OpenAI instrumentation from a monolithic implementation (
lib/braintrust/trace/contrib/openai.rb) to the new Integration API framework. By putting this behind a consistent programmatic interface, it will help facilitate the implementation of auto-instrumentation.Architectural Overview
The new integration uses a three-layer architecture:
1. Integration (
lib/braintrust/contrib/openai/integration.rb)instrument!for high-level API2. Patcher (
lib/braintrust/contrib/openai/patcher.rb)ChatPatcher- Patcheschat.completionsmethodsResponsesPatcher- PatchesresponsesAPI (new OpenAI feature)3. Instrumentation Modules (
lib/braintrust/contrib/openai/instrumentation/)Chat::Completions- Wrapscreate(),stream(),stream_raw()Responses- Wraps responses API methodsCommon- Shared utilities for parsing usage, aggregating streamsThe instrumentation modules use
Module#prependto wrap methods, creating OpenTelemetry spans with input/output/metrics attributes.Usage Examples
Class-level instrumentation (all clients):
Instance-level instrumentation (specific client):
Files Changed
lib/braintrust/trace/contrib/openai.rb(611 lines) - Monolithic implementationlib/braintrust/contrib/openai/:integration.rb- Integration metadata and discoverypatcher.rb- ChatPatcher and ResponsesPatcher classesinstrumentation/chat.rb- Chat completions instrumentationinstrumentation/responses.rb- Responses API instrumentationinstrumentation/common.rb- Shared utilities