Skip to content

Commit

Permalink
Allow non-string Resource label values. (#180)
Browse files Browse the repository at this point in the history
Fixes #158.

Co-authored-by: Matthew Wear <[email protected]>
  • Loading branch information
fhwang and mwear authored Feb 9, 2020
1 parent c95f7cd commit 5dd0c82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions sdk/lib/opentelemetry/sdk/resources/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ class << self

# Returns a newly created {Resource} with the specified labels
#
# @param [Hash<String, String>] labels Hash of key-value pairs to be used
# @param [Hash{String => String, Numeric, Boolean} labels Hash of key-value pairs to be used
# as labels for this resource
# @raise [ArgumentError] If label keys and values are not strings
# @return [Resource]
def create(labels = {})
frozen_labels = labels.each_with_object({}) do |(k, v), memo|
raise ArgumentError, 'label keys and values must be strings' unless k.is_a?(String) && v.is_a?(String)
raise ArgumentError, 'label keys must be strings' unless k.is_a?(String)
raise ArgumentError, 'label values must be strings, integers, floats, or booleans' unless Internal.valid_value?(v)

memo[-k] = -v
memo[-k] = v.freeze
end.freeze

new(frozen_labels)
Expand Down
7 changes: 6 additions & 1 deletion sdk/test/opentelemetry/sdk/resources/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
_(proc { Resource.create(k1: 'v1') }).must_raise(ArgumentError)
end

it 'enforces values are strings' do
it 'enforces values are strings, ints, floats, or booleans' do
_(proc { Resource.create('k1' => :v1) }).must_raise(ArgumentError)
values = ['v1', 123, 456.78, false, true]
values.each do |value|
resource = Resource.create('k1' => value)
_(resource.label_enumerator.first.last).must_equal(value)
end
end
end

Expand Down

0 comments on commit 5dd0c82

Please sign in to comment.