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 15
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 20
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 32
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 46
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 56
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 62
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 50
def validate_symbol(key)
  return true if key.is_a?(Symbol)

  raise InvalidLabelError, "label #{key} is not a symbol"
end