Skip to content
Merged
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
30 changes: 23 additions & 7 deletions giskard/utils/analytics_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import platform
import sys
import threading
import traceback
from traceback import TracebackException
from types import TracebackType
import uuid
from functools import wraps
from threading import Lock
from typing import Dict, Optional
from threading import ExceptHookArgs, Lock
from typing import Dict, Optional, Type

import requests
from mixpanel import Mixpanel
Expand Down Expand Up @@ -82,8 +83,8 @@ def get_dataset_properties(dataset):
}


def _report_error(e, error_type = "python error"):
exc = traceback.TracebackException.from_exception(e)
def _report_error(e, error_type="python error"):
exc = TracebackException.from_exception(e)
is_giskard_error = False
for frame in exc.stack:
if not is_giskard_error and "giskard" in frame.filename:
Expand Down Expand Up @@ -225,7 +226,7 @@ def initialize_geo(self):


def add_exception_hook(original_hook=None):
def _exception_hook(type, value, traceback):
def _exception_hook(type: Type[BaseException], value: BaseException, traceback: Optional[TracebackType]):
try:
_report_error(value)
except BaseException: # noqa NOSONAR
Expand All @@ -239,8 +240,23 @@ def _exception_hook(type, value, traceback):
return _exception_hook


def add_thread_exception_hook(original_hook=None):
def _thread_exception_hook(args: ExceptHookArgs):
try:
_report_error(args.exc_value)
except BaseException: # noqa NOSONAR
pass

if original_hook:
return original_hook(args)
else:
return sys.__excepthook__(args.exc_type, args.exc_value, args.exc_traceback)

return _thread_exception_hook


if not settings.disable_analytics:
sys.excepthook = add_exception_hook(sys.excepthook)
threading.excepthook = add_exception_hook(threading.excepthook)
threading.excepthook = add_thread_exception_hook(threading.excepthook)

analytics = GiskardAnalyticsCollector()