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

Add each checks' unique ID to logs #4410

Merged
merged 4 commits into from
Aug 23, 2019
Merged

Add each checks' unique ID to logs #4410

merged 4 commits into from
Aug 23, 2019

Conversation

ofek
Copy link
Contributor

@ofek ofek commented Aug 22, 2019

Motivation

The ability to correlate a log entry with a specific check instance

Output

Capture

Capture

@codecov
Copy link

codecov bot commented Aug 22, 2019

Codecov Report

Merging #4410 into master will decrease coverage by 1.94%.
The diff coverage is 76.92%.

Impacted Files Coverage Δ
datadog_checks_base/datadog_checks/base/log.py 68.85% <71.42%> (+4.14%) ⬆️
...dog_checks_base/datadog_checks/base/checks/base.py 75.22% <90.9%> (+0.62%) ⬆️
datadog_checks_base/tests/conftest.py 71.42% <0%> (-28.58%) ⬇️
...atadog_checks_base/datadog_checks/base/__init__.py 73.33% <0%> (-13.34%) ⬇️
datadog_checks_base/tests/test_winpdh.py 90.69% <0%> (-6.98%) ⬇️
datadog_checks_base/tests/test_pdhbasecheck.py 96% <0%> (-4%) ⬇️
datadog_checks_base/tests/test_http.py 97.33% <0%> (-0.89%) ⬇️
...checks_dev/tests/plugin/test_environment_runner.py
riakcs/datadog_checks/riakcs/riakcs.py
mesos_slave/tests/conftest.py
... and 721 more

vsphere/tox.ini Outdated Show resolved Hide resolved
Copy link
Contributor

@mfpierre mfpierre left a comment

Choose a reason for hiding this comment

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

the logic LGTM overall 👍

# the agent5 'AgentCheck' setup a log attribute.
self.log = logging.getLogger('{}.{}'.format(__name__, self.name))
# We need to load this lazily because the Agent sets `check_id` after instantiation
self._log = None
Copy link
Member

@AlexandreYang AlexandreYang Aug 22, 2019

Choose a reason for hiding this comment

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

Seems in this case we could use a logging.Filter instead. Here is an example from the official cookbook:

Using Filters to impart contextual information
You can also add contextual information to log output using a user-defined Filter. Filter instances are allowed to modify the LogRecords passed to them, including adding additional attributes which can then be output using a suitable format string, or if needed a custom Formatter.

https://docs.python.org/3/howto/logging-cookbook.html#adding-contextual-information-to-your-logging-output

That could avoid having to create our own CheckLogger and overriding logging methods (info/critical/warn/debug/etc), IMHO should be avoided if possible.

import logging

class ContextFilter(logging.Filter):
    def __init__(self, check_id):
        super(ContextFilter).__init__()
        self._check_id = check_id

    def filter(self, record):

        record._check_id = self._check_id or "unknown"
        return True

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)-15s %(name)-5s %(levelname)-8s CheckID: %(_check_id)-15s %(message)s')
    log = logging.getLogger('my logger')

    my_check_id = 1234

    log.addFilter(ContextFilter(my_check_id)) # we can also pass the check instance here if needed
    log.debug('A debug message')


# output
# 2019-08-22 10:51:00,563 my logger DEBUG    CheckID: 1234            A debug message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That won't work https://docs.python.org/3/library/logging.html#logging.getLogger

All calls to this function with a given name return the same logger instance.

self.log = logging.getLogger('{}.{}'.format(__name__, self.name))

Copy link
Member

@AlexandreYang AlexandreYang Aug 23, 2019

Choose a reason for hiding this comment

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

How about this by using the logging.LoggerAdapter ? :)

https://gist.github.com/AlexandreYang/c518db4380f2de53d644f17d29523ea8

@AlexandreYang AlexandreYang self-requested a review August 22, 2019 13:08
Copy link
Member

@AlexandreYang AlexandreYang left a comment

Choose a reason for hiding this comment

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

[msg deleted]

Copy link
Member

@AlexandreYang AlexandreYang left a comment

Choose a reason for hiding this comment

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

Looks good, 👍

Just few comments.

This part is not critical, but few tests to check that the _check_id is correctly transmitted could help. But up to you :)

@@ -15,6 +15,27 @@ def critical(self, msg, *args, **kwargs):
raise NotImplementedError('The critical log level is reserved for agent shutdowns.')


class CheckLoggingAdapter(logging.LoggerAdapter):
Copy link
Member

Choose a reason for hiding this comment

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

Should we inherit from datadog_checks_base/datadog_checks/base/log.py#CheckLoggingAdapter to reduce duplication ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No since stubs are meant to be isolated

@ofek ofek merged commit cd61523 into master Aug 23, 2019
@ofek ofek deleted the ofek/l branch August 23, 2019 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants