Skip to content

Commit

Permalink
Merge branch 'main' into purge-spans-on-fork
Browse files Browse the repository at this point in the history
  • Loading branch information
fbogsany authored Oct 31, 2023
2 parents e264720 + 0ccb683 commit f78a08d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
10 changes: 8 additions & 2 deletions sdk/lib/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,14 @@ def validated_attributes(attrs)
def trim_span_attributes(attrs)
return if attrs.nil?

excess = attrs.size - @span_limits.attribute_count_limit
excess.times { attrs.shift } if excess.positive?
if attrs.size > @span_limits.attribute_count_limit
n = @span_limits.attribute_count_limit
attrs.delete_if do |_key, _value|
n -= 1
n.negative?
end
end

truncate_attribute_values(attrs, @span_limits.attribute_length_limit)
nil
end
Expand Down
17 changes: 9 additions & 8 deletions sdk/test/opentelemetry/sdk/trace/span_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
_(span.attributes).must_equal('foo' => 'bar')
end

it 'trims the oldest attribute' do
it 'trims the newest attribute' do
span.set_attribute('old', 'oldbar')
span.set_attribute('foo', 'bar')
_(span.attributes).must_equal('foo' => 'bar')
_(span.attributes).must_equal('old' => 'oldbar')
end

it 'truncates attribute value length based if configured' do
Expand Down Expand Up @@ -138,10 +138,10 @@
_(span.attributes).must_equal('foo' => 'bar')
end

it 'trims the oldest attributes' do
it 'trims the newest attributes' do
span.add_attributes('old' => 'oldbar')
span.add_attributes('foo' => 'bar', 'bar' => 'baz')
_(span.attributes).must_equal('bar' => 'baz')
_(span.attributes).must_equal('old' => 'oldbar')
end

it 'truncates attribute value length based if configured' do
Expand Down Expand Up @@ -499,22 +499,23 @@
end

it 'trims excess attributes' do
attributes = { 'foo': 'bar', 'other': 'attr' }
attributes = { 'foo' => 'bar', 'other' => 'attr' }
span = Span.new(context, Context.empty, OpenTelemetry::Trace::Span::INVALID, 'name', SpanKind::INTERNAL, nil, span_limits,
[], attributes, nil, Time.now, nil, nil)
_(span.to_span_data.total_recorded_attributes).must_equal(2)
_(span.attributes.length).must_equal(1)
_(span.attributes).must_equal('foo' => 'bar')
end

it 'truncates attributes if configured' do
attributes = { 'foo': 'oldbaroldbaroldbaroldbaroldbaroldbar' }
attributes = { 'foo' => 'oldbaroldbaroldbaroldbaroldbaroldbar' }
span = Span.new(context, Context.empty, OpenTelemetry::Trace::Span::INVALID, 'name', SpanKind::INTERNAL, nil, span_limits,
[], attributes, nil, Time.now, nil, nil)
_(span.attributes[:foo]).must_equal('oldbaroldbaroldbaroldbaroldba...')
_(span.attributes['foo']).must_equal('oldbaroldbaroldbaroldbaroldba...')
end

it 'counts attributes' do
attributes = { 'foo': 'bar', 'other': 'attr' }
attributes = { 'foo' => 'bar', 'other' => 'attr' }
span = Span.new(context, Context.empty, OpenTelemetry::Trace::Span::INVALID, 'name', SpanKind::INTERNAL, nil, span_limits,
[], attributes, nil, Time.now, nil, nil)
_(span.to_span_data.total_recorded_attributes).must_equal(2)
Expand Down

0 comments on commit f78a08d

Please sign in to comment.