-
Notifications
You must be signed in to change notification settings - Fork 28
Cache and expose extractors #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
Open
nuno-andre
wants to merge
3
commits into
elastic:main
Choose a base branch
from
nuno-andre:expose-extractors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -30,12 +30,7 @@ | |
merge_dicts, | ||
) | ||
|
||
from typing import Any, Callable, Dict, Optional, Sequence, Union | ||
|
||
try: | ||
from typing import Literal # type: ignore | ||
except ImportError: | ||
from typing_extensions import Literal # type: ignore | ||
from typing import Any, Callable, Dict, Optional, Sequence, Union, Literal | ||
|
||
|
||
# Load the attributes of a LogRecord so if some are | ||
|
@@ -164,21 +159,25 @@ def format(self, record: logging.LogRecord) -> str: | |
result = self.format_to_ecs(record) | ||
return json_dumps(result) | ||
|
||
def format_to_ecs(self, record: logging.LogRecord) -> Dict[str, Any]: | ||
"""Function that can be overridden to add additional fields to | ||
(or remove fields from) the JSON before being dumped into a string. | ||
@property | ||
@lru_cache | ||
def extractors(self) -> Dict[str, Callable[[logging.LogRecord], Any]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not very inclined to add a new public property, no objection in creating a private |
||
"""Property that can be overridden to add additional field | ||
extractors to (or remove fields from) the JSON before being | ||
dumped into a string. | ||
|
||
.. code-block: python | ||
|
||
class MyFormatter(StdlibFormatter): | ||
def format_to_ecs(self, record): | ||
result = super().format_to_ecs(record) | ||
del result["log"]["original"] # remove unwanted field(s) | ||
result["my_field"] = "my_value" # add custom field | ||
return result | ||
@property | ||
@lru_cache | ||
def extractors(self): | ||
extractors = super().extractors | ||
del extractors["log.original"] # remove unwanted field(s) | ||
extractors["my_field"] = self._my_extractor # add custom field | ||
return extractors | ||
""" | ||
|
||
extractors: Dict[str, Callable[[logging.LogRecord], Any]] = { | ||
return { | ||
"@timestamp": self._record_timestamp, | ||
"ecs.version": lambda _: ECS_VERSION, | ||
"log.level": lambda r: (r.levelname.lower() if r.levelname else None), | ||
|
@@ -196,11 +195,24 @@ def format_to_ecs(self, record): | |
"error.stack_trace": self._record_error_stack_trace, | ||
} | ||
|
||
def format_to_ecs(self, record: logging.LogRecord) -> Dict[str, Any]: | ||
"""Function that can be overridden to add additional fields to | ||
(or remove fields from) the JSON before being dumped into a string. | ||
|
||
.. code-block: python | ||
|
||
class MyFormatter(StdlibFormatter): | ||
def format_to_ecs(self, record): | ||
result = super().format_to_ecs(record) | ||
del result["log"]["original"] # remove unwanted field(s) | ||
result["my_field"] = "my_value" # add custom field | ||
return result | ||
""" | ||
result: Dict[str, Any] = {} | ||
for field in set(extractors.keys()).difference(self._exclude_fields): | ||
for field in set(self.extractors.keys()).difference(self._exclude_fields): | ||
if self._is_field_excluded(field): | ||
continue | ||
value = extractors[field](record) | ||
value = self.extractors[field](record) | ||
if value is not None: | ||
# special case ecs.version that should not be de-dotted | ||
if field == "ecs.version": | ||
|
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.
Please drop these changes, we can do them in another PR