Class: Opencode::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/opencode/tracer.rb

Overview

A namespacing trace emitter.

Opencode::Turn emits unprefixed event names like “response.started” and “session.recreated”. The host product wraps Turn in a Tracer whose job is to prepend a product prefix and forward to whatever actually emits trace events (typically the host job’s ‘EventTraceable#trace_event`).

Two responsibilities live here, and only here:

1. Callable interface: `tracer.call(name, **payload)` — the
   contract Turn relies on.
2. Namespacing strategy: prepend "<prefix>." to every event name.

A closure-based alternative that mixes both concerns looks like:

tracer: ->(name, **payload) { trace_event("myapp.#{name}", **payload) }

That closure conflates the two responsibilities; every caller has to rediscover the prefix-with-period rule, and a typo only shows up in production trace data. Making it a real role removes that risk and makes the rule visible in one place.

Usage:

Opencode::Tracer.new(prefix: "myapp", emitter: self)

‘emitter` must respond to `trace_event(name, **payload)`.

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, emitter:) ⇒ Tracer

Returns a new instance of Tracer.



33
34
35
36
# File 'lib/opencode/tracer.rb', line 33

def initialize(prefix:, emitter:)
  @prefix = prefix
  @emitter = emitter
end

Instance Method Details

#call(name, **payload) ⇒ Object

Tracer is callable so existing call sites that treated the tracer as a lambda (‘tracer.call(name, **payload)`) keep working without change. Turn uses this exclusively.

Uses ‘send` because EventTraceable’s ‘trace_event` is a private method of the including class — the convention is “private inside the job, but the substrate’s Tracer is allowed to dispatch to it the same way the job’s own perform method would.”



46
47
48
# File 'lib/opencode/tracer.rb', line 46

def call(name, **payload)
  @emitter.send(:trace_event, "#{@prefix}.#{name}", **payload)
end