From 5dd0c8230ac3ca2b16a9784a3491b004f9bb6a92 Mon Sep 17 00:00:00 2001 From: Francis Hwang Date: Sun, 9 Feb 2020 02:35:05 -0500 Subject: [PATCH] Allow non-string Resource label values. (#180) Fixes #158. Co-authored-by: Matthew Wear --- sdk/lib/opentelemetry/sdk/resources/resource.rb | 7 ++++--- sdk/test/opentelemetry/sdk/resources/resource_test.rb | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sdk/lib/opentelemetry/sdk/resources/resource.rb b/sdk/lib/opentelemetry/sdk/resources/resource.rb index 32e40cfba..ebaf32e37 100644 --- a/sdk/lib/opentelemetry/sdk/resources/resource.rb +++ b/sdk/lib/opentelemetry/sdk/resources/resource.rb @@ -15,15 +15,16 @@ class << self # Returns a newly created {Resource} with the specified labels # - # @param [Hash] 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) diff --git a/sdk/test/opentelemetry/sdk/resources/resource_test.rb b/sdk/test/opentelemetry/sdk/resources/resource_test.rb index 0145a72e8..4896f02c1 100644 --- a/sdk/test/opentelemetry/sdk/resources/resource_test.rb +++ b/sdk/test/opentelemetry/sdk/resources/resource_test.rb @@ -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