Class: Prometheus::Client::LabelSetValidator

Inherits:
Object
  • Object
show all
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

BASE_RESERVED_LABELS =

TODO: we might allow setting :instance in the future

[:job, :instance, :pid].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_labels:, reserved_labels: []) ⇒ LabelSetValidator

Returns a new instance of LabelSetValidator.


18
19
20
21
# File 'lib/prometheus/client/label_set_validator.rb', line 18

def initialize(expected_labels:, reserved_labels: [])
  @expected_labels = expected_labels.sort
  @reserved_labels = BASE_RESERVED_LABELS + reserved_labels
end

Instance Attribute Details

#expected_labelsObject (readonly)

Returns the value of attribute expected_labels


16
17
18
# File 'lib/prometheus/client/label_set_validator.rb', line 16

def expected_labels
  @expected_labels
end

#reserved_labelsObject (readonly)

Returns the value of attribute reserved_labels


16
17
18
# File 'lib/prometheus/client/label_set_validator.rb', line 16

def reserved_labels
  @reserved_labels
end

Instance Method Details

#validate_labelset!(labelset) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/prometheus/client/label_set_validator.rb', line 35

def validate_labelset!(labelset)
  begin
    return labelset if keys_match?(labelset)
  rescue ArgumentError
    # If labelset contains keys that are a mixture of strings and symbols, this will
    # raise when trying to sort them, but the error should be the same:
    # InvalidLabelSetError
  end

  raise InvalidLabelSetError, "labels must have the same signature " \
                              "(keys given: #{labelset.keys} vs." \
                              " keys expected: #{expected_labels}"
end

#validate_symbols!(labels) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prometheus/client/label_set_validator.rb', line 23

def validate_symbols!(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