Skip to content

Commit

Permalink
feat: support OTEL_ATTRIBUTE_{COUNT,VALUE_LENGTH}_LIMIT env vars (#1292)
Browse files Browse the repository at this point in the history
Fixes #1251 by additionally supporting `OTEL_ATTRIBUTE_COUNT_LIMIT` and
`OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT` as ways to configure span limits. As
per spec, the existing model-specific limits (eg: `OTEL_SPAN_ATTRIBUTE_...`
env vars) are preferred when they are present.

The easiest way to do this was to re-use the handy `config_opt` method
in `opentelemetry-common` package, on which we already depend. It made
the lines even longer, which is gross, but it works pretty nicely.

Co-authored-by: Francis Bogsanyi <francis.bogsanyi@shopify.com>
  • Loading branch information
ahayworth and fbogsany authored Jun 21, 2022
1 parent 85aba69 commit e9bd2b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 6 additions & 7 deletions sdk/lib/opentelemetry/sdk/trace/span_limits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ class SpanLimits
#
# @return [SpanLimits] with the desired values.
# @raise [ArgumentError] if any of the max numbers are not positive.
def initialize(attribute_count_limit: Integer(ENV.fetch('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT', 128)), # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
attribute_length_limit: ENV.fetch('OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT',
ENV['OTEL_RUBY_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT']),
event_count_limit: Integer(ENV.fetch('OTEL_SPAN_EVENT_COUNT_LIMIT', 128)),
link_count_limit: Integer(ENV.fetch('OTEL_SPAN_LINK_COUNT_LIMIT', 128)),
event_attribute_count_limit: Integer(ENV.fetch('OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT', 128)),
link_attribute_count_limit: Integer(ENV.fetch('OTEL_LINK_ATTRIBUTE_COUNT_LIMIT', 128)))
def initialize(attribute_count_limit: Integer(OpenTelemetry::Common::Utilities.config_opt('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT', 'OTEL_ATTRIBUTE_COUNT_LIMIT', default: 128)), # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
attribute_length_limit: OpenTelemetry::Common::Utilities.config_opt('OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT', 'OTEL_RUBY_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT', 'OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT'),
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)),
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?
Expand Down
21 changes: 21 additions & 0 deletions sdk/test/opentelemetry/sdk/trace/span_limits_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
_(config.link_attribute_count_limit).must_equal 6
end
end

it 'reflects explicit overrides' do
OpenTelemetry::TestHelpers.with_env('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT' => '1',
'OTEL_SPAN_EVENT_COUNT_LIMIT' => '2',
Expand All @@ -76,5 +77,25 @@
_(config.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
end
end

it 'prefers model-specific attribute env vars over generic attribute env vars' do
OpenTelemetry::TestHelpers.with_env('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT' => '1',
'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
end
end
end
end

0 comments on commit e9bd2b2

Please sign in to comment.