Skip to content

Commit 0fb0a9f

Browse files
committed
align valid_value semantics with AnyValue spec
According to https://opentelemetry.io/docs/specs/otel/common/#anyvalue (Introduced in v1.51.0 of the OTel Spec: https://github.com/open-telemetry/opentelemetry-specification/tree/v1.51.0/specification/common) an attribute value is defined as `AnyValue` with AnyValue being allowed to be a simple value as well as an Array of AnyValue (which includes nested arrays and mixed array), as well as map<string, AnyValue>. Empty values are allowed as well.
1 parent 30fb4fc commit 0fb0a9f

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

sdk/lib/opentelemetry/sdk/internal.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,24 @@ def numeric?(value)
2525
end
2626

2727
def valid_simple_value?(value)
28-
value.instance_of?(String) || boolean?(value) || numeric?(value)
28+
value.instance_of?(String) || boolean?(value) || numeric?(value) || value.nil?
2929
end
3030

3131
def valid_array_value?(value)
3232
return false unless value.is_a?(Array)
33-
return true if value.empty?
3433

35-
case value.first
36-
when String
37-
value.all? { |v| v.instance_of?(String) }
38-
when TrueClass, FalseClass
39-
value.all? { |v| boolean?(v) }
40-
when Numeric
41-
value.all? { |v| numeric?(v) }
42-
else
43-
false
44-
end
34+
value.all? { |v| valid_value?(v) }
35+
end
36+
37+
def valid_mapping_value?(value)
38+
return false unless value.is_a?(Hash)
39+
return false unless value.keys.all? { |k| valid_key?(k) }
40+
41+
value.values.all? { |v| valid_value?(v) }
4542
end
4643

4744
def valid_value?(value)
48-
valid_simple_value?(value) || valid_array_value?(value)
45+
valid_simple_value?(value) || valid_array_value?(value) || valid_mapping_value?(value)
4946
end
5047

5148
def valid_attributes?(owner, kind, attrs)

sdk/test/opentelemetry/sdk/trace/span_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@
114114
end
115115
end
116116

117-
it 'reports an error for a NilClass value, which is invalid' do
117+
it 'does not report an error for a NilClass value, as empty values are valid' do
118118
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
119119
span.set_attribute('foo', nil)
120120
span.finish
121-
_(log_stream.string).must_match(/invalid span attribute value type NilClass for key 'foo' on span 'name'/)
121+
_(log_stream.string.length).must_equal(0)
122122
end
123123
end
124124

@@ -223,12 +223,12 @@
223223
_(events.first.attributes).must_equal(attrs)
224224
end
225225

226-
it 'does not keep nil-valued attributes' do
226+
it 'keeps nil-valued attributes' do
227227
attrs = { 'foo' => nil }
228228
span.add_event('added', attributes: attrs)
229229
events = span.events
230230
_(events.size).must_equal(1)
231-
_(events.first.attributes).must_equal({})
231+
_(events.first.attributes).must_equal({ 'foo' => nil })
232232
end
233233

234234
it 'accepts array-valued attributes' do
@@ -247,12 +247,12 @@
247247
_(events.first.attributes).must_equal({})
248248
end
249249

250-
it 'does not accept array-valued attributes if the elements are different types' do
250+
it 'accepts array-valued attributes even if the elements are different types' do
251251
attrs = { 'foo' => [1, 2, 'bar'] }
252252
span.add_event('added', attributes: attrs)
253253
events = span.events
254254
_(events.size).must_equal(1)
255-
_(events.first.attributes).must_equal({})
255+
_(events.first.attributes).must_equal({ 'foo' => [1, 2, 'bar'] })
256256
end
257257

258258
it 'accepts array-valued attributes if the elements are true and false' do

0 commit comments

Comments
 (0)