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

Add code.lineno, code.function and code.filepath to all logs #3675

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3623](https://github.com/open-telemetry/opentelemetry-python/pull/3623))
- Improve Resource Detector timeout messaging
([#3645](https://github.com/open-telemetry/opentelemetry-python/pull/3645))
- Add `code.lineno`, `code.function` and `code.filepath` to all logs
([#3645](https://github.com/open-telemetry/opentelemetry-python/pull/3645))

## Version 1.22.0/0.43b0 (2023-12-15)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ def _get_attributes(record: logging.LogRecord) -> Attributes:
attributes = {
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

if record.exc_info:
exc_type = ""
message = ""
Expand Down
15 changes: 14 additions & 1 deletion opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,20 @@ def test_log_record_user_attributes(self):
log_record = args[0]

self.assertIsNotNone(log_record)
self.assertEqual(log_record.attributes, {"http.status_code": 200})
self.assertEqual(len(log_record.attributes), 4)
self.assertEqual(log_record.attributes["http.status_code"], 200)
self.assertTrue(
log_record.attributes[SpanAttributes.CODE_FILEPATH].endswith(
"test_handler.py"
)
)
self.assertEqual(
log_record.attributes[SpanAttributes.CODE_FUNCTION],
"test_log_record_user_attributes",
)
# The line of the log statement is not a constant (changing tests may change that),
# so only check that the attribute is present.
self.assertTrue(SpanAttributes.CODE_LINENO in log_record.attributes)
self.assertTrue(isinstance(log_record.attributes, BoundedAttributes))

def test_log_record_exception(self):
Expand Down
Loading