diff --git a/opentelemetry-api/src/opentelemetry/distributedcontext/__init__.py b/opentelemetry-api/src/opentelemetry/distributedcontext/__init__.py index 7b57b94173d..dd58824b364 100644 --- a/opentelemetry-api/src/opentelemetry/distributedcontext/__init__.py +++ b/opentelemetry-api/src/opentelemetry/distributedcontext/__init__.py @@ -49,7 +49,7 @@ def __new__(cls, value: str) -> "EntryKey": @staticmethod def create(value: str) -> "EntryKey": - if len(value) > 255 or any(c not in PRINTABLE for c in value): + if not 0 < len(value) <= 255 or any(c not in PRINTABLE for c in value): raise ValueError("Invalid EntryKey", value) return typing.cast(EntryKey, value) diff --git a/opentelemetry-api/tests/distributedcontext/test_distributed_context.py b/opentelemetry-api/tests/distributedcontext/test_distributed_context.py index 936f0aa78ac..be1aadac9a1 100644 --- a/opentelemetry-api/tests/distributedcontext/test_distributed_context.py +++ b/opentelemetry-api/tests/distributedcontext/test_distributed_context.py @@ -32,6 +32,10 @@ def test_entry_ttl_unlimited_propagation(self): class TestEntryKey(unittest.TestCase): + def test_create_empty(self): + with self.assertRaises(ValueError): + distributedcontext.EntryKey.create("") + def test_create_too_long(self): with self.assertRaises(ValueError): distributedcontext.EntryKey.create("a" * 256)