Class: Riffer::Tracing::Otel

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/tracing/otel.rb,
sig/generated/riffer/tracing/otel.rbs

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

Returns:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:) ⇒ Otel

-- : (provider: untyped) -> void

Parameters:

  • provider: (Object)


108
109
110
# File 'lib/riffer/tracing/otel.rb', line 108

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)


75
76
77
78
79
80
# File 'lib/riffer/tracing/otel.rb', line 75

def available?
  version = api_version
  return false unless version

  supported?(version)
end

.build(provider: nil) ⇒ Riffer::Tracing::Otel?

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?

Parameters:

  • provider: (Object) (defaults to: nil)

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
# 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

Parameters:

  • (Gem::Version)

Returns:

  • (Boolean)


87
88
89
# File 'lib/riffer/tracing/otel.rb', line 87

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

Instance Method Details

#current_contextObject

Returns the active OTEL context.

: () -> untyped

Returns:

  • (Object)


124
125
126
# File 'lib/riffer/tracing/otel.rb', line 124

def current_context
  ::OpenTelemetry::Context.current
end

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

This method returns an undefined value.

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



115
116
117
118
119
# File 'lib/riffer/tracing/otel.rb', line 115

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) ⇒ void

This method returns an undefined value.

Runs the block with the given OTEL context active.

: [R] (untyped) { () -> R } -> R



131
132
133
134
135
# File 'lib/riffer/tracing/otel.rb', line 131

def with_context(context, &)
  return yield if context.nil?

  ::OpenTelemetry::Context.with_current(context, &)
end