Skip to content

Added guard against negative value of max_value_len - #5647

Open
GauravR31 wants to merge 4 commits into
open-telemetry:mainfrom
GauravR31:issue-5646
Open

GauravR31 wants to merge 4 commits into
open-telemetry:mainfrom
GauravR31:issue-5646

Conversation

@GauravR31

@GauravR31 GauravR31 commented Sep 9, 2026

Copy link
Copy Markdown

Description

Previously, passing a negative value for max_value_len when initializing BoundedAttributes leads to Python string slicing (value[:max_value_len]) chopping characters off the end of strings instead of enforcing length limits, while logging misleading warning messages.
There was no check for a negative value, this PR adds this to the BoundedAttributes constructor.

Fixes #5646

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Before

from opentelemetry.attributes import BoundedAttributes
ba = BoundedAttributes(max_value_len=-2, immutable=False)
ba["test_key"] = "hello world"
print(dict(ba))

No ValueError is raised during __init__. The string is truncated unexpectedly from the right ({'test_key': 'hello wor'}), and an invalid warning log is emitted (String attribute value exceeds max length of -2, truncating.).

After

from opentelemetry.attributes import BoundedAttributes
ba = BoundedAttributes(max_value_len=-2, immutable=False)

    raise ValueError("max_value_len must be valid int greater or equal to 0")
ValueError: max_value_len must be valid int greater or equal to 0

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@GauravR31
GauravR31 requested a review from a team as a code owner September 9, 2026 22:55
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 9, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@GauravR31 GauravR31 changed the title Added guard for max_value_len Added guard against negative value of max_value_len Sep 9, 2026
@github-project-automation github-project-automation Bot moved this to Approved PRs in Python PR digest Sep 9, 2026
@lzchen

lzchen commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

@GauravR31

Please rename changelog for the PR not the ISSUE. (.changelog/5647.fixed)

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 10, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-09-14 18:43 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@GauravR31

Copy link
Copy Markdown
Author

@lzchen Thanks for catching that, have renamed the changelog accordingly

@lzchen
lzchen enabled auto-merge September 15, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

[bug] BoundedAttributes missing negative value validation for max_value_len parameter

4 participants