Module: Riffer::Tracing

Extended by:
Tracing
Included in:
Tracing
Defined in:
lib/riffer/tracing.rb

Overview

Internal tracing port — emits OTEL spans when the host bundles the OpenTelemetry API and no-ops otherwise, so riffer never declares an OTEL dependency.

Defined Under Namespace

Modules: Capture, Null Classes: Otel, StreamRecorder

Constant Summary collapse

MUTEX =
Mutex.new
SCHEMA_URL =

The Ruby API cannot attach a schema URL to a tracer, so the semconv pin lives here as the documented contract version.

"https://opentelemetry.io/schemas/1.37.0"

Instance Method Summary collapse

Instance Method Details

#current_contextObject

Returns the active trace context, for re-attachment across fiber or thread boundaries. – : () -> untyped



30
31
32
33
# File 'lib/riffer/tracing.rb', line 30

def current_context
  return Null.current_context unless Riffer.config.tracing.enabled
  backend.current_context
end

#in_span(name, attributes: nil, kind: :internal, &block) ⇒ Object

Opens a span around the block, yielding the span. – : [R] (String, ?attributes: Hash[String, untyped]?, ?kind: Symbol) { (Riffer::Tracing::Otel::Span | Riffer::Tracing::Null::Span) -> R } -> R



21
22
23
24
# File 'lib/riffer/tracing.rb', line 21

def in_span(name, attributes: nil, kind: :internal, &block)
  return Null.in_span(name, &block) unless Riffer.config.tracing.enabled
  backend.in_span(name, attributes: attributes, kind: kind, &block)
end

#record_usage(span, usage) ⇒ Object

Stamps token usage onto the span — the gen_ai.usage.* counts and, when the model was priced, riffer.cost. – : ((Riffer::Tracing::Otel::Span | Riffer::Tracing::Null::Span), Riffer::Providers::TokenUsage?) -> void



48
49
50
51
52
53
54
55
56
# File 'lib/riffer/tracing.rb', line 48

def record_usage(span, usage)
  return unless usage

  span.set_attribute("gen_ai.usage.input_tokens", usage.input_tokens)
  span.set_attribute("gen_ai.usage.output_tokens", usage.output_tokens)
  span.set_attribute("gen_ai.usage.cache_read.input_tokens", usage.cache_read_tokens) if usage.cache_read_tokens
  span.set_attribute("gen_ai.usage.cache_creation.input_tokens", usage.cache_write_tokens) if usage.cache_write_tokens
  span.set_attribute("riffer.cost", usage.cost) if usage.cost
end

#reset!Object

Discards the resolved backend so the next span re-resolves it. – : () -> void



61
62
63
# File 'lib/riffer/tracing.rb', line 61

def reset!
  MUTEX.synchronize { @backend = nil }
end

#with_context(context, &block) ⇒ Object

Runs the block with the given trace context active; nil passes through so captures taken while tracing was dark stay harmless. – : [R] (untyped) { () -> R } -> R



39
40
41
42
# File 'lib/riffer/tracing.rb', line 39

def with_context(context, &block)
  return Null.with_context(context, &block) unless Riffer.config.tracing.enabled
  backend.with_context(context, &block)
end