diff --git a/giskard/utils/analytics_collector.py b/giskard/utils/analytics_collector.py index a5c89781f1..428c981fd2 100644 --- a/giskard/utils/analytics_collector.py +++ b/giskard/utils/analytics_collector.py @@ -4,11 +4,11 @@ import platform import sys import threading -from traceback import TracebackException -from types import TracebackType import uuid from functools import wraps from threading import ExceptHookArgs, Lock +from traceback import TracebackException +from types import TracebackType from typing import Dict, Optional, Type import requests @@ -31,11 +31,8 @@ def inner_function(*args, **kwargs): try: return f(*args, **kwargs) - except BaseException as e: # NOSONAR - try: - _report_error(e, error_type="tracking error") - except BaseException: # NOSONAR - pass + except BaseException: # NOSONAR + pass return inner_function @@ -149,6 +146,7 @@ def track(self, event_name, properties=None, meta=None, force=False): return self._track(event_name, properties=properties, meta=meta, force=force) @threaded + @analytics_method def _track(self, event_name, properties=None, meta=None, force=False): self.initialize_giskard_version() self.initialize_user_properties() diff --git a/tests/communications/test_analytics_collector.py b/tests/communications/test_analytics_collector.py new file mode 100644 index 0000000000..94a1f46220 --- /dev/null +++ b/tests/communications/test_analytics_collector.py @@ -0,0 +1,44 @@ +import pytest +from mixpanel import Consumer + +from giskard.settings import settings +from giskard.utils.analytics_collector import GiskardAnalyticsCollector, analytics_method + + +class EnableAnalytics: + """ + Context manager to enable/disable analytics on demand, disregarding the global settings + """ + def __init__(self, do_enable=True): + self.new_value = do_enable + self.original_value = settings.disable_analytics + + def __enter__(self): + settings.disable_analytics = not self.new_value + + def __exit__(self, exc_type, exc_val, exc_tb): + settings.disable_analytics = self.original_value + + +def test_tracking_doesnt_throw_errors(): + with EnableAnalytics(): + ac = GiskardAnalyticsCollector() + ac.mp._consumer = Consumer(events_url="https://invalid.url") + ac.track("test") + + +def test_analytics_method(): + def no_exception(): + return "OK" + + def exception(): + raise RuntimeError("KO") + + with EnableAnalytics(): + assert analytics_method(no_exception)() == "OK" + with pytest.raises(RuntimeError, match="KO"): + assert analytics_method(exception())() + + with EnableAnalytics(False): + assert analytics_method(no_exception)() is None + assert analytics_method(exception)() is None