Skip to content

Commit

Permalink
[dogstatsd] support unicode tags 🔣
Browse files Browse the repository at this point in the history
Using unicode with DogStatsD tags causes `UnicodeDecodeError`
exceptions.

Do not raise on unicode tags. Fix #132.
  • Loading branch information
yannmh committed Aug 24, 2016
1 parent be01e58 commit ccbd94d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
13 changes: 8 additions & 5 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions datadog/util/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def iteritems(d):
def iternext(iter):
return next(iter)

text = str

else:
get_input = raw_input
import ConfigParser as configparser
Expand All @@ -36,6 +38,8 @@ def iteritems(d):
def iternext(iter):
return iter.next()

text = unicode


try:
from UserDict import IterableUserDict
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down

0 comments on commit ccbd94d

Please sign in to comment.