class Prometheus::Client::MmapedValue
A float protected by a mutex backed by a per-process mmaped file.
Constants
- VALUE_LOCK
Public Class Methods
multiprocess()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 95 def self.multiprocess true end
new(type, metric_name, name, labels, multiprocess_mode = '')
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 14 def initialize(type, metric_name, name, labels, multiprocess_mode = '') @file_prefix = type.to_s @metric_name = metric_name @name = name @labels = labels if type == :gauge @file_prefix += '_' + multiprocess_mode.to_s end @pid = -1 @mutex = Mutex.new initialize_file end
pid_changed?()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 91 def self.pid_changed? @@pid != Process.pid end
reinitialize_on_pid_change()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 83 def self.reinitialize_on_pid_change VALUE_LOCK.synchronize do reset_on_pid_change ObjectSpace.each_object(MmapedValue, &:unsafe_reinitialize_file) end end
reset_and_reinitialize()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 65 def self.reset_and_reinitialize VALUE_LOCK.synchronize do @@pid = Process.pid @@files = {} ObjectSpace.each_object(MmapedValue).each do |v| v.unsafe_reinitialize_file(false) end end end
reset_on_pid_change()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 76 def self.reset_on_pid_change if pid_changed? @@pid = Process.pid @@files = {} end end
Public Instance Methods
get()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 49 def get @mutex.synchronize do initialize_file if pid_changed? return @value end end
increment(amount = 1)
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 29 def increment(amount = 1) @mutex.synchronize do initialize_file if pid_changed? @value += amount write_value(@key, @value) @value end end
pid_changed?()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 56 def pid_changed? @pid != Process.pid end
set(value)
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 39 def set(value) @mutex.synchronize do initialize_file if pid_changed? @value = value write_value(@key, @value) @value end end
unsafe_reinitialize_file(check_pid = true)
click to toggle source
method needs to be run in VALUE_LOCK mutex
# File lib/prometheus/client/mmaped_value.rb, line 61 def unsafe_reinitialize_file(check_pid = true) unsafe_initialize_file if !check_pid || pid_changed? end
Private Instance Methods
initialize_file()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 101 def initialize_file VALUE_LOCK.synchronize do unsafe_initialize_file end end
read_value(key)
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 145 def read_value(key) @file.read_value(key) rescue StandardError => e Prometheus::Client.logger.warn("reading value from #{@file.path} failed with #{e}") Prometheus::Client.logger.debug(e.backtrace.join("\n")) 0 end
rebuild_key()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 127 def rebuild_key labelnames = [] labelvalues = [] @labels.each do |k, v| labelnames << k labelvalues << v end [@metric_name, @name, labelnames, labelvalues].to_json end
unsafe_initialize_file()
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 107 def unsafe_initialize_file self.class.reset_on_pid_change @pid = Process.pid unless @@files.has_key?(@file_prefix) unless @file.nil? @file.close end mmaped_file = Helper::MmapedFile.open_exclusive_file(@file_prefix) @@files[@file_prefix] = MmapedDict.new(mmaped_file) end @file = @@files[@file_prefix] @key = rebuild_key @value = read_value(@key) end
write_value(key, val)
click to toggle source
# File lib/prometheus/client/mmaped_value.rb, line 138 def write_value(key, val) @file.write_value(key, val) rescue StandardError => e Prometheus::Client.logger.warn("writing value to #{@file.path} failed with #{e}") Prometheus::Client.logger.debug(e.backtrace.join("\n")) end