Skip to content

Only replace the logging formatter if it is a logging.Formatter #155

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

Merged
merged 2 commits into from
Jul 23, 2021

Conversation

agocs
Copy link
Contributor

@agocs agocs commented Jul 23, 2021

What does this PR do?

Makes the trace context log injection code check the type of the logging Formatter before replacing the Formatter. If the user is using a JSON formatter, it will pick up the trace context from the dummy span that contains the trace context.

Motivation

#153

Testing Guidelines

Screenshots of it working:

(JSON logging, no lambda library)
image

(JSON logging, old version of the lambda library)
image

(JSON logging, new version of the lambda library)
image

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@agocs agocs requested a review from a team as a code owner July 23, 2021 19:28
@agocs agocs changed the title Only replace the logging handler if it is using a logging.Formatter Only replace the logging formatter if it is a logging.Formatter Jul 23, 2021
@nhinsch
Copy link
Contributor

nhinsch commented Jul 23, 2021

If the user is using a JSON formatter, it will pick up the trace context from the dummy span that contains the trace context.

Can you elaborate on this?

@agocs
Copy link
Contributor Author

agocs commented Jul 23, 2021

Can you elaborate on this?

The Python lambda library creates a dummy span that contains the trace context in https://github.com/DataDog/datadog-lambda-python/pull/155/files#diff-eb1f51efb218a7a829e913e4ff6b17ce27be8a777bc20c283e5b843e5db151efL304-L325

If you are logging using the json-log-formatter as recommended in the logs_collection/python docs, that trace context is added to the logs automatically. The logging.Formatter that is created on line 341/344 gets the logs context from that dummy span (or, I suppose, the actual span, but it's the same thing either way).

Copy link
Contributor

@nhinsch nhinsch left a comment

Choose a reason for hiding this comment

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

LGTM

@agocs agocs merged commit e3c0a45 into main Jul 23, 2021
@agocs agocs deleted the chris.agocs/dont_replace_json_formatters branch July 23, 2021 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants