Merge pull request #2284 from coreos-inc/better-tracking
Small fixes around tracking in the registry
This commit is contained in:
commit
64a6f10431
2 changed files with 18 additions and 14 deletions
|
@ -1,8 +1,9 @@
|
||||||
import redis
|
|
||||||
import json
|
import json
|
||||||
import threading
|
import threading
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import redis
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class CannotReadUserEventsException(Exception):
|
class CannotReadUserEventsException(Exception):
|
||||||
|
@ -56,7 +57,7 @@ class UserEvent(object):
|
||||||
as backed by Redis.
|
as backed by Redis.
|
||||||
"""
|
"""
|
||||||
def __init__(self, redis_config, username):
|
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
|
self._username = username
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -75,7 +76,7 @@ class UserEvent(object):
|
||||||
try:
|
try:
|
||||||
self.publish_event_data_sync(event_id, data_obj)
|
self.publish_event_data_sync(event_id, data_obj)
|
||||||
logger.debug('Published user event %s: %s', 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')
|
logger.exception('Could not publish user event')
|
||||||
|
|
||||||
thread = threading.Thread(target=conduct)
|
thread = threading.Thread(target=conduct)
|
||||||
|
@ -87,7 +88,8 @@ class UserEventListener(object):
|
||||||
Defines a helper class for subscribing to realtime user events as
|
Defines a helper class for subscribing to realtime user events as
|
||||||
backed by Redis.
|
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]
|
channels = [self._user_event_key(username, e) for e in events]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -114,8 +116,8 @@ class UserEventListener(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(item['data'] or '{}')
|
data = json.loads(item['data'] or '{}')
|
||||||
except:
|
except ValueError:
|
||||||
pass
|
continue
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
yield event_id, data
|
yield event_id, data
|
||||||
|
|
|
@ -56,16 +56,9 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1,
|
||||||
metadata['public'] = True
|
metadata['public'] = True
|
||||||
analytics_id = 'anonymous'
|
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)
|
# Publish the user event (if applicable)
|
||||||
logger.debug('Checking publishing %s to the user events system', event_name)
|
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)
|
logger.debug('Publishing %s to the user events system', event_name)
|
||||||
user_event_data = {
|
user_event_data = {
|
||||||
'action': event_name,
|
'action': event_name,
|
||||||
|
@ -80,7 +73,16 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1,
|
||||||
if random.random() < analytics_sample:
|
if random.random() < analytics_sample:
|
||||||
if analytics_name is None:
|
if analytics_name is None:
|
||||||
analytics_name = event_name
|
analytics_name = event_name
|
||||||
|
|
||||||
logger.debug('Logging the %s to Mixpanel', analytics_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)
|
analytics.track(analytics_id, analytics_name, extra_params)
|
||||||
|
|
||||||
# Log the action to the database.
|
# Log the action to the database.
|
||||||
|
|
Reference in a new issue