Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve log messages for when tags aren't utf-8 #2966

Merged
merged 2 commits into from
Jan 17, 2019
Merged
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
30 changes: 20 additions & 10 deletions datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_na
# ignore metric sample
return

tags = self._normalize_tags_type(tags, device_name)
tags = self._normalize_tags_type(tags, device_name, name)
if hostname is None:
hostname = ''

Expand Down Expand Up @@ -319,7 +319,7 @@ def convert_to_underscore_separated(self, name):
metric_name = self.METRIC_REPLACEMENT.sub(br'_', metric_name)
return self.DOT_UNDERSCORE_CLEANUP.sub(br'.', metric_name).strip(b'_')

def _normalize_tags_type(self, tags, device_name=None):
def _normalize_tags_type(self, tags, device_name=None, metric_name=None):
"""
Normalize tags contents and type:
- append `device_name` as `device:` tag
Expand All @@ -338,7 +338,9 @@ def _normalize_tags_type(self, tags, device_name=None):
try:
tag = tag.decode('utf-8')
except UnicodeError:
self.log.warning('Error decoding tag `{}` as utf-8, ignoring tag'.format(tag))
self.log.warning(
'Error decoding tag `{}` as utf-8 for metric `{}`, ignoring tag'.format(tag, metric_name)
)
continue

normalized_tags.append(tag)
Expand Down Expand Up @@ -523,7 +525,7 @@ def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_na
# ignore metric sample
return

tags = self._normalize_tags_type(tags, device_name)
tags = self._normalize_tags_type(tags, device_name, name)
if hostname is None:
hostname = b''

Expand Down Expand Up @@ -670,7 +672,7 @@ def convert_to_underscore_separated(self, name):
metric_name = self.METRIC_REPLACEMENT.sub(br'_', metric_name)
return self.DOT_UNDERSCORE_CLEANUP.sub(br'.', metric_name).strip(b'_')

def _normalize_tags_type(self, tags, device_name=None):
def _normalize_tags_type(self, tags, device_name=None, metric_name=None):
"""
Normalize tags contents and type:
- append `device_name` as `device:` tag
Expand All @@ -683,17 +685,25 @@ def _normalize_tags_type(self, tags, device_name=None):
self._log_deprecation("device_name")
device_tag = self._to_bytes("device:{}".format(device_name))
if device_tag is None:
self.log.warning('Error encoding device tag to utf-8 encoded string, ignoring')
self.log.warning(
'Error encoding device name `{}` to utf-8 for metric `{}`, ignoring tag'.format(
repr(device_name), repr(metric_name)
)
)
else:
normalized_tags.append(device_tag)

if tags is not None:
for tag in tags:
tag = self._to_bytes(tag)
if tag is None:
self.log.warning('Error encoding tag to utf-8 encoded string, ignoring tag')
encoded_tag = self._to_bytes(tag)
if encoded_tag is None:
self.log.warning(
'Error encoding tag `{}` to utf-8 for metric `{}`, ignoring tag'.format(
repr(tag), repr(metric_name)
)
)
continue
normalized_tags.append(tag)
normalized_tags.append(encoded_tag)

return normalized_tags

Expand Down