-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2442 from newrelic/openai_instrumentation
AIM Instrumentation - OpenAI
- Loading branch information
Showing
41 changed files
with
1,945 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
require_relative 'ruby_openai/instrumentation' | ||
require_relative 'ruby_openai/chain' | ||
require_relative 'ruby_openai/prepend' | ||
|
||
DependencyDetection.defer do | ||
named :'ruby_openai' | ||
|
||
depends_on do | ||
NewRelic::Agent.config[:'ai_monitoring.enabled'] && | ||
defined?(OpenAI) && defined?(OpenAI::Client) && | ||
Gem::Version.new(OpenAI::VERSION) >= Gem::Version.new('3.4.0') | ||
end | ||
|
||
executes do | ||
if use_prepend? | ||
# TODO: Remove condition when we drop support for versions below 5.0.0 | ||
if Gem::Version.new(OpenAI::VERSION) >= Gem::Version.new('5.0.0') | ||
prepend_instrument OpenAI::Client, | ||
NewRelic::Agent::Instrumentation::OpenAI::Prepend, | ||
NewRelic::Agent::Instrumentation::OpenAI::VENDOR | ||
else | ||
prepend_instrument OpenAI::Client.singleton_class, | ||
NewRelic::Agent::Instrumentation::OpenAI::Prepend, | ||
NewRelic::Agent::Instrumentation::OpenAI::VENDOR | ||
end | ||
else | ||
chain_instrument NewRelic::Agent::Instrumentation::OpenAI::Chain, | ||
NewRelic::Agent::Instrumentation::OpenAI::VENDOR | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module OpenAI::Chain | ||
def self.instrument! | ||
::OpenAI::Client.class_eval do | ||
include NewRelic::Agent::Instrumentation::OpenAI | ||
|
||
alias_method(:json_post_without_new_relic, :json_post) | ||
|
||
# In versions 4.0.0+ json_post is an instance method | ||
# defined in the OpenAI::HTTP module, included by the | ||
# OpenAI::Client class | ||
def json_post(**kwargs) | ||
json_post_with_new_relic(**kwargs) do | ||
json_post_without_new_relic(**kwargs) | ||
end | ||
end | ||
|
||
# In versions below 4.0.0 json_post is a class method | ||
# on OpenAI::Client | ||
class << self | ||
alias_method(:json_post_without_new_relic, :json_post) | ||
|
||
def json_post(**kwargs) | ||
json_post_with_new_relic(**kwargs) do | ||
json_post_without_new_relic(**kwargs) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.