Last active
February 22, 2016 19:55
-
-
Save cshoe/4995700fbccf43224aa0 to your computer and use it in GitHub Desktop.
Datadog Python library doesn't seem to like Unicode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import (absolute_import, division, print_function, | |
unicode_literals) | |
from datadog import statsd | |
def main(): | |
"""docstring for main""" | |
print('ASCII Tags') | |
ascii_tags = { | |
'person': 'John', | |
'place': 'Mars', | |
'thing': 'burrito' | |
} | |
formatted_tags = format_tags(ascii_tags) | |
statsd.increment('tagged_counter', 1, tags=formatted_tags) | |
print('Unicode Tags') | |
unicode_tags = { | |
'person': 'Jóhn', | |
'place': 'Хельсинки', | |
'thing': 'burrito' | |
} | |
formatted_tags = format_tags(unicode_tags) | |
statsd.increment('tagged_counter', 1, tags=formatted_tags) | |
def format_tags(tags): | |
""" | |
Format tag data for Datadog. | |
:param dict tags: key-value pairs of tag data to include with metric | |
:return: Tags formatted for Datadog. | |
:rtype: list | |
""" | |
dd_tags = [] | |
map(dd_tags.append, ('{}:{}'.format(tag, val) for tag, val in tags.iteritems())) | |
return dd_tags | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python datadog_ascii.py | |
ASCII Tags | |
Unicode Tags | |
Traceback (most recent call last): | |
File "datadog_ascii.py", line 44, in <module> | |
main() | |
File "datadog_ascii.py", line 26, in main | |
formatted_tags = format_tags(unicode_tags) | |
File "datadog_ascii.py", line 39, in format_tags | |
map(dd_tags.append, (b'{}:{}'.format(tag, val) for tag, val in tags.iteritems())) | |
File "datadog_ascii.py", line 39, in <genexpr> | |
map(dd_tags.append, (b'{}:{}'.format(tag, val) for tag, val in tags.iteritems())) | |
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 1: ordinal not in range(128) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment