Skip to content

Add logger exception support for logs API/SDK#4908

Merged
xrmx merged 35 commits intoopen-telemetry:mainfrom
iblancasa:4907
Apr 13, 2026
Merged

Add logger exception support for logs API/SDK#4908
xrmx merged 35 commits intoopen-telemetry:mainfrom
iblancasa:4907

Conversation

@iblancasa
Copy link
Copy Markdown
Contributor

@iblancasa iblancasa commented Feb 11, 2026

Description

Fixes #4907

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Unit tests

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added

@iblancasa iblancasa requested a review from a team as a code owner February 11, 2026 17:28
@iblancasa iblancasa marked this pull request as draft February 11, 2026 17:34
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
@xrmx
Copy link
Copy Markdown
Contributor

xrmx commented Feb 12, 2026

Haven't looked in detail at the PR but we are usually cautious on adding in development stuff to the api / sdk.

@trask
Copy link
Copy Markdown
Member

trask commented Feb 13, 2026

hey @xrmx! check out open-telemetry/opentelemetry-specification#4886

if you can give this PR a basic review and "lgtm" without approving/merging it, then we can treat it as a "qualifying prototype" for marking it stable in the spec. we already have 3 qualifying prototypes which is the min bar for stabilizing the spec, but the more the better!

Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

github-merge-queue Bot pushed a commit to open-telemetry/opentelemetry-specification that referenced this pull request Feb 18, 2026
Fixes #4858

Implementations / Prototypes:

- Java:
[`ExtendedLogRecordBuilder.setException(Throwable)`](https://github.com/open-telemetry/opentelemetry-java/blob/main/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java#L137)
- Builder method called before `emit()`
  - Merged and available to users
- .NET:
[`LogRecordAttributeList.RecordException(Exception)`](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Api/Logs/LogRecordAttributeList.cs#L191-L198)
- Attribute list method called before `EmitLog()`
- JavaScript:
[opentelemetry-js#6385](open-telemetry/opentelemetry-js#6385)
- Field on LogRecord passed to `emit()`
- Python:
[opentelemetry-python#4908](open-telemetry/opentelemetry-python#4908)
- Parameter on LogRecord passed to `emit()`

(only the Python prototype doesn't yet meet the definition of
[qualifying
prototype](https://github.com/open-telemetry/opentelemetry-specification/blob/main/CONTRIBUTING.md#proposing-a-change)
yet)

Co-authored-by: Robert Pająk <pellared@hotmail.com>
@xrmx
Copy link
Copy Markdown
Contributor

xrmx commented Feb 18, 2026

hey @xrmx! check out open-telemetry/opentelemetry-specification#4886

if you can give this PR a basic review and "lgtm" without approving/merging it, then we can treat it as a "qualifying prototype" for marking it stable in the spec. we already have 3 qualifying prototypes which is the min bar for stabilizing the spec, but the more the better!

Missed this comment but already approved the other PR 😅

Copy link
Copy Markdown

@nagkumar91 nagkumar91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice implementation — spec compliance looks solid. A couple of nits below.

Comment thread opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
Comment thread opentelemetry-sdk/tests/logs/test_logs.py
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
@MikeGoldsmith
Copy link
Copy Markdown
Member

Looks like there are some merge conflicts that need fixing - otherwise I think this is ready for review / merge by a @open-telemetry/python-maintainers 👍🏻

@MikeGoldsmith MikeGoldsmith moved this from Ready for review to Approved PRs in Python PR digest Apr 8, 2026
Comment thread opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py Outdated
Copy link
Copy Markdown
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With something like this, tests should still pass:

diff --git a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
index 6264b6eca..12a8cc08f 100644
--- a/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
+++ b/opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
@@ -167,8 +167,6 @@ class Logger(ABC):
     def emit(
         self,
         record: LogRecord,
-        *,
-        exception: BaseException | None = None,
     ) -> None: ...
 
     @abstractmethod
@@ -214,8 +212,6 @@ class NoOpLogger(Logger):
     def emit(  # pylint:disable=arguments-differ
         self,
         record: LogRecord,
-        *,
-        exception: BaseException | None = None,
     ) -> None: ...
 
     def emit(
@@ -284,8 +280,6 @@ class ProxyLogger(Logger):
     def emit(  # pylint:disable=arguments-differ
         self,
         record: LogRecord,
-        *,
-        exception: BaseException | None = None,
     ) -> None: ...
 
     def emit(
@@ -303,7 +297,7 @@ class ProxyLogger(Logger):
         exception: BaseException | None = None,
     ) -> None:
         if record:
-            self._logger.emit(record, exception=exception)
+            self._logger.emit(record)
         else:
             self._logger.emit(
                 timestamp=timestamp,
diff --git a/opentelemetry-api/tests/logs/test_proxy.py b/opentelemetry-api/tests/logs/test_proxy.py
index f7744e5f3..1b5d0c22c 100644
--- a/opentelemetry-api/tests/logs/test_proxy.py
+++ b/opentelemetry-api/tests/logs/test_proxy.py
@@ -77,15 +77,12 @@ class TestProxy(LoggingGlobalsTest, unittest.TestCase):
         real_logger = provider.get_logger("proxy-test")
         self.assertIsInstance(real_logger, LoggerTest)
 
-    def test_proxy_logger_forwards_exception_with_record(self):
+    def test_proxy_logger_forwards_record_with_exception(self):
         logger = _logs_internal.ProxyLogger("proxy-test")
         logger._real_logger = Mock(spec=LoggerTest("proxy-test"))
-        record = _logs.LogRecord()
-        exception = ValueError("boom")
+        record = _logs.LogRecord(exception=ValueError("boom"))
 
         self.assertIsNotNone(logger._real_logger)
-        logger.emit(record, exception=exception)
+        logger.emit(record)
 
-        logger._real_logger.emit.assert_called_once_with(
-            record, exception=exception
-        )
+        logger._real_logger.emit.assert_called_once_with(record)
diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py
index 3633a308c..4ed043e67 100644
--- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py
@@ -793,19 +793,12 @@ class Logger(APILogger):
             return
         # If a record is provided, use it directly
         if record is not None:
-            record_exception = exception or getattr(record, "exception", None)
-            if record_exception is None and isinstance(
-                record, ReadWriteLogRecord
-            ):
-                record_exception = getattr(
-                    record.log_record, "exception", None
-                )
             if not isinstance(record, ReadWriteLogRecord):
-                if record_exception is not None:
+                if record.exception is not None:
                     record = _copy_log_record(
                         record,
                         _get_attributes_with_exception(
-                            record.attributes, record_exception
+                            record.attributes, record.exception
                         ),
                     )
                 # pylint:disable=protected-access
@@ -816,7 +809,7 @@ class Logger(APILogger):
                 )
             else:
                 record.log_record.attributes = _get_attributes_with_exception(
-                    record.log_record.attributes, record_exception
+                    record.log_record.attributes, record.log_record.exception
                 )
                 writable_record = record
         else:

iblancasa and others added 9 commits April 10, 2026 12:51
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
Signed-off-by: Israel Blancas <iblancasa@gmail.com>
@xrmx xrmx enabled auto-merge (squash) April 13, 2026 15:35
@xrmx xrmx merged commit 0eedb27 into open-telemetry:main Apr 13, 2026
465 of 466 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest Apr 13, 2026
chimchim89 pushed a commit to chimchim89/opentelemetry-python that referenced this pull request Apr 15, 2026
* Add logger exception support for logs API/SDK

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Apply changes requested in code review

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Fix CI

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Fix ci

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Apply feedback from code review

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Fix lint

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Remove unrelated entry from changelog

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Fix lint

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

* Fix lint

Signed-off-by: Israel Blancas <iblancasa@gmail.com>

---------

Signed-off-by: Israel Blancas <iblancasa@gmail.com>
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
Co-authored-by: Mike Goldsmith <goldsmith.mike@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add log exception support to Logger API/SDK

8 participants