Skip to content

Commit

Permalink
fix: remove attribute extension to keep this PR on the enabling of fo…
Browse files Browse the repository at this point in the history
…rmatting only
  • Loading branch information
a-recknagel committed Aug 28, 2024
1 parent cbc0920 commit b9f0de9
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,6 @@

_DEFAULT_OTEL_ATTRIBUTE_COUNT_LIMIT = 128
_ENV_VALUE_UNSET = ""
_EXCLUDED_ATTRIBUTES = {
# pseudo-private log-record attributes, they should always be dropped
"args",
"msg",
"message",
"stack_info",
"exc_info",
"exc_text",
# attributes that are retained, but with a different name
# following semantic conventions
"pathname",
"funcName",
"lineno",
"thread",
"threadName",
# attributes that are omitted because no semantic convention exists for them yet
"asctime",
"created",
"filename",
"levelname",
"levelno",
"module",
"msecs",
"name",
"process",
"processName",
"relativeCreated",
"taskName",
}


class LogDroppedAttributesWarning(UserWarning):
Expand Down Expand Up @@ -456,6 +427,37 @@ 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",
"message",
"levelname",
"levelno",
"lineno",
"module",
"msecs",
"msg",
"name",
"pathname",
"process",
"processName",
"relativeCreated",
"stack_info",
"thread",
"threadName",
"taskName",
)
)


class LoggingHandler(logging.Handler):
"""A handler class which writes logging records, in OTLP format, to
a network destination or file. Supports signals from the `logging` module.
Expand All @@ -476,18 +478,13 @@ def __init__(
@staticmethod
def _get_attributes(record: logging.LogRecord) -> Attributes:
attributes = {
k: v
for k, v in vars(record).items()
if k not in _EXCLUDED_ATTRIBUTES and v is not None
k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS
}

# Add standard code attributes for logs.
attributes[SpanAttributes.CODE_FILEPATH] = record.pathname
attributes[SpanAttributes.CODE_FUNCTION] = record.funcName
attributes[SpanAttributes.CODE_LINENO] = record.lineno
# Add thread identifiers for logs.
attributes[SpanAttributes.THREAD_ID] = record.thread
attributes[SpanAttributes.THREAD_NAME] = record.threadName

if record.exc_info:
exctype, value, tb = record.exc_info
Expand Down

0 comments on commit b9f0de9

Please sign in to comment.