Module: OpenAI::Chain

Defined in:
lib/new_relic/agent/instrumentation/ruby_openai/chain.rb

Class Method Summary collapse

Class Method Details

.instrument!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/new_relic/agent/instrumentation/ruby_openai/chain.rb', line 7

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