Class: Prometheus::Client::LabelSetValidator
- Inherits:
-
Object
- Object
- Prometheus::Client::LabelSetValidator
- Defined in:
- lib/prometheus/client/label_set_validator.rb
Overview
LabelSetValidator ensures that all used label sets comply with the Prometheus specification.
Defined Under Namespace
Classes: InvalidLabelError, InvalidLabelSetError, LabelSetError, ReservedLabelError
Constant Summary collapse
- RESERVED_LABELS =
[].freeze
Instance Method Summary collapse
-
#initialize(reserved_labels = []) ⇒ LabelSetValidator
constructor
A new instance of LabelSetValidator.
- #valid?(labels) ⇒ Boolean
- #validate(labels) ⇒ Object
Constructor Details
#initialize(reserved_labels = []) ⇒ LabelSetValidator
Returns a new instance of LabelSetValidator.
15 16 17 18 |
# File 'lib/prometheus/client/label_set_validator.rb', line 15 def initialize(reserved_labels = []) @reserved_labels = (reserved_labels + RESERVED_LABELS).freeze @validated = {} end |
Instance Method Details
#valid?(labels) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/prometheus/client/label_set_validator.rb', line 20 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) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/prometheus/client/label_set_validator.rb', line 33 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: (#{label_diff(labels, @validated.first.last)})" end @validated[labels.hash] = labels end |