Class: Riffer::Tracing::Otel

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/tracing/otel.rb

Overview

OTEL-backed tracing backend. ::OpenTelemetry constants appear only inside method bodies here, so the gem loads and eager-loads cleanly when the OpenTelemetry API is absent.

Defined Under Namespace

Classes: Span

Constant Summary collapse

SUPPORTED_API_VERSIONS =

:nodoc: all

Gem::Requirement.new(">= 1.1", "< 2")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:) ⇒ Otel

– : (provider: untyped) -> void



104
105
106
# File 'lib/riffer/tracing/otel.rb', line 104

def initialize(provider:)
  @tracer = provider.tracer("riffer", Riffer::VERSION)
end

Class Method Details

.available?Boolean

Whether the OpenTelemetry API gem is loadable at a supported version. – : () -> bool

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/riffer/tracing/otel.rb', line 73

def available?
  version = api_version
  !version.nil? && supported?(version)
end

.build(provider:) ⇒ Object

Builds a backend when the OpenTelemetry API is loadable at a supported version; returns nil so resolution falls back to Null. – : (provider: untyped) -> Riffer::Tracing::Otel?



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/riffer/tracing/otel.rb', line 58

def build(provider:)
  version = api_version
  return nil unless version

  unless supported?(version)
    Kernel.warn "riffer: opentelemetry-api #{version} is outside the supported range (#{SUPPORTED_API_VERSIONS}); tracing is disabled"
    return nil
  end

  new(provider: provider || ::OpenTelemetry.tracer_provider)
end

.supported?(version) ⇒ Boolean

Whether the given opentelemetry-api version is one riffer codes against. The gem is undeclared, so this guard is the only protection against an incompatible API. – : (Gem::Version) -> bool

Returns:

  • (Boolean)


83
84
85
# File 'lib/riffer/tracing/otel.rb', line 83

def supported?(version)
  SUPPORTED_API_VERSIONS.satisfied_by?(version)
end

Instance Method Details

#current_contextObject

Returns the active OTEL context. – : () -> untyped



120
121
122
# File 'lib/riffer/tracing/otel.rb', line 120

def current_context
  ::OpenTelemetry::Context.current
end

#in_span(name, attributes:, kind:) ⇒ Object

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



111
112
113
114
115
# File 'lib/riffer/tracing/otel.rb', line 111

def in_span(name, attributes:, kind:)
  @tracer.in_span(name, attributes: attributes, kind: kind) do |otel_span, _context|
    yield Span.new(otel_span)
  end
end

#with_context(context) ⇒ Object

Runs the block with the given OTEL context active. – : [R] (untyped) { () -> R } -> R



127
128
129
130
# File 'lib/riffer/tracing/otel.rb', line 127

def with_context(context)
  return yield if context.nil?
  ::OpenTelemetry::Context.with_current(context) { yield }
end