Module: Foam::Otel::PrependCollisionShield

Defined in:
lib/foam/otel/prepend_collision_shield.rb

Class Method Summary collapse

Class Method Details

.dispatch(receiver, defs, caller_path, args, kwargs, block) ⇒ Object

The dispatch core, called from every shim method. Public for the shim modules only; not customer surface.



82
83
84
85
86
87
88
89
# File 'lib/foam/otel/prepend_collision_shield.rb', line 82

def dispatch(receiver, defs, caller_path, args, kwargs, block)
  impl = resolve(defs, caller_path, args, kwargs)
  if kwargs.empty?
    impl.bind_call(receiver, *args, &block)
  else
    impl.bind_call(receiver, *args, **kwargs, &block)
  end
end

.install!(klass) ⇒ Object

Scan + shield the class's prepend chain. Idempotent per collision set: names already shielded by a live foam shim are skipped. Never raises (rule 9): a scan failure degrades to a loud no-op.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/foam/otel/prepend_collision_shield.rb', line 58

def install!(klass)
  front = klass.ancestors.take_while { |m| !m.equal?(klass) }
  already = front.select { |m| shim?(m) }.flat_map { |m| m.instance_variable_get(:@foam_shielded_names) || [] }
  foreign = front.reject { |m| shim?(m) }
  collisions = colliding_helpers(klass, foreign).reject { |name, _| already.include?(name) }
  return [] if collisions.empty?

  klass.prepend(build_shim(collisions))
  collisions.each do |name, defs|
    owners = defs.map { |d| d[:mod].name || d[:mod].inspect }.join(" AND ")
    Diagnostics.warn("coexistence shield: private helper ##{name} on #{klass} is defined at " \
                     "incompatible signatures by #{owners} — one would crash the other's callers " \
                     "(and every #{klass} user). Foam installed a signature-dispatch shim so both " \
                     "keep working (GOTCHAS F18).")
  end
  collisions.keys
rescue StandardError, SystemStackError => e
  Diagnostics.warn("coexistence shield: scan of #{klass} failed (#{e.class}: #{e.message}) — " \
                   "no shim installed")
  []
end