Skip to content

Commit

Permalink
fix: SpanLimits setting event attributes length limit (#1230)
Browse files Browse the repository at this point in the history
* fix: SpanLimits setting event attributes length limit
  • Loading branch information
robertlaurin authored Sep 14, 2022
1 parent 3402bfd commit 70bdf98
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 45 deletions.
8 changes: 3 additions & 5 deletions sdk/lib/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def add_attributes(attributes)
#
# @return [self] returns itself
def add_event(name, attributes: nil, timestamp: nil)
event = Event.new(name, truncate_attribute_values(attributes), relative_timestamp(timestamp))
event = Event.new(name, truncate_attribute_values(attributes, @span_limits.event_attribute_length_limit), relative_timestamp(timestamp))

@mutex.synchronize do
if @ended
Expand Down Expand Up @@ -349,14 +349,12 @@ def trim_span_attributes(attrs)

excess = attrs.size - @span_limits.attribute_count_limit
excess.times { attrs.shift } if excess.positive?
truncate_attribute_values(attrs)
truncate_attribute_values(attrs, @span_limits.attribute_length_limit)
nil
end

def truncate_attribute_values(attrs)
def truncate_attribute_values(attrs, attribute_length_limit)
return EMPTY_ATTRIBUTES if attrs.nil?

attribute_length_limit = @span_limits.attribute_length_limit
return attrs if attribute_length_limit.nil?

attrs.transform_values! { |value| OpenTelemetry::Common::Utilities.truncate_attribute_value(value, attribute_length_limit) }
Expand Down
7 changes: 7 additions & 0 deletions sdk/lib/opentelemetry/sdk/trace/span_limits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class SpanLimits
# The global default max number of attributes per {OpenTelemetry::SDK::Trace::Event}.
attr_reader :event_attribute_count_limit

# The global default max length of attribute value per {OpenTelemetry::SDK::Trace::Event}.
attr_reader :event_attribute_length_limit

# The global default max number of attributes per {OpenTelemetry::Trace::Link}.
attr_reader :link_attribute_count_limit

Expand All @@ -36,19 +39,23 @@ def initialize(attribute_count_limit: Integer(OpenTelemetry::Common::Utilities.c
event_count_limit: Integer(OpenTelemetry::Common::Utilities.config_opt('OTEL_SPAN_EVENT_COUNT_LIMIT', default: 128)),
link_count_limit: Integer(OpenTelemetry::Common::Utilities.config_opt('OTEL_SPAN_LINK_COUNT_LIMIT', default: 128)),
event_attribute_count_limit: Integer(OpenTelemetry::Common::Utilities.config_opt('OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT', default: 128)),
event_attribute_length_limit: OpenTelemetry::Common::Utilities.config_opt('OTEL_EVENT_ATTRIBUTE_VALUE_LENGTH_LIMIT', 'OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT'),
link_attribute_count_limit: Integer(OpenTelemetry::Common::Utilities.config_opt('OTEL_LINK_ATTRIBUTE_COUNT_LIMIT', default: 128)))

raise ArgumentError, 'attribute_count_limit must be positive' unless attribute_count_limit.positive?
raise ArgumentError, 'attribute_length_limit must not be less than 32' unless attribute_length_limit.nil? || Integer(attribute_length_limit) >= 32
raise ArgumentError, 'event_count_limit must be positive' unless event_count_limit.positive?
raise ArgumentError, 'link_count_limit must be positive' unless link_count_limit.positive?
raise ArgumentError, 'event_attribute_count_limit must be positive' unless event_attribute_count_limit.positive?
raise ArgumentError, 'event_attribute_length_limit must not be less than 32' unless event_attribute_length_limit.nil? || Integer(event_attribute_length_limit) >= 32
raise ArgumentError, 'link_attribute_count_limit must be positive' unless link_attribute_count_limit.positive?

@attribute_count_limit = attribute_count_limit
@attribute_length_limit = attribute_length_limit.nil? ? nil : Integer(attribute_length_limit)
@event_count_limit = event_count_limit
@link_count_limit = link_count_limit
@event_attribute_count_limit = event_attribute_count_limit
@event_attribute_length_limit = event_attribute_length_limit.nil? ? nil : Integer(event_attribute_length_limit)
@link_attribute_count_limit = link_attribute_count_limit
end

Expand Down
96 changes: 57 additions & 39 deletions sdk/test/opentelemetry/sdk/trace/span_limits_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,33 @@
require 'test_helper'

describe OpenTelemetry::SDK::Trace::SpanLimits do
let(:subject) { OpenTelemetry::SDK::Trace::SpanLimits }
let(:span_limits) { OpenTelemetry::SDK::Trace::SpanLimits.new }

describe '#initialize' do
it 'provides defaults' do
config = subject.new
_(config.attribute_count_limit).must_equal 128
_(config.event_count_limit).must_equal 128
_(config.link_count_limit).must_equal 128
_(config.event_attribute_count_limit).must_equal 128
_(config.link_attribute_count_limit).must_equal 128
_(span_limits.attribute_count_limit).must_equal 128
_(span_limits.attribute_length_limit).must_be_nil
_(span_limits.event_count_limit).must_equal 128
_(span_limits.link_count_limit).must_equal 128
_(span_limits.event_attribute_count_limit).must_equal 128
_(span_limits.event_attribute_length_limit).must_be_nil
_(span_limits.link_attribute_count_limit).must_equal 128
end

it 'prioritizes specific environment varibles for attribute value length limits' do
OpenTelemetry::TestHelpers.with_env('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '35',
'OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '33',
'OTEL_EVENT_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '32') do
_(span_limits.attribute_length_limit).must_equal 33
_(span_limits.event_attribute_length_limit).must_equal 32
end
end

it 'uses general attribute value length limits in the absence of more specific ones' do
OpenTelemetry::TestHelpers.with_env('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '35') do
_(span_limits.attribute_length_limit).must_equal 35
_(span_limits.event_attribute_length_limit).must_equal 35
end
end

it 'reflects environment variables' do
Expand All @@ -27,13 +44,13 @@
'OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT' => '5',
'OTEL_LINK_ATTRIBUTE_COUNT_LIMIT' => '6',
'OTEL_TRACES_SAMPLER' => 'always_on') do
config = subject.new
_(config.attribute_count_limit).must_equal 1
_(config.event_count_limit).must_equal 2
_(config.link_count_limit).must_equal 3
_(config.attribute_length_limit).must_equal 32
_(config.event_attribute_count_limit).must_equal 5
_(config.link_attribute_count_limit).must_equal 6
_(span_limits.attribute_count_limit).must_equal 1
_(span_limits.event_count_limit).must_equal 2
_(span_limits.link_count_limit).must_equal 3
_(span_limits.attribute_length_limit).must_equal 32
_(span_limits.event_attribute_count_limit).must_equal 5
_(span_limits.event_attribute_length_limit).must_be_nil
_(span_limits.link_attribute_count_limit).must_equal 6
end
end

Expand All @@ -45,13 +62,13 @@
'OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT' => '5',
'OTEL_LINK_ATTRIBUTE_COUNT_LIMIT' => '6',
'OTEL_TRACES_SAMPLER' => 'always_on') do
config = subject.new
_(config.attribute_count_limit).must_equal 1
_(config.event_count_limit).must_equal 2
_(config.link_count_limit).must_equal 3
_(config.attribute_length_limit).must_equal 32
_(config.event_attribute_count_limit).must_equal 5
_(config.link_attribute_count_limit).must_equal 6
_(span_limits.attribute_count_limit).must_equal 1
_(span_limits.event_count_limit).must_equal 2
_(span_limits.link_count_limit).must_equal 3
_(span_limits.attribute_length_limit).must_equal 32
_(span_limits.event_attribute_count_limit).must_equal 5
_(span_limits.event_attribute_length_limit).must_be_nil
_(span_limits.link_attribute_count_limit).must_equal 6
end
end

Expand All @@ -61,29 +78,31 @@
'OTEL_SPAN_LINK_COUNT_LIMIT' => '3',
'OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '4',
'OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT' => '5',
'OTEL_EVENT_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '32',
'OTEL_LINK_ATTRIBUTE_COUNT_LIMIT' => '6',
'OTEL_TRACES_SAMPLER' => 'always_on') do
config = subject.new(attribute_count_limit: 10,
event_count_limit: 11,
link_count_limit: 12,
event_attribute_count_limit: 13,
link_attribute_count_limit: 14,
attribute_length_limit: 32)
_(config.attribute_count_limit).must_equal 10
_(config.event_count_limit).must_equal 11
_(config.link_count_limit).must_equal 12
_(config.event_attribute_count_limit).must_equal 13
_(config.link_attribute_count_limit).must_equal 14
_(config.attribute_length_limit).must_equal 32
span_limits = OpenTelemetry::SDK::Trace::SpanLimits.new(attribute_count_limit: 10,
event_count_limit: 11,
link_count_limit: 12,
event_attribute_count_limit: 13,
event_attribute_length_limit: 40,
link_attribute_count_limit: 14,
attribute_length_limit: 32)
_(span_limits.attribute_count_limit).must_equal 10
_(span_limits.event_count_limit).must_equal 11
_(span_limits.link_count_limit).must_equal 12
_(span_limits.event_attribute_count_limit).must_equal 13
_(span_limits.event_attribute_length_limit).must_equal 40
_(span_limits.link_attribute_count_limit).must_equal 14
_(span_limits.attribute_length_limit).must_equal 32
end
end

it 'reflects generic attribute env vars' do
OpenTelemetry::TestHelpers.with_env('OTEL_ATTRIBUTE_COUNT_LIMIT' => '1',
'OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '32') do
config = subject.new
_(config.attribute_count_limit).must_equal 1
_(config.attribute_length_limit).must_equal 32
_(span_limits.attribute_count_limit).must_equal 1
_(span_limits.attribute_length_limit).must_equal 32
end
end

Expand All @@ -92,9 +111,8 @@
'OTEL_ATTRIBUTE_COUNT_LIMIT' => '2',
'OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '32',
'OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT' => '33') do
config = subject.new
_(config.attribute_count_limit).must_equal 1
_(config.attribute_length_limit).must_equal 32
_(span_limits.attribute_count_limit).must_equal 1
_(span_limits.attribute_length_limit).must_equal 32
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion sdk/test/opentelemetry/sdk/trace/span_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
link_count_limit: 1,
event_attribute_count_limit: 1,
link_attribute_count_limit: 1,
attribute_length_limit: 32
attribute_length_limit: 32,
event_attribute_length_limit: 32
)
end
let(:span) do
Expand Down

0 comments on commit 70bdf98

Please sign in to comment.