Skip to content

Commit

Permalink
Merge 7431d45 into 607a43a
Browse files Browse the repository at this point in the history
  • Loading branch information
dmagliola committed Jul 17, 2019
2 parents 607a43a + 7431d45 commit fde2980
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
19 changes: 12 additions & 7 deletions lib/prometheus/client/label_set_validator.rb
Expand Up @@ -36,15 +36,20 @@ def validate_symbols!(labels)
def validate_labelset!(labelset)
return labelset if @validated.key?(labelset.hash)

validate_symbols!(labelset)

unless keys_match?(labelset)
raise InvalidLabelSetError, "labels must have the same signature " \
"(keys given: #{labelset.keys.sort} vs." \
" keys expected: #{expected_labels}"
begin
if keys_match?(labelset)
@validated[labelset.hash] = true
return labelset
end
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

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

private
Expand Down
2 changes: 1 addition & 1 deletion spec/prometheus/client/histogram_spec.rb
Expand Up @@ -47,7 +47,7 @@
it 'raise error for le labels' do
expect do
histogram.observe(5, labels: { le: 1 })
end.to raise_error Prometheus::Client::LabelSetValidator::ReservedLabelError
end.to raise_error Prometheus::Client::LabelSetValidator::InvalidLabelSetError
end

it 'raises an InvalidLabelSetError if sending unexpected labels' do
Expand Down
11 changes: 5 additions & 6 deletions spec/prometheus/client/label_set_validator_spec.rb
Expand Up @@ -54,17 +54,16 @@
expect(validator.validate_labelset!(hash)).to eql(hash)
end

it 'raises an exception if a given label set is not `validate_symbols!`' do
input = 'broken'
expect(validator).to receive(:validate_symbols!).with(input).and_raise(invalid)

expect { validator.validate_labelset!(input) }.to raise_exception(invalid)
it 'returns an exception if there are malformed labels' do
expect do
validator.validate_labelset!('method' => 'get', :code => '200')
end.to raise_exception(invalid, /keys given: \["method", :code\] vs. keys expected: \[:code, :method\]/)
end

it 'raises an exception if there are unexpected labels' do
expect do
validator.validate_labelset!(method: 'get', code: '200', exception: 'NoMethodError')
end.to raise_exception(invalid, /keys given: \[:code, :exception, :method\] vs. keys expected: \[:code, :method\]/)
end.to raise_exception(invalid, /keys given: \[:method, :code, :exception\] vs. keys expected: \[:code, :method\]/)
end

it 'raises an exception if there are missing labels' do
Expand Down
2 changes: 1 addition & 1 deletion spec/prometheus/client/summary_spec.rb
Expand Up @@ -42,7 +42,7 @@
it 'raise error for quantile labels' do
expect do
summary.observe(5, labels: { quantile: 1 })
end.to raise_error Prometheus::Client::LabelSetValidator::ReservedLabelError
end.to raise_error Prometheus::Client::LabelSetValidator::InvalidLabelSetError
end

it 'raises an InvalidLabelSetError if sending unexpected labels' do
Expand Down

0 comments on commit fde2980

Please sign in to comment.