From b656188d6756d0b90ace11857a8ece44ec1fd7fe Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Fri, 31 May 2024 12:27:25 -0300 Subject: [PATCH] experimenting log dropped attributes in boundedattributes --- .../src/opentelemetry/attributes/__init__.py | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/attributes/__init__.py b/opentelemetry-api/src/opentelemetry/attributes/__init__.py index 72ee38abd79..1c2d47fc0ff 100644 --- a/opentelemetry-api/src/opentelemetry/attributes/__init__.py +++ b/opentelemetry-api/src/opentelemetry/attributes/__init__.py @@ -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. @@ -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) @@ -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): @@ -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