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()
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 15 def initialize @validated = {} end
Public Instance Methods
valid?(labels)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 19 def valid?(labels) unless labels.respond_to?(:all?) raise InvalidLabelSetError, "#{labels} is not a valid label set" end labels.all? do |key, _| validate_symbol(key) validate_name(key) validate_reserved_key(key) end end
validate(labels)
click to toggle source
# File lib/prometheus/client/label_set_validator.rb, line 31 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 45 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 55 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 61 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 49 def validate_symbol(key) return true if key.is_a?(Symbol) raise InvalidLabelError, "label #{key} is not a symbol" end