Repository navigation
Open
Labels
bugSomething isn't working
Description
Describe your environment
OS: Windows
Python version: Python 3.13
SDK version: the latest
API version: the latest
TraceState.add() does not fully check whether the inputs are correct.
What happened?
TraceState.add() should reject the illegal inputs.
Steps to Reproduce
Here is my test case:
`from opentelemetry.trace import TraceState
Java constants mapped to Python
EMPTY = TraceState()
FIRST_VALUE = "first_value" # Adjust to match your test's actual constant
class MyTestCase(unittest.TestCase):
def test_valid_long_tenant_id(self):
result = EMPTY.add("12345678901234567890@nr", FIRST_VALUE)
assert result.get("12345678901234567890@nr") == FIRST_VALUE
def test_vendor_id_longer_than_13_characters(self):
with self.assertRaises(Exception) as context:
EMPTY.add("1@nrabcdefghijkl", FIRST_VALUE)
self.assertTrue('This is broken' in context.exception)
def test_vendor_id_longer_than_13_characters_long_tenant_id(self):
with self.assertRaises(Exception) as context:
EMPTY.add("12345678901234567890@nrabcdefghijkl", FIRST_VALUE)
self.assertTrue('This is broken' in context.exception)
def test_tenant_id_longer_than_240_characters(self):
tenant_id = "a" * 241
with self.assertRaises(Exception) as context:
EMPTY.add(f"{tenant_id}@nr", FIRST_VALUE)
self.assertTrue('This is broken' in context.exception)
def test_non_vendor_format_first_key_character(self):
with self.assertRaises(Exception) as context:
EMPTY.add("1acdfrgs", FIRST_VALUE)
self.assertTrue('This is broken' in context.exception)`
Expected Result
Shall reject.
Actual Result
Added.
Additional context
No response
Would you like to implement a fix?
None
Tip
React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Type
Fields
Priority
None yet
Activity
Abhinav0905 commentedon Aug 9, 2026
I’d like to work on this. I’ll open a draft PR once the focused UI tests pass.
Abhinav0905 commentedon Aug 17, 2026
I reviewed this against current
mainand the W3C Trace Context grammar before starting the implementation. The existing pattern already rejects a simple key beginning with a digit, such as1acdfrgs. However, the two length examples appear to be valid under Trace Context Level 1:tenant-idpermits 241 total characters andsystem-idpermits 14 total characters. Level 2 changes the grammar further to a general key of up to 256 characters that may begin with a lowercase letter or digit and may contain@.Also,
TraceState.add()currently handles invalid pairs by returning the unchanged state rather than raising an exception.Could a maintainer confirm whether this issue is targeting Trace Context Level 1, Level 2, or compatibility with the Java implementation, and identify the exact boundary cases Python should enforce? Once confirmed, I can add the appropriate validation and focused TraceState tests.
ocelotl commentedon Sep 6, 2026
The relevant grammar from https://www.w3.org/TR/trace-context/ is:
key = simple-key / multi-tenant-key
simple-key = lcalpha 0255( lcalpha / DIGIT / "" / "-"/ "" / "/" )
multi-tenant-key = tenant-id "@" system-id
tenant-id = ( lcalpha / DIGIT ) 0240( lcalpha / DIGIT / "" / "-"/ "" / "/" )
system-id = lcalpha 013( lcalpha / DIGIT / "_" / "-"/ "" / "/" )
system-idis one lowercase letter plus up to 13 more characters, so the vendorpart is at most 14 characters, not 13.
tenant-idis one character plus up to240, so at most 241. That is exactly what
_KEY_FORMATinopentelemetry-api/src/opentelemetry/trace/span.pyalready encodes with{0,13}and
{0,240}.I think the current implementation is right, @open-telemetry/python-maintainers please close this issue.