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



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

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)


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

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

.build(provider: nil) ⇒ Object

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



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

def build(provider: nil)
  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)


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

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

Instance Method Details

#current_contextObject

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



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

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



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

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



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

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