Class: Braintrust::Contrib::Patcher

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

Overview

Base class for all patchers. Provides thread-safe, idempotent patching with error handling.

Class Method Summary collapse

Class Method Details

.applicable?Boolean

Override in subclasses to check if patcher should apply. Called after patcher loads but before perform_patch.

Returns:

  • (Boolean)

    true if this patcher should be applied



28
29
30
# File 'lib/braintrust/contrib/patcher.rb', line 28

def applicable?
  true # Default: always applicable
end

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  subclass.instance_variable_set(:@patch_mutex, Mutex.new)
end

.patch!(**options) ⇒ Boolean

Apply the patch (thread-safe and idempotent).

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

Returns:

  • (Boolean)

    true if patching succeeded or was already done



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/braintrust/contrib/patcher.rb', line 37

def patch!(**options)
  return false unless applicable?
  return true if patched?(**options) # Fast path

  @patch_mutex.synchronize do
    unless applicable?
      Braintrust::Log.debug("Skipping #{name} - not applicable")
      return false
    end
    return true if patched?(**options) # Double-check under lock

    perform_patch(**options)
    @patched = true
  end
  Braintrust::Log.debug("Patched #{name}")
  true
rescue => e
  Braintrust::Log.error("Failed to patch #{name}: #{e.message}")
  false
end

.patched?(**options) ⇒ Boolean

Has this patcher already been applied?

Returns:

  • (Boolean)


21
22
23
# File 'lib/braintrust/contrib/patcher.rb', line 21

def patched?(**options)
  @patched == true
end

.perform_patch(**options) ⇒ void

This method returns an undefined value.

Subclasses implement this to perform the actual patching. This method is called under lock after applicable? returns true.

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

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/braintrust/contrib/patcher.rb', line 65

def perform_patch(**options)
  raise NotImplementedError, "#{self} must implement perform_patch"
end

.reset!Object

Reset patched state (primarily for testing).



70
71
72
# File 'lib/braintrust/contrib/patcher.rb', line 70

def reset!
  @patched = false
end