feat: add instrumentation for openai gem#1797
feat: add instrumentation for openai gem#1797xuan-cao-swi wants to merge 11 commits intoopen-telemetry:mainfrom
Conversation
|
@xuan-cao-swi did you generate this using |
|
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
|
@arielvalentin yes, I did use Are we starting to include these before making the release of new gem? |
|
Yes. All of those changes should be included by default unless you want to explicitly omit this instrumentation from all and defer releasing this gem. |
…etry-ruby-contrib into openai-ruby-instr
|
Done. |
|
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
|
@xuan-cao-swi can you sync this with main? |
|
@xuan-cao-swi can I ask you to address broken tests? |
| gem 'opentelemetry-instrumentation-base', path: '../base' | ||
| gem 'appraisal', '~> 2.5' | ||
| gem 'bundler', '~> 2.4' | ||
| gem 'minitest', '~> 5.0' | ||
| gem 'openai', '~> 0.36.0' | ||
| gem 'opentelemetry-sdk', '~> 1.0' | ||
| gem 'opentelemetry-test-helpers', '~> 0.3' | ||
| gem 'rake', '~> 13.0' | ||
| gem 'rubocop', '~> 1.81.1' | ||
| gem 'rubocop-minitest', '~> 0.39.0' | ||
| gem 'rubocop-rspec', '~> 3.9.0' | ||
| gem 'rubocop-rake', '~> 0.7.1' | ||
| gem 'rubocop-performance', '~> 1.26.0' | ||
| gem 'simplecov', '~> 0.17.1' | ||
| gem 'webmock', '~> 3.24' | ||
| gem 'yard', '~> 0.9' |
There was a problem hiding this comment.
| gem 'opentelemetry-instrumentation-base', path: '../base' | |
| gem 'appraisal', '~> 2.5' | |
| gem 'bundler', '~> 2.4' | |
| gem 'minitest', '~> 5.0' | |
| gem 'openai', '~> 0.36.0' | |
| gem 'opentelemetry-sdk', '~> 1.0' | |
| gem 'opentelemetry-test-helpers', '~> 0.3' | |
| gem 'rake', '~> 13.0' | |
| gem 'rubocop', '~> 1.81.1' | |
| gem 'rubocop-minitest', '~> 0.39.0' | |
| gem 'rubocop-rspec', '~> 3.9.0' | |
| gem 'rubocop-rake', '~> 0.7.1' | |
| gem 'rubocop-performance', '~> 1.26.0' | |
| gem 'simplecov', '~> 0.17.1' | |
| gem 'webmock', '~> 3.24' | |
| gem 'yard', '~> 0.9' | |
| gem 'opentelemetry-instrumentation-base', path: '../base' | |
| gem 'appraisal', '~> 2.5.0' | |
| gem 'bundler', '~> 2.4.0' | |
| gem 'minitest', '~> 5.0' | |
| gem 'openai', '~> 0.36.0' | |
| gem 'opentelemetry-sdk', '~> 1.0.0' | |
| gem 'opentelemetry-test-helpers', '~> 0.3.0' | |
| gem 'rake', '~> 13.0.0' | |
| gem 'rubocop', '~> 1.81.1' | |
| gem 'rubocop-minitest', '~> 0.39.0' | |
| gem 'rubocop-rspec', '~> 3.9.0' | |
| gem 'rubocop-rake', '~> 0.7.1' | |
| gem 'rubocop-performance', '~> 1.26.0' | |
| gem 'simplecov', '~> 0.17.1' | |
| gem 'webmock', '~> 3.24.0' | |
| gem 'yard', '~> 0.9.0' |
| module OpenAI | ||
| module Patches | ||
| # OpenAIClient Patch | ||
| # rubocop:disable Metrics/ModuleLength |
There was a problem hiding this comment.
Can we move this to the rubocop config file
| 'gen_ai.request.model' => model, | ||
| 'server.address' => uri&.host || 'api.openai.com', | ||
| 'server.port' => uri&.port || 443, | ||
| 'http.request.method' => req[:method].to_s.upcase, |
There was a problem hiding this comment.
| 'http.request.method' => req[:method].to_s.upcase, |
| 'server.address' => uri&.host || 'api.openai.com', | ||
| 'server.port' => uri&.port || 443, | ||
| 'http.request.method' => req[:method].to_s.upcase, | ||
| 'url.path' => req[:path], |
There was a problem hiding this comment.
| 'url.path' => req[:path], |
Description
This PR introduces OpenTelemetry instrumentation for the
openaiRuby gem from official OpenAI Ruby SDK, enabling automatic tracing for OpenAI API calls including chat completions, streaming responses, and embeddings. The main idea is to monkey-patch the request function which every openai api call use this function with different path.The captured content is the content of message from user and assistants. It will only log the content if it's enabled.
requestfunction takes all different openai ruby gem api call, but for now it only supportchat(with stream) andembeddingsChat Completion
Streaming Chat Completion
Text Embeddings