From ccbd94dabdb0a70bd6876437c0e161ef9dfb546f Mon Sep 17 00:00:00 2001 From: Yann MAHE Date: Wed, 24 Aug 2016 15:12:32 -0400 Subject: [PATCH] =?UTF-8?q?[dogstatsd]=20support=20unicode=20tags=20?= =?UTF-8?q?=F0=9F=94=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using unicode with DogStatsD tags causes `UnicodeDecodeError` exceptions. Do not raise on unicode tags. Fix #132. --- datadog/dogstatsd/base.py | 13 ++++++++----- datadog/util/compat.py | 4 ++++ tests/unit/dogstatsd/test_statsd.py | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/datadog/dogstatsd/base.py b/datadog/dogstatsd/base.py index 25d509c49..6205cdaa4 100644 --- a/datadog/dogstatsd/base.py +++ b/datadog/dogstatsd/base.py @@ -2,20 +2,23 @@ """ DogStatsd is a Python client for DogStatsd, a Statsd fork for Datadog. """ - -import logging -import os +# stdlib +from functools import wraps from random import random from time import time +import logging +import os import socket import struct -from functools import wraps try: from itertools import imap except ImportError: imap = map +# datadog +from datadog.util.compat import text + log = logging.getLogger('dogstatsd') @@ -289,7 +292,7 @@ def _report(self, metric, metric_type, value, tags, sample_rate): if tags: payload.extend(["|#", ",".join(tags)]) - encoded = "".join(imap(str, payload)) + encoded = "".join(imap(text, payload)) # Send it self._send(encoded) diff --git a/datadog/util/compat.py b/datadog/util/compat.py index aa4cb2840..003908cd9 100644 --- a/datadog/util/compat.py +++ b/datadog/util/compat.py @@ -24,6 +24,8 @@ def iteritems(d): def iternext(iter): return next(iter) + text = str + else: get_input = raw_input import ConfigParser as configparser @@ -36,6 +38,8 @@ def iteritems(d): def iternext(iter): return iter.next() + text = unicode + try: from UserDict import IterableUserDict diff --git a/tests/unit/dogstatsd/test_statsd.py b/tests/unit/dogstatsd/test_statsd.py index 13992ca65..112da7a4b 100644 --- a/tests/unit/dogstatsd/test_statsd.py +++ b/tests/unit/dogstatsd/test_statsd.py @@ -103,8 +103,8 @@ def test_tagged_gauge(self): t.assert_equal('gt:123.4|g|#country:china,age:45,blue', self.recv()) def test_tagged_counter(self): - self.statsd.increment('ct', tags=['country:canada', 'red']) - t.assert_equal('ct:1|c|#country:canada,red', self.recv()) + self.statsd.increment('ct', tags=[u'country:españa', 'red']) + t.assert_equal(u'ct:1|c|#country:españa,red', self.recv()) def test_tagged_histogram(self): self.statsd.histogram('h', 1, tags=['red'])