-
Notifications
You must be signed in to change notification settings - Fork 703
Add support for user defined attributes in OTLPHandler #1952
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
Changes from all commits
4c6d5e9
28632ca
93e0037
db57083
826b02a
231fb16
ea248c2
dd2b3dc
5765374
248ac56
f8f4876
e0d3b5e
539df29
2a4ed23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,20 +244,59 @@ def force_flush(self, timeout_millis: int = 30000) -> bool: | |
return True | ||
|
||
|
||
# skip natural LogRecord attributes | ||
# http://docs.python.org/library/logging.html#logrecord-attributes | ||
_RESERVED_ATTRS = frozenset( | ||
( | ||
"asctime", | ||
"args", | ||
"created", | ||
"exc_info", | ||
"exc_text", | ||
"filename", | ||
"funcName", | ||
"getMessage", | ||
"levelname", | ||
"levelno", | ||
"lineno", | ||
"module", | ||
"msecs", | ||
"msg", | ||
"name", | ||
"pathname", | ||
"process", | ||
"processName", | ||
srikanthccv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"relativeCreated", | ||
"stack_info", | ||
"thread", | ||
"threadName", | ||
) | ||
) | ||
|
||
|
||
class OTLPHandler(logging.Handler): | ||
"""A handler class which writes logging records, in OTLP format, to | ||
a network destination or file. | ||
""" | ||
|
||
def __init__(self, level=logging.NOTSET, log_emitter=None) -> None: | ||
def __init__( | ||
self, | ||
level=logging.NOTSET, | ||
log_emitter=None, | ||
) -> None: | ||
super().__init__(level=level) | ||
self._log_emitter = log_emitter or get_log_emitter(__name__) | ||
|
||
@staticmethod | ||
def _get_attributes(record: logging.LogRecord) -> Attributes: | ||
return { | ||
k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS | ||
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. Is 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. Yes, that's how the stdlib logging library works. Documentation: https://docs.python.org/3/library/logging.html#logging.Logger.debug (paragraph 2) |
||
} | ||
|
||
def _translate(self, record: logging.LogRecord) -> LogRecord: | ||
timestamp = int(record.created * 1e9) | ||
span_context = get_current_span().get_span_context() | ||
# TODO: attributes (or resource attributes?) from record metadata | ||
attributes: Attributes = {} | ||
attributes = self._get_attributes(record) | ||
severity_number = std_to_otlp(record.levelno) | ||
return LogRecord( | ||
timestamp=timestamp, | ||
|
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.
We are skipping now to separate the user-defined extra attributes but we will revisit this later if needed.