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

Enforce format of timestamp logs attribute #18218

Merged
merged 2 commits into from
Aug 6, 2024
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/18218.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enforce format of timestamp logs attribute
10 changes: 9 additions & 1 deletion datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,10 @@ def send_log(self, data, cursor=None, stream='default'):
Parameters:

data (dict[str, str]):
The log data to send.
The log data to send. The following keys are treated specially, if present:

- timestamp: should be an integer or float representing the number of seconds since the Unix epoch
- ddtags: if not defined, it will automatically be set based on the instance's `tags` option
cursor (dict[str, Any] or None):
Metadata associated with the log which will be saved to disk. The most recent value may be
retrieved with the `get_log_cursor` method.
Expand All @@ -1012,6 +1015,11 @@ def send_log(self, data, cursor=None, stream='default'):
if 'ddtags' not in attributes and self.formatted_tags:
attributes['ddtags'] = self.formatted_tags

timestamp = attributes.get('timestamp')
if timestamp is not None:
# convert seconds to milliseconds
attributes['timestamp'] = int(timestamp * 1000)

datadog_agent.send_log(to_json(attributes), self.check_id)
if cursor is not None:
self.write_persistent_cache('log_cursor_{}'.format(stream), to_json(cursor))
Expand Down
18 changes: 18 additions & 0 deletions datadog_checks_base/tests/base/checks/test_agent_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,24 @@ def test_stream(self, datadog_agent):
assert check.get_log_cursor(stream='stream1') == {'data': '1'}
assert check.get_log_cursor(stream='stream2') == {'data': '2'}

def test_timestamp(self, datadog_agent):
check = AgentCheck('check_name', {}, [{}])
check.check_id = 'test'

check.send_log({'message': 'foo', 'timestamp': 1722958617.2842212})
check.send_log({'message': 'bar', 'timestamp': 1722958619.2358432})
check.send_log({'message': 'baz', 'timestamp': 1722958620.5963688})

datadog_agent.assert_logs(
check.check_id,
[
{'message': 'foo', 'timestamp': 1722958617284},
{'message': 'bar', 'timestamp': 1722958619235},
{'message': 'baz', 'timestamp': 1722958620596},
],
)
assert check.get_log_cursor() is None


class TestLogsEnabledDetection:
def test_default(self, datadog_agent):
Expand Down
Loading