Skip to content

Commit 57b62c4

Browse files
committed
Changed logger level, added "supported_platform" contract, and other small changes
1 parent a78da3a commit 57b62c4

4 files changed

Lines changed: 29 additions & 27 deletions

File tree

snowplow_tracker/emitters.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from contracts import contract, new_contract
3131

3232
logger = logging.getLogger(__name__)
33-
logger.setLevel(logging.DEBUG)
33+
logger.setLevel(logging.INFO)
3434

3535
DEFAULT_MAX_LENGTH = 10
3636
THREAD_TIMEOUT = 10
@@ -101,6 +101,8 @@ def __init__(self, endpoint, protocol="http", port=None, method="get", buffer_si
101101

102102
self.threads = []
103103

104+
logger.info("Emitter initialized with endpoint " + self.endpoint)
105+
104106
@staticmethod
105107
@contract
106108
def as_collector_uri(endpoint, protocol="http", port=None):
@@ -184,7 +186,7 @@ def http_post(self, data):
184186
"""
185187
logger.debug("Sending POST request...")
186188
r = requests.post(self.endpoint, data=data)
187-
logger.debug("POST request finished with status code: " + str(r.status_code))
189+
logger.info("POST request finished with status code: " + str(r.status_code))
188190
return r
189191

190192
@contract
@@ -195,7 +197,7 @@ def http_get(self, payload):
195197
"""
196198
logger.debug("Sending GET request...")
197199
r = requests.get(self.endpoint, params=payload)
198-
logger.debug("GET request finished with status code: " + str(r.status_code))
200+
logger.info("GET request finished with status code: " + str(r.status_code))
199201
return r
200202

201203
def sync_flush(self):
@@ -241,6 +243,7 @@ def flush(self):
241243
Schedules a flush task
242244
"""
243245
super(CeleryEmitter, self).flush.delay()
246+
logger.info("Scheduled a Celery task to flush the event queue")
244247

245248

246249
class RedisEmitter(object):
@@ -268,7 +271,7 @@ def input(self, payload):
268271
"""
269272
logger.debug("Pushing event to Redis queue...")
270273
self.rdb.rpush(self.key, json.dumps(payload))
271-
logger.debug("Finished sending event to Redis.")
274+
logger.info("Finished sending event to Redis.")
272275

273276
def flush(self):
274277
logger.warn("The RedisEmitter class does not need to be flushed")

snowplow_tracker/redis_worker.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@
2323
import redis
2424
import gevent
2525
from gevent.pool import Pool
26-
from consumer import Consumer
2726
import json
2827
import signal
2928

30-
DEFAULT_REDIS = redis.StrictRedis()
3129
DEFAULT_KEY = "snowplow"
3230

3331
class RedisWorker(object):
3432
"""
35-
Asynchronously take events from redis and send them to a consumer
33+
Asynchronously take events from redis and send them to an emitter
3634
"""
3735

38-
def __init__(self, _consumer, key=DEFAULT_KEY, dbr=DEFAULT_REDIS):
39-
self.consumer = _consumer
36+
def __init__(self, emitter, rdb=None, key=DEFAULT_KEY):
37+
self.emitter = emitter
4038
self.key = key
41-
self.dbr = dbr
39+
if rdb is None:
40+
rdb = redis.StrictRedis()
41+
self.rdb = rdb
4242
self.pool = Pool(5)
4343

4444
signal.signal(signal.SIGTERM, self.request_shutdown)
@@ -47,16 +47,16 @@ def __init__(self, _consumer, key=DEFAULT_KEY, dbr=DEFAULT_REDIS):
4747

4848
def send(self, payload):
4949
"""
50-
Send an event to a consumer
50+
Send an event to an emitter
5151
"""
52-
self.consumer.input(payload)
52+
self.emitter.input(payload)
5353

5454
def pop_payload(self):
5555
"""
5656
Get a single event from Redis and send it
5757
If the Redis queue is empty, sleep to avoid making continual requests
5858
"""
59-
payload = self.dbr.lpop(self.key)
59+
payload = self.rdb.lpop(self.key)
6060
if payload:
6161
self.pool.spawn(self.send, json.loads(payload.decode("utf-8")))
6262
else:

snowplow_tracker/subject.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
from contracts import contract, new_contract
2323

24-
new_contract("subject", lambda x: isinstance(x, Subject))
25-
2624
SUPPORTED_PLATFORMS = set(["pc", "tv", "mob", "cnsl", "iot"])
2725
DEFAULT_PLATFORM = "pc"
2826

27+
new_contract("subject", lambda x: isinstance(x, Subject))
28+
29+
new_contract("supported_platform", lambda x: x in SUPPORTED_PLATFORMS)
30+
2931
class Subject(object):
3032
"""
3133
Class for an event subject, where we view events as of the form
@@ -40,13 +42,10 @@ def __init__(self):
4042
def set_platform(self, value):
4143
"""
4244
:param value: One of ["pc", "tv", "mob", "cnsl", "iot"]
43-
:type value: string
45+
:type value: supported_platform
4446
:rtype: subject
4547
"""
46-
if value in SUPPORTED_PLATFORMS:
47-
self.standard_nv_pairs["p"] = value
48-
else:
49-
raise RuntimeError(value + " is not a supported platform")
48+
self.standard_nv_pairs["p"] = value
5049
return self
5150

5251
@contract

snowplow_tracker/tracker.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class Tracker:
5454
new_contract("emitter", lambda s: hasattr(s, "input"))
5555

5656
@contract
57-
def __init__(self, out_queue, _subject=None,
57+
def __init__(self, emitter, _subject=None,
5858
namespace=None, app_id=None, encode_base64=DEFAULT_ENCODE_BASE64):
5959
"""
60-
:param out_queue: Emitter to which events will be sent
61-
:type out_queue: emitter
60+
:param emitter: Emitter to which events will be sent
61+
:type emitter: emitter
6262
:param _subject: Subject to be tracked
6363
:type _subject: subject | None
6464
:param namespace: Identifier for the Tracker instance
@@ -71,7 +71,7 @@ def __init__(self, out_queue, _subject=None,
7171
if _subject is None:
7272
_subject = subject.Subject()
7373

74-
self.out_queue = out_queue
74+
self.emitter = emitter
7575
self.subject = _subject
7676
self.encode_base64 = encode_base64
7777

@@ -121,7 +121,7 @@ def track(self, pb):
121121
:type pb: payload
122122
:rtype: tracker | int
123123
"""
124-
result = self.out_queue.input(pb.nv_pairs)
124+
result = self.emitter.input(pb.nv_pairs)
125125
if result is not None:
126126
return result
127127
else:
@@ -357,10 +357,10 @@ def flush(self, async=False):
357357
:rtype: tracker | int
358358
"""
359359
if async:
360-
self.out_queue.flush()
360+
self.emitter.flush()
361361
return self
362362
else:
363-
return self.out_queue.sync_flush()
363+
return self.emitter.sync_flush()
364364

365365
@contract
366366
def set_subject(self, _subject):

0 commit comments

Comments
 (0)