Skip to content

Commit

Permalink
Fix exception thrown when LoggerProvider not configured (open-telemet…
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Sep 7, 2023
1 parent 9f3c0af commit 6070a0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix error when no LoggerProvider configured for LoggingHandler
([#3423](https://github.com/open-telemetry/opentelemetry-python/pull/3423))

## Version 1.20.0/0.41b0 (2023-09-04)

- Modify Prometheus exporter to translate non-monotonic Sums into Gauges
([#3306](https://github.com/open-telemetry/opentelemetry-python/pull/3306))


## Version 1.19.0/0.40b0 (2023-07-13)

- Drop `setuptools` runtime requirement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from opentelemetry._logs import LoggerProvider as APILoggerProvider
from opentelemetry._logs import LogRecord as APILogRecord
from opentelemetry._logs import (
NoOpLogger,
SeverityNumber,
get_logger,
get_logger_provider,
Expand Down Expand Up @@ -532,11 +533,12 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:

def emit(self, record: logging.LogRecord) -> None:
"""
Emit a record.
Emit a record. Skip emitting if logger is NoOp.
The record is translated to OTel format, and then sent across the pipeline.
"""
self._logger.emit(self._translate(record))
if not isinstance(self._logger, NoOpLogger):
self._logger.emit(self._translate(record))

def flush(self) -> None:
"""
Expand Down
17 changes: 16 additions & 1 deletion opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest
from unittest.mock import Mock

from opentelemetry._logs import SeverityNumber
from opentelemetry._logs import NoOpLoggerProvider, SeverityNumber
from opentelemetry._logs import get_logger as APIGetLogger
from opentelemetry.attributes import BoundedAttributes
from opentelemetry.sdk import trace
Expand Down Expand Up @@ -64,6 +64,21 @@ def test_handler_custom_log_level(self):
logger.critical("No Time For Caution")
self.assertEqual(emitter_mock.emit.call_count, 2)

# pylint: disable=protected-access
def test_log_record_emit_noop(self):
noop_logger_provder = NoOpLoggerProvider()
logger_mock = APIGetLogger(
__name__, logger_provider=noop_logger_provder
)
logger = logging.getLogger(__name__)
handler_mock = Mock(spec=LoggingHandler)
handler_mock._logger = logger_mock
handler_mock.level = logging.WARNING
logger.addHandler(handler_mock)
with self.assertLogs(level=logging.WARNING):
logger.warning("Warning message")
handler_mock._translate.assert_not_called()

def test_log_record_no_span_context(self):
emitter_provider_mock = Mock(spec=LoggerProvider)
emitter_mock = APIGetLogger(
Expand Down

0 comments on commit 6070a0d

Please sign in to comment.