diff --git a/lib/prometheus/client/label_set_validator.rb b/lib/prometheus/client/label_set_validator.rb index f3625233..e67dde54 100644 --- a/lib/prometheus/client/label_set_validator.rb +++ b/lib/prometheus/client/label_set_validator.rb @@ -5,8 +5,7 @@ module Client # LabelSetValidator ensures that all used label sets comply with the # Prometheus specification. class LabelSetValidator - # TODO: we might allow setting :instance in the future - BASE_RESERVED_LABELS = [:job, :instance, :pid].freeze + BASE_RESERVED_LABELS = [:pid].freeze LABEL_NAME_REGEX = /\A[a-zA-Z_][a-zA-Z0-9_]*\Z/ class LabelSetError < StandardError; end diff --git a/spec/prometheus/client/label_set_validator_spec.rb b/spec/prometheus/client/label_set_validator_spec.rb index f4d38987..ffd1387c 100644 --- a/spec/prometheus/client/label_set_validator_spec.rb +++ b/spec/prometheus/client/label_set_validator_spec.rb @@ -4,7 +4,10 @@ describe Prometheus::Client::LabelSetValidator do let(:expected_labels) { [] } - let(:validator) { Prometheus::Client::LabelSetValidator.new(expected_labels: expected_labels) } + let(:additional_reserved_labels) { [] } + let(:validator) do + Prometheus::Client::LabelSetValidator.new(expected_labels: expected_labels, reserved_labels: additional_reserved_labels) + end let(:invalid) { Prometheus::Client::LabelSetValidator::InvalidLabelSetError } describe '.new' do @@ -42,11 +45,27 @@ end.to raise_exception(described_class::InvalidLabelError) end - it 'raises ReservedLabelError if a label key is reserved' do - [:job, :instance, :pid].each do |label| - expect do - validator.validate_symbols!(label => 'value') - end.to raise_exception(described_class::ReservedLabelError) + context "with only the base set of reserved labels" do + it "doesn't raise ReservedLabelError for the additional reserved label" do + expect { validator.validate_symbols!(additional: 'value') }. + to_not raise_exception + end + + it 'raises ReservedLabelError if a label key is reserved' do + expect { validator.validate_symbols!(pid: 'value') }. + to raise_exception(described_class::ReservedLabelError) + end + end + + context "with an additional reserved label" do + let(:additional_reserved_labels) { [:additional] } + + it 'raises ReservedLabelError if a label key is reserved' do + [:additional, :pid].each do |label| + expect do + validator.validate_symbols!(label => 'value') + end.to raise_exception(described_class::ReservedLabelError) + end end end end