From d7728afea05f9a37adb56c73e8690cf4112e4d95 Mon Sep 17 00:00:00 2001 From: shaun Date: Tue, 1 Nov 2016 20:48:24 +1100 Subject: [PATCH] add throttling to error metric logging add back in consumption of exception in function wrapper --- addon.xml | 2 +- changelog.txt | 3 +++ resources/lib/ga_client.py | 16 +++++++++++++++- resources/lib/objects/_common.py | 6 +++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/addon.xml b/addon.xml index b4476d57..71fcf025 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/changelog.txt b/changelog.txt index 612f9efd..c6b947e5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,6 @@ +version 2.3.4 +- add throttling for error event logging + version 2.3.3 - minor fix to exception handling diff --git a/resources/lib/ga_client.py b/resources/lib/ga_client.py index d4b476d6..0c70b2f2 100644 --- a/resources/lib/ga_client.py +++ b/resources/lib/ga_client.py @@ -8,6 +8,7 @@ import md5 import xbmc import platform import xbmcgui +import time from utils import window, settings, language as lang log = logging.getLogger("EMBY."+__name__) @@ -15,6 +16,8 @@ log = logging.getLogger("EMBY."+__name__) # for info on the metrics that can be sent to Google Analytics # https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#events +logEventHistory = {} + class GoogleAnalytics(): testing = False @@ -128,7 +131,18 @@ class GoogleAnalytics(): self.sendData(data) - def sendEventData(self, eventCategory, eventAction, eventLabel=None): + def sendEventData(self, eventCategory, eventAction, eventLabel=None, throttle=False): + + # if throttling is enabled then only log the same event every 60 seconds + if(throttle): + throttleKey = eventCategory + "-" + eventAction + "-" + str(eventLabel) + lastLogged = logEventHistory.get(throttleKey) + if(lastLogged != None): + timeSinceLastLog = time.time() - lastLogged + if(timeSinceLastLog < 60): + log.info("SKIPPING_LOG_EVENT : " + str(timeSinceLastLog) + " " + throttleKey) + return + logEventHistory[throttleKey] = time.time() data = self.getBaseData() data['t'] = 'event' # action type diff --git a/resources/lib/objects/_common.py b/resources/lib/objects/_common.py index 4100cb30..484b602d 100644 --- a/resources/lib/objects/_common.py +++ b/resources/lib/objects/_common.py @@ -22,7 +22,7 @@ ga = GoogleAnalytics() ################################################################################################## -def catch_except(errors=(Exception, )): +def catch_except(errors=(Exception, ), default_value=False): # Will wrap method with try/except and print parameters for easier debugging def decorator(func): def wrapper(*args, **kwargs): @@ -30,11 +30,11 @@ def catch_except(errors=(Exception, )): return func(*args, **kwargs) except errors as error: errStrings = ga.formatException() - ga.sendEventData("Exception", errStrings[0], errStrings[1]) + ga.sendEventData("Exception", errStrings[0], errStrings[1], True) log.exception(error) log.error("function: %s \n args: %s \n kwargs: %s", func.__name__, args, kwargs) - raise + return default_value return wrapper return decorator