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

Encode hostname in set_external_tags #3866

Merged
merged 1 commit into from
Jun 5, 2019
Merged
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
14 changes: 9 additions & 5 deletions datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..config import is_affirmative
from ..constants import ServiceCheck
from ..utils.agent.debug import enter_pdb
from ..utils.common import ensure_bytes, ensure_unicode
from ..utils.common import ensure_bytes, ensure_unicode, to_string
from ..utils.http import RequestsWrapper
from ..utils.limiter import Limiter
from ..utils.proxy import config_proxy_skip
Expand Down Expand Up @@ -333,10 +333,12 @@ def set_external_tags(self, external_tags):
# ('hostname2', {'src2_name': ['test2:t3']})
# ]
try:
for _, source_map in external_tags:
new_tags = []
Copy link
Contributor

Choose a reason for hiding this comment

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

Didnt @masci factor this out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're waiting for 6.12 to release to merge it IIUC.

for hostname, source_map in external_tags:
new_tags.append((to_string(hostname), source_map))
for src_name, tags in iteritems(source_map):
source_map[src_name] = self._normalize_tags_type(tags)
datadog_agent.set_external_tags(external_tags)
datadog_agent.set_external_tags(new_tags)
except IndexError:
self.log.exception('Unexpected external tags format: {}'.format(external_tags))
raise
Expand Down Expand Up @@ -790,10 +792,12 @@ def set_external_tags(self, external_tags):
# ]

try:
for _, source_map in external_tags:
new_tags = []
for hostname, source_map in external_tags:
new_tags.append((to_string(hostname), source_map))
for src_name, tags in iteritems(source_map):
source_map[src_name] = self._normalize_tags_type(tags)
datadog_agent.set_external_tags(external_tags)
datadog_agent.set_external_tags(new_tags)
except IndexError:
self.log.exception('Unexpected external tags format: {}'.format(external_tags))
raise
Expand Down
11 changes: 11 additions & 0 deletions datadog_checks_base/tests/test_agent_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from six import PY3

from datadog_checks.base import AgentCheck
from datadog_checks.base.checks.base import datadog_agent


def test_instance():
Expand Down Expand Up @@ -267,6 +268,16 @@ def test_external_host_tag_normalization(self):
check.set_external_tags(external_host_tags)
assert external_host_tags == [('hostname', {'src_name': ['normalize:tag']})]

def test_external_hostname(self):
check = AgentCheck()
external_host_tags = [(u'hostnam\xe9', {'src_name': ['key1:val1']})]
with mock.patch.object(datadog_agent, 'set_external_tags') as set_external_tags:
check.set_external_tags(external_host_tags)
if PY3:
set_external_tags.assert_called_with([(u'hostnam\xe9', {'src_name': ['key1:val1']})])
else:
set_external_tags.assert_called_with([('hostnam\xc3\xa9', {'src_name': ['key1:val1']})])


class LimitedCheck(AgentCheck):
DEFAULT_METRIC_LIMIT = 10
Expand Down
2 changes: 1 addition & 1 deletion datadog_checks_base/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ deps =
-rrequirements-dev.txt
commands =
pip install -r requirements.in
pytest -v
pytest -v {posargs}