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 Python 3.9 when invalid type passed to attributes
([#4933](https://github.com/open-telemetry/opentelemetry-python/pull/4933))
- `opentelemetry-sdk`: Drop unused Jaeger exporter environment variables (exporter removed in 1.22.0)
([#4918](https://github.com/open-telemetry/opentelemetry-python/issues/4918))
- `opentelemetry-sdk`: Clarify timeout units in environment variable documentation
Expand Down
13 changes: 11 additions & 2 deletions opentelemetry-api/src/opentelemetry/attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
_logger = logging.getLogger(__name__)


def _get_type_name(tp: type) -> str:
"""Get the name of a type, handling typing module edge cases on Python 3.9."""
name = getattr(tp, "__name__", None)
if name is None:
# Handle typing module types like Sequence, Mapping that don't have __name__ on Python 3.9
name = getattr(tp, "_name", str(tp))
return name


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 @@ -113,7 +122,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 +199,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
Loading