Skip to content

Commit db756a1

Browse files
authored
Merge pull request #277 from prometheus/sinjo-loosen-reserved-labels
Allow use of `instance` and `job` labels
2 parents 825b35c + e53f7ed commit db756a1

3 files changed

Lines changed: 42 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
# Unreleased changes
44

5-
_None outstanding_
5+
## Small improvements
6+
7+
- [#277](https://github.com/prometheus/client_ruby/pull/277) Allow use of `instance` and
8+
`job` labels:
9+
It's now possible to set the `instance` and `job` labels on metrics, where previously
10+
they had been reserved.
11+
12+
The reason we'd reserved them is that Prometheus automatically generates values
13+
for them when it scrapes a target, and we didn't want to cause a collision. It
14+
turns out Prometheus handles that collision just fine.
15+
16+
By default, Prometheus server will prepend `exported_` to them if they're present
17+
in the scraped data (i.e. `exported_instance` and `exported_job`). Users can set
18+
`honor_labels` in their Prometheus server config if they prefer the labels from
19+
the scraped metric data to take precedence over the labels generated by the
20+
server.
621

722
# 4.0.0 / 2022-03-27
823

lib/prometheus/client/label_set_validator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module Client
55
# LabelSetValidator ensures that all used label sets comply with the
66
# Prometheus specification.
77
class LabelSetValidator
8-
# TODO: we might allow setting :instance in the future
9-
BASE_RESERVED_LABELS = [:job, :instance, :pid].freeze
8+
BASE_RESERVED_LABELS = [:pid].freeze
109
LABEL_NAME_REGEX = /\A[a-zA-Z_][a-zA-Z0-9_]*\Z/
1110

1211
class LabelSetError < StandardError; end

spec/prometheus/client/label_set_validator_spec.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
describe Prometheus::Client::LabelSetValidator do
66
let(:expected_labels) { [] }
7-
let(:validator) { Prometheus::Client::LabelSetValidator.new(expected_labels: expected_labels) }
7+
let(:additional_reserved_labels) { [] }
8+
let(:validator) do
9+
Prometheus::Client::LabelSetValidator.new(expected_labels: expected_labels, reserved_labels: additional_reserved_labels)
10+
end
811
let(:invalid) { Prometheus::Client::LabelSetValidator::InvalidLabelSetError }
912

1013
describe '.new' do
@@ -42,11 +45,27 @@
4245
end.to raise_exception(described_class::InvalidLabelError)
4346
end
4447

45-
it 'raises ReservedLabelError if a label key is reserved' do
46-
[:job, :instance, :pid].each do |label|
47-
expect do
48-
validator.validate_symbols!(label => 'value')
49-
end.to raise_exception(described_class::ReservedLabelError)
48+
context "with only the base set of reserved labels" do
49+
it "doesn't raise ReservedLabelError for the additional reserved label" do
50+
expect { validator.validate_symbols!(additional: 'value') }.
51+
to_not raise_exception
52+
end
53+
54+
it 'raises ReservedLabelError if a label key is reserved' do
55+
expect { validator.validate_symbols!(pid: 'value') }.
56+
to raise_exception(described_class::ReservedLabelError)
57+
end
58+
end
59+
60+
context "with an additional reserved label" do
61+
let(:additional_reserved_labels) { [:additional] }
62+
63+
it 'raises ReservedLabelError if a label key is reserved' do
64+
[:additional, :pid].each do |label|
65+
expect do
66+
validator.validate_symbols!(label => 'value')
67+
end.to raise_exception(described_class::ReservedLabelError)
68+
end
5069
end
5170
end
5271
end

0 commit comments

Comments
 (0)