Skip to content

Commit

Permalink
experimenting log dropped attributes in boundedattributes
Browse files Browse the repository at this point in the history
  • Loading branch information
emdneto committed May 31, 2024
1 parent d73593d commit b656188
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions opentelemetry-api/src/opentelemetry/attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,35 @@ def _clean_attribute_value(
return value


class Once:
def __init__(self):
# self._lock = threading.Lock()
self._run = False

def __call__(self, func):
def wrapper(*args, **kwargs):
# with self._lock:
if not self._run:
self._run = True
return func(*args, **kwargs)

return wrapper


class SDKLogger:
def __enter__(self):
_logger.propagate = False

def __exit__(self, exit_type, exit_value, exit_traceback):
_logger.propagate = True


@Once()
def _log_dropped_attributes_warning(msg: str) -> None:
with SDKLogger():
_logger.warning(msg)


class BoundedAttributes(MutableMapping):
"""An ordered dict with a fixed max capacity.
Expand Down Expand Up @@ -167,7 +196,7 @@ def __setitem__(self, key, value):
raise TypeError
with self._lock:
if self.maxlen is not None and self.maxlen == 0:
self.dropped += 1
self._add_dropped(1)
return

value = _clean_attribute(key, value, self.max_value_len)
Expand All @@ -180,8 +209,7 @@ def __setitem__(self, key, value):
if not isinstance(self._dict, OrderedDict):
self._dict = OrderedDict(self._dict)
self._dict.popitem(last=False)
self.dropped += 1

self._add_dropped(1)
self._dict[key] = value

def __delitem__(self, key):
Expand All @@ -199,3 +227,9 @@ def __len__(self):

def copy(self):
return self._dict.copy()

def _add_dropped(self, n: int):
_log_dropped_attributes_warning(
"Attributes were discarded due to limits"
)
self.dropped += n

0 comments on commit b656188

Please sign in to comment.