Skip to content

Commit de1d49b

Browse files
a-recknagelArne Caratti
authored and
Arne Caratti
committed
respect semantic conventions for log record attributes
1 parent b6ed8bf commit de1d49b

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
"exc_info",
6363
"exc_text",
6464
}
65+
_SEMANTIC_CONVENTION_ATTRS = {
66+
"pathname",
67+
"funcName",
68+
"lineno",
69+
"thread",
70+
"threadName",
71+
}
6572

6673

6774
class LogLimits:
@@ -416,26 +423,21 @@ class LoggingHandler(logging.Handler):
416423
https://docs.python.org/3/library/logging.html
417424
"""
418425

419-
def __init__(
420-
self,
421-
level=logging.NOTSET,
422-
logger_provider=None,
423-
exclude_attributes=None,
424-
) -> None:
426+
def __init__(self, level=logging.NOTSET, logger_provider=None) -> None:
425427
super().__init__(level=level)
426428
self._logger_provider = logger_provider or get_logger_provider()
427429
self._logger = get_logger(
428430
__name__, logger_provider=self._logger_provider
429431
)
430-
if exclude_attributes is None:
431-
exclude_attributes = set()
432-
self.exclude_attributes = _PRIVATE_RECORD_ATTRS | exclude_attributes
432+
self._exclude_attributes = (
433+
_PRIVATE_RECORD_ATTRS | _SEMANTIC_CONVENTION_ATTRS
434+
)
433435

434436
def _get_attributes(self, record: logging.LogRecord) -> Attributes:
435437
attributes = {
436438
k: v
437439
for k, v in vars(record).items()
438-
if k not in self.exclude_attributes
440+
if k not in self._exclude_attributes
439441
}
440442
if record.exc_info:
441443
exc_type = ""
@@ -454,6 +456,13 @@ def _get_attributes(self, record: logging.LogRecord) -> Attributes:
454456
attributes[SpanAttributes.EXCEPTION_TYPE] = exc_type
455457
attributes[SpanAttributes.EXCEPTION_MESSAGE] = message
456458
attributes[SpanAttributes.EXCEPTION_STACKTRACE] = stack_trace
459+
460+
# adding these attributes with their semantic convention names
461+
attributes[SpanAttributes.CODE_FILEPATH] = record.pathname
462+
attributes[SpanAttributes.CODE_FUNCTION] = record.funcName
463+
attributes[SpanAttributes.CODE_LINENO] = record.lineno
464+
attributes[SpanAttributes.THREAD_ID] = record.thread
465+
attributes[SpanAttributes.THREAD_NAME] = record.threadName
457466
return attributes
458467

459468
def _translate(self, record: logging.LogRecord) -> LogRecord:

opentelemetry-sdk/tests/logs/test_handler.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_log_record_trace_correlation(self):
201201
self.assertEqual(log_record.span_id, span_context.span_id)
202202
self.assertEqual(log_record.trace_flags, span_context.trace_flags)
203203

204-
def test_original_record_args_are_retained(self):
204+
def test_log_record_args_are_translated(self):
205205
emitter_provider_mock = Mock(spec=LoggerProvider)
206206
emitter_mock = APIGetLogger(
207207
__name__, logger_provider=emitter_provider_mock
@@ -216,21 +216,21 @@ def test_original_record_args_are_retained(self):
216216
self.assertEqual(
217217
set(log_record.attributes),
218218
{
219+
"code.filepath",
220+
"code.function",
221+
"code.lineno",
219222
"created",
220223
"filename",
221-
"funcName",
222224
"levelname",
223225
"levelno",
224-
"lineno",
225226
"module",
226227
"msecs",
227228
"name",
228-
"pathname",
229229
"process",
230230
"processName",
231231
"relativeCreated",
232-
"thread",
233-
"threadName",
232+
"thread.id",
233+
"thread.name",
234234
},
235235
)
236236

0 commit comments

Comments
 (0)