Skip to content

Commit b57d1ec

Browse files
authored
fix!: span limits env vars (#879)
1 parent 314f3b8 commit b57d1ec

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

sdk/lib/opentelemetry/sdk/trace/span.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def initialize(context, parent_context, name, kind, parent_span_id, span_limits,
296296
@attributes = attributes.nil? ? nil : Hash[attributes] # We need a mutable copy of attributes.
297297
trim_span_attributes(@attributes)
298298
@events = nil
299-
@links = trim_links(links, span_limits.link_count_limit, span_limits.attribute_per_link_count_limit)
299+
@links = trim_links(links, span_limits.link_count_limit, span_limits.link_attribute_count_limit)
300300
@span_processors.each { |processor| processor.on_start(self, parent_context) }
301301
end
302302

@@ -327,33 +327,33 @@ def truncate_attribute_values(attrs)
327327
attrs
328328
end
329329

330-
def trim_links(links, link_count_limit, attribute_per_link_count_limit) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
330+
def trim_links(links, link_count_limit, link_attribute_count_limit) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
331331
# Fast path (likely) common cases.
332332
return nil if links.nil?
333333

334334
if links.size <= link_count_limit &&
335-
links.all? { |link| link.attributes.size <= attribute_per_link_count_limit && Internal.valid_attributes?(name, 'link', link.attributes) }
335+
links.all? { |link| link.attributes.size <= link_attribute_count_limit && Internal.valid_attributes?(name, 'link', link.attributes) }
336336
return links.frozen? ? links : links.clone.freeze
337337
end
338338

339339
# Slow path: trim attributes for each Link.
340340
links.last(link_count_limit).map! do |link|
341341
attrs = Hash[link.attributes] # link.attributes is frozen, so we need an unfrozen copy to adjust.
342342
attrs.keep_if { |key, value| Internal.valid_key?(key) && Internal.valid_value?(value) }
343-
excess = attrs.size - attribute_per_link_count_limit
343+
excess = attrs.size - link_attribute_count_limit
344344
excess.times { attrs.shift } if excess.positive?
345345
OpenTelemetry::Trace::Link.new(link.span_context, attrs)
346346
end.freeze
347347
end
348348

349349
def append_event(events, event) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
350350
event_count_limit = @span_limits.event_count_limit
351-
attribute_per_event_count_limit = @span_limits.attribute_per_event_count_limit
351+
event_attribute_count_limit = @span_limits.event_attribute_count_limit
352352
valid_attributes = Internal.valid_attributes?(name, 'event', event.attributes)
353353

354354
# Fast path (likely) common case.
355355
if events.size < event_count_limit &&
356-
event.attributes.size <= attribute_per_event_count_limit &&
356+
event.attributes.size <= event_attribute_count_limit &&
357357
valid_attributes
358358
return events << event
359359
end
@@ -362,11 +362,11 @@ def append_event(events, event) # rubocop:disable Metrics/AbcSize, Metrics/Cyclo
362362
excess = events.size + 1 - event_count_limit
363363
events.shift(excess) if excess.positive?
364364

365-
excess = event.attributes.size - attribute_per_event_count_limit
365+
excess = event.attributes.size - event_attribute_count_limit
366366
if excess.positive? || !valid_attributes
367367
attrs = Hash[event.attributes] # event.attributes is frozen, so we need an unfrozen copy to adjust.
368368
attrs.keep_if { |key, value| Internal.valid_key?(key) && Internal.valid_value?(value) }
369-
excess = attrs.size - attribute_per_event_count_limit
369+
excess = attrs.size - event_attribute_count_limit
370370
excess.times { attrs.shift } if excess.positive?
371371
event = Event.new(event.name, attrs.freeze, event.timestamp)
372372
end

sdk/lib/opentelemetry/sdk/trace/span_limits.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class SpanLimits
2222
attr_reader :link_count_limit
2323

2424
# The global default max number of attributes per {OpenTelemetry::SDK::Trace::Event}.
25-
attr_reader :attribute_per_event_count_limit
25+
attr_reader :event_attribute_count_limit
2626

2727
# The global default max number of attributes per {OpenTelemetry::Trace::Link}.
28-
attr_reader :attribute_per_link_count_limit
28+
attr_reader :link_attribute_count_limit
2929

3030
# Returns a {SpanLimits} with the desired values.
3131
#
@@ -35,21 +35,21 @@ def initialize(attribute_count_limit: Integer(ENV.fetch('OTEL_SPAN_ATTRIBUTE_COU
3535
attribute_length_limit: ENV['OTEL_RUBY_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT'],
3636
event_count_limit: Integer(ENV.fetch('OTEL_SPAN_EVENT_COUNT_LIMIT', 128)),
3737
link_count_limit: Integer(ENV.fetch('OTEL_SPAN_LINK_COUNT_LIMIT', 128)),
38-
attribute_per_event_count_limit: attribute_count_limit,
39-
attribute_per_link_count_limit: attribute_count_limit)
38+
event_attribute_count_limit: Integer(ENV.fetch('OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT', 128)),
39+
link_attribute_count_limit: Integer(ENV.fetch('OTEL_LINK_ATTRIBUTE_COUNT_LIMIT', 128)))
4040
raise ArgumentError, 'attribute_count_limit must be positive' unless attribute_count_limit.positive?
4141
raise ArgumentError, 'attribute_length_limit must not be less than 32' unless attribute_length_limit.nil? || Integer(attribute_length_limit) >= 32
4242
raise ArgumentError, 'event_count_limit must be positive' unless event_count_limit.positive?
4343
raise ArgumentError, 'link_count_limit must be positive' unless link_count_limit.positive?
44-
raise ArgumentError, 'attribute_per_event_count_limit must be positive' unless attribute_per_event_count_limit.positive?
45-
raise ArgumentError, 'attribute_per_link_count_limit must be positive' unless attribute_per_link_count_limit.positive?
44+
raise ArgumentError, 'event_attribute_count_limit must be positive' unless event_attribute_count_limit.positive?
45+
raise ArgumentError, 'link_attribute_count_limit must be positive' unless link_attribute_count_limit.positive?
4646

4747
@attribute_count_limit = attribute_count_limit
4848
@attribute_length_limit = attribute_length_limit.nil? ? nil : Integer(attribute_length_limit)
4949
@event_count_limit = event_count_limit
5050
@link_count_limit = link_count_limit
51-
@attribute_per_event_count_limit = attribute_per_event_count_limit
52-
@attribute_per_link_count_limit = attribute_per_link_count_limit
51+
@event_attribute_count_limit = event_attribute_count_limit
52+
@link_attribute_count_limit = link_attribute_count_limit
5353
end
5454

5555
# The default {SpanLimits}.

sdk/test/opentelemetry/sdk/trace/span_limits_test.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@
1515
_(config.attribute_count_limit).must_equal 128
1616
_(config.event_count_limit).must_equal 128
1717
_(config.link_count_limit).must_equal 128
18-
_(config.attribute_per_event_count_limit).must_equal 128
19-
_(config.attribute_per_link_count_limit).must_equal 128
18+
_(config.event_attribute_count_limit).must_equal 128
19+
_(config.link_attribute_count_limit).must_equal 128
2020
end
2121

2222
it 'reflects environment variables' do
2323
with_env('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT' => '1',
2424
'OTEL_SPAN_EVENT_COUNT_LIMIT' => '2',
2525
'OTEL_SPAN_LINK_COUNT_LIMIT' => '3',
2626
'OTEL_RUBY_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '32',
27+
'OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT' => '5',
28+
'OTEL_LINK_ATTRIBUTE_COUNT_LIMIT' => '6',
2729
'OTEL_TRACES_SAMPLER' => 'always_on') do
2830
config = subject.new
2931
_(config.attribute_count_limit).must_equal 1
3032
_(config.event_count_limit).must_equal 2
3133
_(config.link_count_limit).must_equal 3
3234
_(config.attribute_length_limit).must_equal 32
33-
_(config.attribute_per_event_count_limit).must_equal 1
34-
_(config.attribute_per_link_count_limit).must_equal 1
35+
_(config.event_attribute_count_limit).must_equal 5
36+
_(config.link_attribute_count_limit).must_equal 6
3537
end
3638
end
3739

@@ -40,18 +42,20 @@
4042
'OTEL_SPAN_EVENT_COUNT_LIMIT' => '2',
4143
'OTEL_SPAN_LINK_COUNT_LIMIT' => '3',
4244
'OTEL_RUBY_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '4',
45+
'OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT' => '5',
46+
'OTEL_LINK_ATTRIBUTE_COUNT_LIMIT' => '6',
4347
'OTEL_TRACES_SAMPLER' => 'always_on') do
4448
config = subject.new(attribute_count_limit: 10,
4549
event_count_limit: 11,
4650
link_count_limit: 12,
47-
attribute_per_event_count_limit: 13,
48-
attribute_per_link_count_limit: 14,
51+
event_attribute_count_limit: 13,
52+
link_attribute_count_limit: 14,
4953
attribute_length_limit: 32)
5054
_(config.attribute_count_limit).must_equal 10
5155
_(config.event_count_limit).must_equal 11
5256
_(config.link_count_limit).must_equal 12
53-
_(config.attribute_per_event_count_limit).must_equal 13
54-
_(config.attribute_per_link_count_limit).must_equal 14
57+
_(config.event_attribute_count_limit).must_equal 13
58+
_(config.link_attribute_count_limit).must_equal 14
5559
_(config.attribute_length_limit).must_equal 32
5660
end
5761
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
attribute_count_limit: 1,
2020
event_count_limit: 1,
2121
link_count_limit: 1,
22-
attribute_per_event_count_limit: 1,
23-
attribute_per_link_count_limit: 1,
22+
event_attribute_count_limit: 1,
23+
link_attribute_count_limit: 1,
2424
attribute_length_limit: 32
2525
)
2626
end
@@ -250,7 +250,7 @@
250250
SpanLimits.new(
251251
attribute_count_limit: 10,
252252
event_count_limit: 5,
253-
attribute_per_event_count_limit: 10
253+
event_attribute_count_limit: 10
254254
)
255255
end
256256

sdk/test/opentelemetry/sdk/trace/tracer_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
end
131131

132132
it 'trims link attributes' do
133-
tracer_provider.span_limits = SpanLimits.new(attribute_per_link_count_limit: 1)
133+
tracer_provider.span_limits = SpanLimits.new(link_attribute_count_limit: 1)
134134
link = OpenTelemetry::Trace::Link.new(OpenTelemetry::Trace::SpanContext.new, '1' => 1, '2' => 2)
135135
span = tracer.start_root_span('root', links: [link])
136136
_(span.links.first.attributes.size).must_equal(1)
@@ -285,7 +285,7 @@
285285
end
286286

287287
it 'trims link attributes' do
288-
tracer_provider.span_limits = SpanLimits.new(attribute_per_link_count_limit: 1)
288+
tracer_provider.span_limits = SpanLimits.new(link_attribute_count_limit: 1)
289289
link = OpenTelemetry::Trace::Link.new(OpenTelemetry::Trace::SpanContext.new, '1' => 1, '2' => 2)
290290
span = tracer.start_span('op', with_parent: context, links: [link])
291291
_(span.links.first.attributes.size).must_equal(1)

0 commit comments

Comments
 (0)