Class: Riffer::Metrics::Otel

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

Overview

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

Constant Summary collapse

SUPPORTED_API_VERSIONS =

:nodoc: all

Gem::Requirement.new(">= 0.2", "< 1.0")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:) ⇒ Otel

– : (provider: untyped) -> void



64
65
66
67
68
# File 'lib/riffer/metrics/otel.rb', line 64

def initialize(provider:)
  @meter = provider.meter("riffer", version: Riffer::VERSION)
  @instruments = {}
  @mutex = Mutex.new
end

Class Method Details

.available?Boolean

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

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/riffer/metrics/otel.rb', line 31

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

.build(provider:) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/riffer/metrics/otel.rb', line 15

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

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

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

.supported?(version) ⇒ Boolean

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/riffer/metrics/otel.rb', line 41

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

Instance Method Details

#record_histogram(name, value, unit:, description:, attributes:) ⇒ Object

Records a value onto the named histogram. – : (String, Numeric, unit: String?, description: String?, attributes: Hash[String, untyped]?) -> void



73
74
75
76
77
78
# File 'lib/riffer/metrics/otel.rb', line 73

def record_histogram(name, value, unit:, description:, attributes:)
  histogram = @mutex.synchronize do
    @instruments[name] ||= @meter.create_histogram(name, unit: unit, description: description)
  end
  histogram.record(value, attributes: attributes)
end