Skip to content

feat: add support for OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT #3090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

msierks-pcln
Copy link

@msierks-pcln msierks-pcln commented Jul 28, 2025

User description

Fixes #2214

Changes

Add support for the OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT environment variable as explained in Opentelemetry SDK environment variable doc. This will truncate Value::String and Array::String attribute values if they exceed the limit.

  • Adds TracerProviderBuilder::with_max_attribute_value_length()

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@msierks-pcln msierks-pcln requested a review from a team as a code owner July 28, 2025 17:44
Copy link

linux-foundation-easycla bot commented Jul 28, 2025

CLA Not Signed

@msierks-pcln msierks-pcln force-pushed the span-attribute-value-length-limit branch 2 times, most recently from 3542d6c to 1bfde50 Compare July 28, 2025 18:00
Copy link

codecov bot commented Jul 28, 2025

Codecov Report

❌ Patch coverage is 96.07843% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.2%. Comparing base (42139cb) to head (f7dc9cd).

Files with missing lines Patch % Lines
opentelemetry-sdk/src/trace/span.rs 93.4% 3 Missing ⚠️
opentelemetry-sdk/src/trace/config.rs 66.6% 2 Missing ⚠️
opentelemetry/src/common.rs 98.8% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #3090     +/-   ##
=======================================
+ Coverage   80.1%   80.2%   +0.1%     
=======================================
  Files        126     126             
  Lines      21957   22109    +152     
=======================================
+ Hits       17603   17752    +149     
- Misses      4354    4357      +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msierks-pcln msierks-pcln force-pushed the span-attribute-value-length-limit branch 2 times, most recently from 5294944 to 5a73cc1 Compare July 28, 2025 18:20
@@ -4,6 +4,7 @@

- *Breaking* Change return type of `opentelemetry::global::set_tracer_provider` to Unit to align with metrics counterpart
- Add `get_all` method to `opentelemetry::propagation::Extractor` to return all values of the given propagation key and provide a default implementation.
- Add support for max span attribute value length; Applies to `Value::String` and `Array::String` only.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attribute length enforcements are not part of opentelemetry crate. Its part of opentelemetry_sdk crate, so changelog should be in that crate.

{
config.span_limits.max_attribute_value_length = max_attribute_value_length;
} else if let Some(max_attribute_value_length) =
env::var("OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supporting OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT looks reasonable, but OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT is not something we can add right now, as Logs don't respect this at all.

@@ -378,6 +378,12 @@ impl TracerProviderBuilder {
self
}

/// Specify the maximum allowed length of attribute values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you enhance the doc to make it clear that this is applicable to string values or array of strings.
and also mention that the trimming behavior is simply trimming!
(some people might expect "..." being added as the last 3 characters to indicate trimming has occurred.. But we are not doing that, and there is no way a backend can tell if trimming has occurred. So if a user opts in to this, they should be made aware of the implications)

@@ -378,6 +378,12 @@ impl TracerProviderBuilder {
self
}

/// Specify the maximum allowed length of attribute values.
pub fn with_max_attribute_value_length(mut self, max_attribute_value_length: i32) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we expect positive values only right? i32 is probably the wrong choice.

span.set_attribute(KeyValue::new("third", "test data, after span builder"));

span.with_data(|data| {
assert_eq!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of picking attributes at each index(0,1,..), could we do a general validation - for each string or array of string, validate that length is <max_attribute_value_length?

@@ -73,6 +73,14 @@ impl SdkTracer {
.saturating_sub(span_attributes_limit);
attribute_options.truncate(span_attributes_limit);
let dropped_attributes_count = dropped_attributes_count as u32;
let span_attribute_value_limit = span_limits.max_attribute_value_length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_attribute already does the truncation...Is it needed to be done again on this method?

Copy link
Author

@msierks-pcln msierks-pcln Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, this method is used by the SpanBuilder when a span is initially built. The set_attribute method is used for setting single attributes after the span has been created.

Copy link
Member

@cijothomas cijothomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. left some small comments.

One is blocking - https://github.com/open-telemetry/opentelemetry-rust/pull/3090/files#r2237524061

@msierks-pcln msierks-pcln force-pushed the span-attribute-value-length-limit branch from 5a73cc1 to f7dc9cd Compare July 29, 2025 13:04
@cijothomas
Copy link
Member

@msierks-pcln Can you sign the CLA, so we can consider contributions? It's a one-time process and is mandatory. If contributing on behalf of your employer, please do consult them before signing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants