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.

Examples:

Initialize with HTTP collector

Valkey::OpenTelemetry.init(
  traces: {
    endpoint: "http://localhost:4318/v1/traces",
    sample_percentage: 10
  },
  metrics: {
    endpoint: "http://localhost:4318/v1/metrics"
  },
  flush_interval_ms: 5000
)

Initialize with gRPC collector

Valkey::OpenTelemetry.init(
  traces: {
    endpoint: "grpc://localhost:4317",
    sample_percentage: 1
  },
  metrics: {
    endpoint: "grpc://localhost:4317"
  }
)

Initialize with file exporter (for testing)

Valkey::OpenTelemetry.init(
  traces: {
    endpoint: "file:///tmp/valkey_traces.json",
    sample_percentage: 100
  },
  metrics: {
    endpoint: "file:///tmp/valkey_metrics.json"
  }
)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configHash? (readonly)

Get the current OpenTelemetry configuration.

Returns:

  • (Hash, nil)

    the configuration hash or nil if not initialized



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.

Parameters:

  • traces (Hash, nil) (defaults to: nil)

    Traces configuration

  • metrics (Hash, nil) (defaults to: nil)

    Metrics configuration

  • flush_interval_ms (Integer, nil) (defaults to: nil)

    Flush interval in milliseconds (default: 5000) Must be a positive integer

Options Hash (traces:):

  • :endpoint (String)

    The endpoint URL (required) Supported formats:

  • :sample_percentage (Integer)

    Sample percentage 0-100 (default: 1) Keep low (1-5%) in production for performance

Options Hash (metrics:):

  • :endpoint (String)

    The endpoint URL (required) Same format as traces endpoint

Raises:

  • (ArgumentError)

    if neither traces nor metrics is provided

  • (ArgumentError)

    if sample_percentage is not between 0-100

  • (RuntimeError)

    if initialization fails



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.

Returns:

  • (Boolean)

    true if 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.

Returns:

  • (Boolean)

    true if the request should be sampled



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