class Prometheus::Client::LabelSetValidator
LabelSetValidator
ensures that all used label sets comply with the Prometheus
specification.
Constants
- RESERVED_LABELS
TODO: we might allow setting :instance in the future
Public Class Methods
new(reserved_labels = [])
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 16 def initialize(reserved_labels = []) @reserved_labels = (reserved_labels + RESERVED_LABELS).freeze @validated = {} end
Public Instance Methods
valid?(labels)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 21 def valid?(labels) unless labels.is_a?(Hash) raise InvalidLabelSetError, "#{labels} is not a valid label set" end labels.all? do |key, value| validate_symbol(key) validate_name(key) validate_reserved_key(key) validate_value(key, value) end end
validate(labels)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 34 def validate(labels) return labels if @validated.key?(labels.hash) valid?(labels) unless @validated.empty? || match?(labels, @validated.first.last) raise InvalidLabelSetError, 'labels must have the same signature' end @validated[labels.hash] = labels end
Private Instance Methods
match?(a, b)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 48 def match?(a, b) a.keys.sort == b.keys.sort end
validate_name(key)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 58 def validate_name(key) return true unless key.to_s.start_with?('__') raise ReservedLabelError, "label #{key} must not start with __" end
validate_reserved_key(key)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 64 def validate_reserved_key(key) return true unless @reserved_labels.include?(key) raise ReservedLabelError, "#{key} is reserved" end
validate_symbol(key)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 52 def validate_symbol(key) return true if key.is_a?(Symbol) raise InvalidLabelError, "label #{key} is not a symbol" end
validate_value(key, value)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 70 def validate_value(key, value) return true if value.is_a?(String) || value.is_a?(Numeric) || value.is_a?(Symbol) || value.is_a?(FalseClass) || value.is_a?(TrueClass) || value.nil? raise InvalidLabelError, "#{key} does not contain a valid value (type #{value.class})" end