-
Notifications
You must be signed in to change notification settings - Fork 403
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
feat(logger): add DatadogLogFormatter and observability provider #2183
Merged
leandrodamascena
merged 6 commits into
aws-powertools:develop
from
heitorlessa:logger/datadog-formatter
May 1, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35a3195
feat(logger): add DatadogLogFormatter
heitorlessa 97965da
docs(logger): new observability provider section
heitorlessa e3b4e5a
chore: remove barrel import to avoid Tracer-like situations
heitorlessa ad667d5
fix: correct test import after refactor
heitorlessa dd58bb5
chore: add docstring and note on field change
heitorlessa 3b2fc9a
chore: use keyword-arg in super to ease future refactoring
heitorlessa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Built-in Logger formatters for Observability Providers that require custom config.""" | ||
|
||
# NOTE: we don't expose formatters directly (barrel import) | ||
# as we cannot know if they'll need additional dependencies in the future | ||
# so we isolate to avoid a performance hit and workarounds like lazy imports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Callable | ||
|
||
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter | ||
|
||
|
||
class DatadogLogFormatter(LambdaPowertoolsFormatter): | ||
def __init__( | ||
self, | ||
json_serializer: Callable[[dict], str] | None = None, | ||
json_deserializer: Callable[[dict | str | bool | int | float], str] | None = None, | ||
json_default: Callable[[Any], Any] | None = None, | ||
datefmt: str | None = None, | ||
use_datetime_directive: bool = False, | ||
log_record_order: list[str] | None = None, | ||
utc: bool = False, | ||
use_rfc3339: bool = True, # NOTE: The only change from our base formatter | ||
**kwargs, | ||
): | ||
"""Datadog formatter to comply with Datadog log parsing | ||
|
||
Changes compared to the default Logger Formatter: | ||
|
||
- timestamp format to use RFC3339 e.g., "2023-05-01T15:34:26.841+0200" | ||
|
||
|
||
Parameters | ||
---------- | ||
log_record_order : list[str] | None, optional | ||
_description_, by default None | ||
|
||
Parameters | ||
---------- | ||
json_serializer : Callable, optional | ||
function to serialize `obj` to a JSON formatted `str`, by default json.dumps | ||
json_deserializer : Callable, optional | ||
function to deserialize `str`, `bytes`, bytearray` containing a JSON document to a Python `obj`, | ||
by default json.loads | ||
json_default : Callable, optional | ||
function to coerce unserializable values, by default str | ||
|
||
Only used when no custom JSON encoder is set | ||
|
||
datefmt : str, optional | ||
String directives (strftime) to format log timestamp. | ||
|
||
See https://docs.python.org/3/library/time.html#time.strftime or | ||
use_datetime_directive: str, optional | ||
Interpret `datefmt` as a format string for `datetime.datetime.strftime`, rather than | ||
`time.strftime` - Only useful when used alongside `datefmt`. | ||
|
||
See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This | ||
also supports a custom %F directive for milliseconds. | ||
|
||
log_record_order : list, optional | ||
set order of log keys when logging, by default ["level", "location", "message", "timestamp"] | ||
|
||
utc : bool, optional | ||
set logging timestamp to UTC, by default False to continue to use local time as per stdlib | ||
use_rfc3339: bool, optional | ||
Whether to use a popular dateformat that complies with both RFC3339 and ISO8601. | ||
e.g., 2022-10-27T16:27:43.738+02:00. | ||
kwargs | ||
Key-value to persist in all log messages | ||
""" | ||
super().__init__( | ||
json_serializer=json_serializer, | ||
json_deserializer=json_deserializer, | ||
json_default=json_default, | ||
datefmt=datefmt, | ||
use_datetime_directive=use_datetime_directive, | ||
log_record_order=log_record_order, | ||
utc=utc, | ||
use_rfc3339=use_rfc3339, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
examples/logger/src/observability_provider_builtin_formatters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.logging.formatters.datadog import DatadogLogFormatter | ||
|
||
logger = Logger(service="payment", logger_formatter=DatadogLogFormatter()) | ||
logger.info("hello") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some docstring explaining why this exist and what the small difference is to the base class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're the best! Doing that now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added a specific list so we can easily append if future changes are required - lemme know if that addresses it.