Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion snowplow_tracker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from snowplow_tracker._version import __version__
from snowplow_tracker.subject import Subject
from snowplow_tracker.emitters import logger, Emitter, AsyncEmitter, CeleryEmitter, RedisEmitter
from snowplow_tracker.emitters import Emitter, AsyncEmitter, CeleryEmitter, RedisEmitter
from snowplow_tracker.self_describing_json import SelfDescribingJson
from snowplow_tracker.tracker import Tracker
from contracts import disable_all as disable_contracts, enable_all as enable_contracts
import logging

# Set default logging handler to avoid "No handler found" warnings.
logging.getLogger(__name__).addHandler(logging.NullHandler())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change the existing logging?


12 changes: 0 additions & 12 deletions snowplow_tracker/emitters.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
from snowplow_tracker.self_describing_json import SelfDescribingJson

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

DEFAULT_MAX_LENGTH = 10
PAYLOAD_DATA_SCHEMA = "iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4"
Expand All @@ -52,16 +51,6 @@

new_contract("redis", lambda x: isinstance(x, (redis.Redis, redis.StrictRedis)))

try:
# Check whether a custom Celery configuration module named "snowplow_celery_config" exists
import snowplow_celery_config
app = Celery()
app.config_from_object(snowplow_celery_config)

except ImportError:
# Otherwise configure Celery with default settings
app = Celery("Snowplow", broker="redis://guest@localhost//")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the motivation behind this?



class Emitter(object):
"""
Expand Down Expand Up @@ -172,7 +161,6 @@ def reached_limit(self):
else:
return self.bytes_queued >= self.byte_limit or len(self.buffer) >= self.buffer_size

@task(name="Flush")
def flush(self):
"""
Sends all events in the buffer to the collector.
Expand Down
12 changes: 6 additions & 6 deletions snowplow_tracker/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ def add_json(self, dict_, encode_base64, type_when_encoded, type_when_not_encode
"""

if dict_ is not None and dict_ != {}:

json_dict = json.dumps(dict_, ensure_ascii=False)

if encode_base64:
encoded_dict = base64.urlsafe_b64encode(json_dict.encode("ascii"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR doesn't solve the issue #194 in my local tests

Simply changing the encoding on this line to utf-8 does the trick

if not isinstance(encoded_dict, str):
try:
encoded_dict = base64.urlsafe_b64encode(json_dict)
if not isinstance(encoded_dict, str):
encoded_dict = encoded_dict.decode("utf-8")
except UnicodeDecodeError:
encoded_dict = encoded_dict.decode("utf-8")
self.add(type_when_encoded, encoded_dict)

self.add(type_when_encoded, encoded_dict)
else:
self.add(type_when_not_encoded, json_dict)

Expand Down
18 changes: 18 additions & 0 deletions snowplow_tracker/test/unit/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,21 @@ def test_add_dict(self):
p.add_dict({"name4": 4, "name3": 3}) # Order doesn't matter
output = {"n1": "v1", "n2": "v2", "name3": 3, "name4": 4}
self.assertTrue(is_subset(output, p.nv_pairs))

def test_add_json_encode_base64(self):
p = payload.Payload()
p.add_json({"name5": 5, "name6": 6}, True, "Encoded_type", "Not_encoded_type")
output = {'Encoded_type': 'eyJuYW1lNiI6IDYsICJuYW1lNSI6IDV9'}
self.assertDictEqual(output, p.nv_pairs)

def test_add_json_not_encode_base64(self):
p = payload.Payload()
p.add_json({"name7": 7, "name8": 8}, False, "Encoded_type", "Not_encoded_type")
output = {'Not_encoded_type': '{"name7": 7, "name8": 8}'}
self.assertDictEqual(output, p.nv_pairs)

def test_add_json_with_emoji(self):
p = payload.Payload()
p.add_json({"name9": "\xF0\xe0\xe7\x82\xf1\U0001f44d", "name10": 10}, True, "Encoded_type", "Not_encoded_type")
output = {'Encoded_type': 'eyJuYW1lMTAiOiAxMCwgIm5hbWU5IjogIvDg54LxXFxVMDAwMWY0NGQifQ=='}
self.assertDictEqual(output, p.nv_pairs)
2 changes: 1 addition & 1 deletion snowplow_tracker/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"""


class Tracker:
class Tracker(object):

new_contract("not_none", lambda s: s is not None)

Expand Down