Module: Valkey::OpenTelemetry
- Defined in:
- lib/valkey/opentelemetry.rb
Overview
OpenTelemetry integration for Valkey GLIDE Ruby client.
This module provides integration with the OpenTelemetry implementation built into the Valkey GLIDE core (Rust FFI layer). Unlike typical Ruby OpenTelemetry instrumentation, this directly configures the native OpenTelemetry exporter in the Rust layer.
Class Attribute Summary collapse
-
.config ⇒ Hash?
readonly
Get the current OpenTelemetry configuration.
Class Method Summary collapse
-
.init(traces: nil, metrics: nil, flush_interval_ms: nil, parent_span_context_provider: nil) ⇒ void
Initialize OpenTelemetry in the Valkey GLIDE core.
-
.initialized? ⇒ Boolean
Check if OpenTelemetry has been initialized.
-
.parent_span_context ⇒ Hash?
Invoke the registered parent-span-context provider (if any) and return a validated context Hash, or nil if no provider is registered, the provider returned nil, the provider raised, or the returned context failed validation.
-
.reset! ⇒ Object
private
Reset initialization state (for testing only).
-
.set_parent_span_context_provider(callable = nil, &block) ⇒ void
Register a callable that returns the current application span context, so that spans created for Valkey commands become children of it instead of independent root spans.
-
.should_sample? ⇒ Boolean
Determine if the current request should be sampled based on the configured sample percentage.
Class Attribute Details
.config ⇒ Hash? (readonly)
Get the current OpenTelemetry configuration.
142 143 144 |
# File 'lib/valkey/opentelemetry.rb', line 142 def config @config end |
Class Method Details
.init(traces: nil, metrics: nil, flush_interval_ms: nil, parent_span_context_provider: nil) ⇒ void
This method returns an undefined value.
Initialize OpenTelemetry in the Valkey GLIDE core.
This method can only be called once per process. Subsequent calls will be ignored with a warning.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/valkey/opentelemetry.rb', line 80 def init(traces: nil, metrics: nil, flush_interval_ms: nil, parent_span_context_provider: nil) if @initialized warn "Valkey::OpenTelemetry already initialized - ignoring new configuration" return end # Validate input raise ArgumentError, "At least one of traces or metrics must be provided" if traces.nil? && metrics.nil? if traces && traces[:sample_percentage] sample = traces[:sample_percentage] unless sample.is_a?(Integer) && sample >= 0 && sample <= 100 raise ArgumentError, "sample_percentage must be an integer between 0 and 100, got: #{sample}" end end if flush_interval_ms && (!flush_interval_ms.is_a?(Integer) || flush_interval_ms <= 0) raise ArgumentError, "flush_interval_ms must be a positive integer, got: #{flush_interval_ms}" end # Build the configuration config = build_config(traces, metrics, flush_interval_ms) # Call the FFI function error_ptr = Bindings.init_open_telemetry(config) unless error_ptr.null? error_msg = error_ptr.read_string Bindings.free_c_string(error_ptr) raise "Failed to initialize OpenTelemetry: #{error_msg}" end @initialized = true @config = { traces: traces, metrics: metrics, flush_interval_ms: flush_interval_ms } set_parent_span_context_provider(parent_span_context_provider) if parent_span_context_provider puts "✅ Valkey OpenTelemetry initialized successfully" puts " Traces: #{traces ? traces[:endpoint] : 'disabled'}" puts " Metrics: #{metrics ? metrics[:endpoint] : 'disabled'}" end |
.initialized? ⇒ Boolean
Check if OpenTelemetry has been initialized.
124 125 126 |
# File 'lib/valkey/opentelemetry.rb', line 124 def initialized? @initialized end |
.parent_span_context ⇒ Hash?
Invoke the registered parent-span-context provider (if any) and return a validated context Hash, or nil if no provider is registered, the provider returned nil, the provider raised, or the returned context failed validation.
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/valkey/opentelemetry.rb', line 180 def parent_span_context return nil unless @parent_span_context_provider ctx = @parent_span_context_provider.call return nil if ctx.nil? validate_parent_span_context!(ctx) ctx rescue StandardError => e warn "Valkey::OpenTelemetry parent_span_context_provider failed: #{e.}" nil end |
.reset! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reset initialization state (for testing only).
196 197 198 199 200 |
# File 'lib/valkey/opentelemetry.rb', line 196 def reset! @initialized = false @config = nil @parent_span_context_provider = nil end |
.set_parent_span_context_provider(callable = nil, &block) ⇒ void
This method returns an undefined value.
Register a callable that returns the current application span context, so that spans created for Valkey commands become children of it instead of independent root spans. This is how distributed tracing context (e.g. from the Rails request span) is propagated into the native OpenTelemetry spans created for each command.
171 172 173 |
# File 'lib/valkey/opentelemetry.rb', line 171 def set_parent_span_context_provider(callable = nil, &block) @parent_span_context_provider = block || callable end |
.should_sample? ⇒ Boolean
Determine if the current request should be sampled based on the configured sample percentage.
131 132 133 134 135 136 137 |
# File 'lib/valkey/opentelemetry.rb', line 131 def should_sample? return false unless @initialized return false unless @config&.dig(:traces) sample_percentage = @config.dig(:traces, :sample_percentage) || 1 rand(100) < sample_percentage end |