Class: Braintrust::Contrib::Anthropic::MessagesPatcher

Inherits:
Patcher
  • Object
show all
Defined in:
lib/braintrust/contrib/anthropic/patcher.rb

Overview

Patcher for Anthropic messages. Instruments Anthropic::Messages#create and #stream methods.

Class Method Summary collapse

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 14

def applicable?
  defined?(::Anthropic::Client)
end

.patched?(**options) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 18

def patched?(**options)
  target_class = get_singleton_class(options[:target]) || ::Anthropic::Resources::Messages
  Instrumentation::Messages.applied?(target_class)
end

.perform_patch(**options) ⇒ void

This method returns an undefined value.

Perform the actual patching.

Parameters:

  • options (Hash)

    Configuration options passed from integration

Options Hash (**options):

  • :target (Object)

    Optional target instance to patch

  • :tracer_provider (OpenTelemetry::SDK::Trace::TracerProvider)

    Optional tracer provider



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/braintrust/contrib/anthropic/patcher.rb', line 28

def perform_patch(**options)
  return unless applicable?

  # MessageStream is shared across all clients, so patch at class level.
  # The instrumentation short-circuits when no context is present,
  # so uninstrumented clients' streams pass through unaffected.
  patch_message_stream

  if options[:target]
    # Instance-level (for only this client instance)
    raise ArgumentError, "target must be a kind of ::Anthropic::Client" unless options[:target].is_a?(::Anthropic::Client)

    get_singleton_class(options[:target]).include(Instrumentation::Messages)
  else
    # Class-level (for all client instances)
    ::Anthropic::Resources::Messages.include(Instrumentation::Messages)
  end
end