Skip to content
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
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/22973.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix process_isolation parameter to handle log formats and external tags.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def _kill_on_timeout():
elif message_type == 'datadog_agent':
method = message['method']
value = getattr(datadog_agent, method)(*message['args'], **message['kwargs'])
args = message['args']
if method == 'set_external_tags':
args = [[tuple(item) for item in args[0]]]
value = getattr(datadog_agent, method)(*args, **message['kwargs'])
if method not in KNOWN_DATADOG_AGENT_SETTER_METHODS:
try:
process.stdin.write(b'%s\n' % ensure_bytes(json.encode({'value': value})))
Expand All @@ -110,7 +114,6 @@ def _kill_on_timeout():
message_type,
)
break

if timed_out:
check.log.error('Check timed out after %s seconds', timeout)
check.warning('Check timed out and possibly reported incomplete data.')
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ def method(*args, **kwargs):


class ReplayLogger(logging.Logger):
def log(self, level, *args, **kwargs):
def log(self, level, msg, *args, **kwargs):
if args:
try:
msg = msg % args
except (TypeError, ValueError, OverflowError, KeyError):
return
print(
'{}:log:{}'.format(
MESSAGE_INDICATOR,
to_native_string(json.encode({'method': LOG_METHODS[level], 'args': [str(a) for a in args]})),
to_native_string(json.encode({'method': LOG_METHODS[level], 'args': [str(msg)]})),
)
)

Expand Down
36 changes: 30 additions & 6 deletions datadog_checks_base/tests/base/utils/replay/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def initialize(self):
def check(self, _):
self.gauge('metric', 0, tags=self.tags)
self.service_check('sc', ServiceCheck.OK if self.redirecting else ServiceCheck.CRITICAL, tags=self.tags)
self.log.debug('Metric count: %d', 42)
self.log.debug('Test log: %s', 'test')
self.set_external_tags([('myhost', {'src': ['tag:val']})])


@pytest.mark.parametrize(
Expand Down Expand Up @@ -56,12 +59,33 @@ def test_replay_all(caplog, dd_run_check, aggregator, datadog_agent, init_config
aggregator.assert_service_check('replay.sc', ServiceCheck.OK, count=1, tags=expected_tags)
aggregator.assert_all_metrics_covered()

expected_message = 'Initializing - replay - test:123'
for _, level, message in caplog.record_tuples:
if level == logging.DEBUG and message == expected_message:
break
else:
raise AssertionError('Expected DEBUG log with message: {}'.format(expected_message))
captured_logs = {(level, message) for _, level, message in caplog.record_tuples}
assert (logging.DEBUG, 'Initializing - replay - test:123') in captured_logs
assert (logging.DEBUG, 'Metric count: 42') in captured_logs

datadog_agent.assert_external_tags('myhost', {'src': ['tag:val']})
datadog_agent.assert_external_tags_count(1)


class ReplayCheckBadLog(AgentCheck):
__NAMESPACE__ = 'replay'

def check(self, _):
self.log.debug('TypeError format: %d', 'not_a_number')
self.log.debug('OverflowError format: %c', 2**32)


def test_replay_skips_invalid_log_format(caplog, dd_run_check, aggregator, datadog_agent):
datadog_agent._config['log_level'] = 'debug'

check = ReplayCheckBadLog('replay', {}, [{'process_isolation': True}])
check.check_id = 'test:123'

with caplog.at_level(logging.DEBUG):
dd_run_check(check)

assert 'TypeError format' not in caplog.text
assert 'OverflowError format' not in caplog.text


class SlowReplayCheck(AgentCheck):
Expand Down
Loading