Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `opentelemetry-api`: Fix AttributeError on invalid attribute type in Python 3.9
([#4905](https://github.com/open-telemetry/opentelemetry-python/pull/4905))
- `opentelemetry-exporter-otlp-proto-grpc`: Fix re-initialization of gRPC channel on UNAVAILABLE error
([#4825](https://github.com/open-telemetry/opentelemetry-python/pull/4825))
- `opentelemetry-exporter-prometheus`: Fix duplicate HELP/TYPE declarations for metrics with different label sets
Expand Down
11 changes: 8 additions & 3 deletions opentelemetry-api/src/opentelemetry/attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
_logger = logging.getLogger(__name__)


def _get_type_name(type_: type) -> str:
"""Get the name of a type, handling typing generics that may lack __name__."""
return getattr(type_, "__name__", str(type_))


def _clean_attribute(
key: str, value: types.AttributeValue, max_len: Optional[int]
) -> Optional[Union[types.AttributeValue, Tuple[Union[str, int, float], ...]]]:
Expand Down Expand Up @@ -83,7 +88,7 @@ def _clean_attribute(
element_type.__name__,
key,
[
valid_type.__name__
_get_type_name(valid_type)
for valid_type in _VALID_ATTR_VALUE_TYPES
],
)
Expand Down Expand Up @@ -113,7 +118,7 @@ def _clean_attribute(
"sequence of those types",
type(value).__name__,
key,
[valid_type.__name__ for valid_type in _VALID_ATTR_VALUE_TYPES],
[_get_type_name(valid_type) for valid_type in _VALID_ATTR_VALUE_TYPES],
)
return None

Expand Down Expand Up @@ -190,7 +195,7 @@ def _clean_extended_attribute_value( # pylint: disable=too-many-branches
except Exception:
raise TypeError(
f"Invalid type {type(value).__name__} for attribute value. "
f"Expected one of {[valid_type.__name__ for valid_type in _VALID_ANY_VALUE_TYPES]} or a "
f"Expected one of {[_get_type_name(valid_type) for valid_type in _VALID_ANY_VALUE_TYPES]} or a "
"sequence of those types",
)

Expand Down
26 changes: 25 additions & 1 deletion opentelemetry-api/tests/attributes/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

import unittest
from typing import MutableSequence
from unittest import mock

from opentelemetry.attributes import (
BoundedAttributes,
_clean_attribute,
_clean_extended_attribute,
_clean_extended_attribute_value,
)


Expand Down Expand Up @@ -182,6 +184,28 @@ def test_mapping(self):
_clean_extended_attribute("headers", mapping, None), expected
)

def test_invalid_type_error_message(self):
"""Test that invalid types produce proper TypeError, not AttributeError.

Regression test for issue #4821 where typing.Mapping lacks __name__ in Python 3.9.
"""

# Create a class that raises when converted to string
class InvalidType:
def __str__(self):
raise ValueError("Cannot convert to string")

# This should raise TypeError with expected types listed, not AttributeError
with self.assertRaises(TypeError) as ctx:
_clean_extended_attribute_value(InvalidType(), None)

# Verify the error message contains expected information and doesn't raise AttributeError
error_msg = str(ctx.exception)
self.assertIn("Invalid type", error_msg)
self.assertIn("InvalidType", error_msg)
# Ensure the message includes valid types without raising AttributeError
self.assertIn("Expected one of", error_msg)


class TestBoundedAttributes(unittest.TestCase):
# pylint: disable=consider-using-dict-items
Expand Down Expand Up @@ -294,7 +318,7 @@ def test_locking(self):
# pylint: disable=no-self-use
def test_extended_attributes(self):
bdict = BoundedAttributes(extended_attributes=True, immutable=False)
with unittest.mock.patch(
with mock.patch(
"opentelemetry.attributes._clean_extended_attribute",
return_value="mock_value",
) as clean_extended_attribute_mock:
Expand Down
Loading