From 89229a8f2c75ab44ea5b6142ed0acd892cfb64e2 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 18 Jan 2017 11:22:07 -0500 Subject: [PATCH 1/3] Don't publish registry events to Redis for robots The tutorial can only be used by users, so no need to publish for robots, which can cause issues in pulling for builders and other prod mechanisms if Redis is being finicky --- endpoints/trackhelper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/trackhelper.py b/endpoints/trackhelper.py index f1bc708e5..5c336643c 100644 --- a/endpoints/trackhelper.py +++ b/endpoints/trackhelper.py @@ -65,7 +65,7 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1, # Publish the user event (if applicable) logger.debug('Checking publishing %s to the user events system', event_name) - if authenticated_user: + if authenticated_user and not authenticated_user.robot: logger.debug('Publishing %s to the user events system', event_name) user_event_data = { 'action': event_name, From 669a3070bd28e9cce2cc8ccd13f0a97343565549 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 18 Jan 2017 11:23:23 -0500 Subject: [PATCH 2/3] Only parse request URL in track_and_log when necessary --- endpoints/trackhelper.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/endpoints/trackhelper.py b/endpoints/trackhelper.py index 5c336643c..0aa66cefa 100644 --- a/endpoints/trackhelper.py +++ b/endpoints/trackhelper.py @@ -56,13 +56,6 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1, metadata['public'] = True analytics_id = 'anonymous' - request_parsed = urlparse(request.url_root) - extra_params = { - 'repository': '%s/%s' % (namespace_name, repo_name), - 'user-agent': request.user_agent.string, - 'hostname': request_parsed.hostname, - } - # Publish the user event (if applicable) logger.debug('Checking publishing %s to the user events system', event_name) if authenticated_user and not authenticated_user.robot: @@ -80,7 +73,16 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1, if random.random() < analytics_sample: if analytics_name is None: analytics_name = event_name + logger.debug('Logging the %s to Mixpanel', analytics_name) + + request_parsed = urlparse(request.url_root) + extra_params = { + 'repository': '%s/%s' % (namespace_name, repo_name), + 'user-agent': request.user_agent.string, + 'hostname': request_parsed.hostname, + } + analytics.track(analytics_id, analytics_name, extra_params) # Log the action to the database. From 3cf8f6c28a161e9822a5360bed4a14c962ce64a5 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 18 Jan 2017 11:27:00 -0500 Subject: [PATCH 3/3] Cleanup user event reporting and lower its timeout --- data/userevent.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/data/userevent.py b/data/userevent.py index 47e433c89..a5b7671ca 100644 --- a/data/userevent.py +++ b/data/userevent.py @@ -1,8 +1,9 @@ -import redis import json import threading import logging +import redis + logger = logging.getLogger(__name__) class CannotReadUserEventsException(Exception): @@ -56,7 +57,7 @@ class UserEvent(object): as backed by Redis. """ def __init__(self, redis_config, username): - self._redis = redis.StrictRedis(socket_connect_timeout=5, **redis_config) + self._redis = redis.StrictRedis(socket_connect_timeout=2, **redis_config) self._username = username @staticmethod @@ -75,7 +76,7 @@ class UserEvent(object): try: self.publish_event_data_sync(event_id, data_obj) logger.debug('Published user event %s: %s', event_id, data_obj) - except Exception: + except redis.RedisError: logger.exception('Could not publish user event') thread = threading.Thread(target=conduct) @@ -87,7 +88,8 @@ class UserEventListener(object): Defines a helper class for subscribing to realtime user events as backed by Redis. """ - def __init__(self, redis_config, username, events=set([])): + def __init__(self, redis_config, username, events=None): + events = events or set([]) channels = [self._user_event_key(username, e) for e in events] try: @@ -114,8 +116,8 @@ class UserEventListener(object): try: data = json.loads(item['data'] or '{}') - except: - pass + except ValueError: + continue if data: yield event_id, data