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) ⇒ void
Initialize OpenTelemetry in the Valkey GLIDE core.
-
.initialized? ⇒ Boolean
Check if OpenTelemetry has been initialized.
-
.reset! ⇒ Object
private
Reset initialization state (for testing only).
-
.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.
136 137 138 |
# File 'lib/valkey/opentelemetry.rb', line 136 def config @config end |
Class Method Details
.init(traces: nil, metrics: nil, flush_interval_ms: 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.
75 76 77 78 79 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 |
# File 'lib/valkey/opentelemetry.rb', line 75 def init(traces: nil, metrics: nil, flush_interval_ms: 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 } 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.
118 119 120 |
# File 'lib/valkey/opentelemetry.rb', line 118 def initialized? @initialized 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).
141 142 143 144 |
# File 'lib/valkey/opentelemetry.rb', line 141 def reset! @initialized = false @config = nil end |
.should_sample? ⇒ Boolean
Determine if the current request should be sampled based on the configured sample percentage.
125 126 127 128 129 130 131 |
# File 'lib/valkey/opentelemetry.rb', line 125 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 |