Skip to content

Commit

Permalink
blacken
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Jul 14, 2021
1 parent 4c6d5e9 commit 28632ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
42 changes: 35 additions & 7 deletions opentelemetry-sdk/src/opentelemetry/sdk/logs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,31 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:

# skip natural LogRecord attributes
# http://docs.python.org/library/logging.html#logrecord-attributes
_RESERVED_ATTRS = frozenset((
'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename',
'funcName', 'levelname', 'levelno', 'lineno', 'module',
'msecs', 'message', 'msg', 'name', 'pathname', 'process',
'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName')
_RESERVED_ATTRS = frozenset(
(
"args",
"asctime",
"created",
"exc_info",
"exc_text",
"filename",
"funcName",
"levelname",
"levelno",
"lineno",
"module",
"msecs",
"message",
"msg",
"name",
"pathname",
"process",
"processName",
"relativeCreated",
"stack_info",
"thread",
"threadName",
)
)


Expand All @@ -259,15 +279,23 @@ class OTLPHandler(logging.Handler):
a network destination or file.
"""

def __init__(self, level=logging.NOTSET, log_emitter=None, *, attributes_key: Optional[str] = None) -> None:
def __init__(
self,
level=logging.NOTSET,
log_emitter=None,
*,
attributes_key: Optional[str] = None,
) -> None:
super().__init__(level=level)
self._log_emitter = log_emitter or get_log_emitter(__name__)
self.attributes_key = attributes_key

def _get_attributes(self, record: logging.LogRecord) -> Attributes:
if self.attributes_key is not None:
return vars(record)[self.attributes_key]
return {k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS}
return {
k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS
}

def _translate(self, record: logging.LogRecord) -> LogRecord:
timestamp = int(record.created * 1e9)
Expand Down
14 changes: 12 additions & 2 deletions opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,20 @@ def test_log_record_user_attributes_key(self):
"""Users can specify a key to extract attributes from"""
emitter_mock = Mock(spec=LogEmitter)
logger = logging.getLogger(__name__)
handler = OTLPHandler(level=logging.NOTSET, log_emitter=emitter_mock, attributes_key="opentelemetry")
handler = OTLPHandler(
level=logging.NOTSET,
log_emitter=emitter_mock,
attributes_key="opentelemetry",
)
logger.addHandler(handler)
# Assert emit gets called for warning message
logger.warning("Wanrning message", extra={"opentelemetry": {"http.status_code": 200}, "ignored": True})
logger.warning(
"Wanrning message",
extra={
"opentelemetry": {"http.status_code": 200},
"ignored": True,
},
)
args, _ = emitter_mock.emit.call_args_list[0]
log_record = args[0]

Expand Down

0 comments on commit 28632ca

Please sign in to comment.