Skip to content

Commit

Permalink
Add , and to all logs
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Feb 5, 2024
1 parent d270d41 commit 093afa9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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
7 changes: 6 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,12 @@ 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("tests/logs/test_handler.py"))
self.assertEqual(log_record.attributes[SpanAttributes.CODE_FUNCTION], "test_log_record_user_attributes")
# The line of the log statement from 8 lines above.
self.assertEqual(log_record.attributes[SpanAttributes.CODE_LINENO], 110)
self.assertTrue(isinstance(log_record.attributes, BoundedAttributes))

def test_log_record_exception(self):
Expand Down

0 comments on commit 093afa9

Please sign in to comment.