Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
sdk: Change attribute validation logic
Browse files Browse the repository at this point in the history
4fca8c9 ("Add runtime validation in setAttribute (open-telemetry#348)") added a robust
attribute validation using numbers.Number to validate numeric types.
Although the approach is correct, it presents some complications because
Complex, Fraction and Decimal are accepted because they are Numbers. This
presents a problem to the exporters because they will have to consider all
these different cases when converting attributes to the underlying exporter
representation.

This commit simplifies the logic by accepting only int and float as numeric
values.
  • Loading branch information
mauriciovasquezbernal committed Mar 4, 2020
1 parent 888bed9 commit 78fd6a0
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import random
import threading
from contextlib import contextmanager
from numbers import Number
from types import TracebackType
from typing import Iterator, Optional, Sequence, Tuple, Type

Expand Down Expand Up @@ -238,7 +237,7 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
if error_message is not None:
logger.warning("%s in attribute value sequence", error_message)
return
elif not isinstance(value, (bool, str, Number, Sequence)):
elif not isinstance(value, (bool, str, int, float)):
logger.warning("invalid type for attribute value")
return

Expand All @@ -254,12 +253,13 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]:

first_element_type = type(sequence[0])

if issubclass(first_element_type, Number):
first_element_type = Number

if first_element_type not in (bool, str, Number):
if first_element_type not in (bool, str, int, float):
return "invalid type"

# int and float are both numeric types, allow mixed arrays of those
if first_element_type in (int, float):
first_element_type = (int, float)

for element in sequence:
if not isinstance(element, first_element_type):
return "different type"
Expand Down

0 comments on commit 78fd6a0

Please sign in to comment.