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

Testing: Add type annotations to common/logging #6714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rdimaio
Copy link
Contributor

@rdimaio rdimaio commented Apr 22, 2024

Part of #6588

On the ECS_FIELDS and LOG_RECORDS change, here's an execution of that code to show that the final BUILTIN_FIELDS is the same as before:

>>> from typing import TYPE_CHECKING, Any, Literal, Optional, Union, get_args
>>> ECS_FIELDS = Literal[
...     '@timestamp',
...     'message',
...     'log.level',
...     'log.origin.function',
...     'log.origin.file.line',
...     'log.origin.file.name',
...     'log.logger',
...     'process.pid',
...     'process.name',
...     'process.thread.id',
...     'process.thread.name'
... ]
>>>
>>> LOG_RECORDS = Literal[
...     'asctime',
...     'message',
...     'levelname',
...     'funcName',
...     'lineno',
...     'filename',
...     'name',
...     'process',
...     'processName',
...     'thread',
...     'threadName'
... ]
>>>
>>> BUILTIN_FIELDS = tuple((x, y) for x,y in zip(get_args(ECS_FIELDS), get_args(LOG_RECORDS)))
>>> BUILTIN_FIELDS
(('@timestamp', 'asctime'), ('message', 'message'), ('log.level', 'levelname'), ('log.origin.function', 'funcName'), ('log.origin.file.line', 'lineno'), ('log.origin.file.name', 'filename'), ('log.logger', 'name'), ('process.pid', 'process'), ('process.name', 'processName'), ('process.thread.id', 'thread'), ('process.thread.name', 'threadName'))

"""
Sanitize the path-like field name into a symbol which can be the name of an object attribute.
"""
record = ECS_TO_LOG_RECORD_MAP.get(field_name)
record = ECS_TO_LOG_RECORD_MAP.get(field_name) # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type ignore here is to ignore the error that occurs when a str that's not in ECS_FIELDS is passed to _ecs_field_to_record_attribute, in which case the function returns field_name.replace('-', '_').replace('.', '_').

I personally don't like this logic being in this function, as the function name doesn't say anything about replacing those symbols within a string, and from the function name _ecs_field_to_record_attribute you would assume that this only takes in ECS_FIELDS and returns LOG_RECORDS.

I think it would be a good idea to change the way this works in future.

def format(self, record):
json_record = dict(itertools.chain.from_iterable(f.format(record) for f in self._desired_data_sources))
def format(self, record: "LogRecord") -> str:
json_record = dict(itertools.chain.from_iterable(f.format(record) for f in self._desired_data_sources)) # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type: ignore here is due to the fact that LogDataSource.format returns None if there is no self._formatter, and the static type checker is not able to guarantee that f has a _formatter specified in this case.

This logic should likely be changed so that it only runs LogDataSource.format if _formatter exists, and otherwise returns None, but I think it's best to justtype: ignore for now and address this later.

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.

None yet

1 participant