Skip to content

Commit

Permalink
Update code and tests to adhere to new LoggingHandler.__init__() sign…
Browse files Browse the repository at this point in the history
…ature
  • Loading branch information
pranavmarla committed Aug 6, 2022
1 parent 22b81fe commit b8c3306
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def _init_logging(
BatchLogProcessor(exporter_class(**exporter_args))
)

log_emitter = provider.get_log_emitter(__name__)
handler = LoggingHandler(level=logging.NOTSET, log_emitter=log_emitter)
handler = LoggingHandler(level=logging.NOTSET, log_emitter_provider=provider)

logging.getLogger().addHandler(handler)

Expand Down
30 changes: 10 additions & 20 deletions opentelemetry-sdk/tests/logs/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ class TestSimpleLogProcessor(unittest.TestCase):
def test_simple_log_processor_default_level(self):
exporter = InMemoryLogExporter()
log_emitter_provider = LogEmitterProvider()
log_emitter = log_emitter_provider.get_log_emitter(__name__)

log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter))

logger = logging.getLogger("default_level")
logger.addHandler(LoggingHandler(log_emitter=log_emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider))

logger.warning("Something is wrong")
finished_logs = exporter.get_finished_logs()
Expand All @@ -68,13 +67,12 @@ def test_simple_log_processor_default_level(self):
def test_simple_log_processor_custom_level(self):
exporter = InMemoryLogExporter()
log_emitter_provider = LogEmitterProvider()
log_emitter = log_emitter_provider.get_log_emitter(__name__)

log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter))

logger = logging.getLogger("custom_level")
logger.setLevel(logging.ERROR)
logger.addHandler(LoggingHandler(log_emitter=log_emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider))

logger.warning("Warning message")
logger.debug("Debug message")
Expand All @@ -99,12 +97,11 @@ def test_simple_log_processor_custom_level(self):
def test_simple_log_processor_trace_correlation(self):
exporter = InMemoryLogExporter()
log_emitter_provider = LogEmitterProvider()
log_emitter = log_emitter_provider.get_log_emitter("name", "version")

log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter))

logger = logging.getLogger("trace_correlation")
logger.addHandler(LoggingHandler(log_emitter=log_emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider))

logger.warning("Warning message")
finished_logs = exporter.get_finished_logs()
Expand Down Expand Up @@ -137,12 +134,11 @@ def test_simple_log_processor_trace_correlation(self):
def test_simple_log_processor_shutdown(self):
exporter = InMemoryLogExporter()
log_emitter_provider = LogEmitterProvider()
log_emitter = log_emitter_provider.get_log_emitter(__name__)

log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter))

logger = logging.getLogger("shutdown")
logger.addHandler(LoggingHandler(log_emitter=log_emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider))

logger.warning("Something is wrong")
finished_logs = exporter.get_finished_logs()
Expand All @@ -167,9 +163,8 @@ def test_emit_call_log_record(self):
provider = LogEmitterProvider()
provider.add_log_processor(log_processor)

emitter = provider.get_log_emitter(__name__)
logger = logging.getLogger("emit_call")
logger.addHandler(LoggingHandler(log_emitter=emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=provider))

logger.error("error")
self.assertEqual(log_processor.emit.call_count, 1)
Expand All @@ -181,9 +176,8 @@ def test_shutdown(self):
provider = LogEmitterProvider()
provider.add_log_processor(log_processor)

emitter = provider.get_log_emitter(__name__)
logger = logging.getLogger("shutdown")
logger.addHandler(LoggingHandler(log_emitter=emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=provider))

logger.warning("warning message: %s", "possible upcoming heatwave")
logger.error("Very high rise in temperatures across the globe")
Expand Down Expand Up @@ -214,9 +208,8 @@ def test_force_flush(self):
provider = LogEmitterProvider()
provider.add_log_processor(log_processor)

emitter = provider.get_log_emitter(__name__)
logger = logging.getLogger("force_flush")
logger.addHandler(LoggingHandler(log_emitter=emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=provider))

logger.critical("Earth is burning")
log_processor.force_flush()
Expand All @@ -233,9 +226,8 @@ def test_log_processor_too_many_logs(self):
provider = LogEmitterProvider()
provider.add_log_processor(log_processor)

emitter = provider.get_log_emitter(__name__)
logger = logging.getLogger("many_logs")
logger.addHandler(LoggingHandler(log_emitter=emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=provider))

for log_no in range(1000):
logger.critical("Log no: %s", log_no)
Expand All @@ -251,9 +243,8 @@ def test_with_multiple_threads(self):
provider = LogEmitterProvider()
provider.add_log_processor(log_processor)

emitter = provider.get_log_emitter(__name__)
logger = logging.getLogger("threads")
logger.addHandler(LoggingHandler(log_emitter=emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=provider))

def bulk_log_and_flush(num_logs):
for _ in range(num_logs):
Expand Down Expand Up @@ -286,9 +277,8 @@ def test_batch_log_processor_fork(self):
provider = LogEmitterProvider()
provider.add_log_processor(log_processor)

emitter = provider.get_log_emitter(__name__)
logger = logging.getLogger("test-fork")
logger.addHandler(LoggingHandler(log_emitter=emitter))
logger.addHandler(LoggingHandler(log_emitter_provider=provider))

logger.critical("yolo")
time.sleep(0.5) # give some time for the exporter to upload
Expand Down
41 changes: 24 additions & 17 deletions opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@
from unittest.mock import Mock

from opentelemetry.sdk import trace
from opentelemetry.sdk._logs import LogEmitter, LoggingHandler
from opentelemetry.sdk._logs import get_log_emitter, LogEmitterProvider, LoggingHandler
from opentelemetry.sdk._logs.severity import SeverityNumber
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import INVALID_SPAN_CONTEXT


def get_logger(level=logging.NOTSET, log_emitter=None):
def get_logger(level=logging.NOTSET, log_emitter_provider=None):
logger = logging.getLogger(__name__)
handler = LoggingHandler(level=level, log_emitter=log_emitter)
handler = LoggingHandler(level=level, log_emitter_provider=log_emitter_provider)
logger.addHandler(handler)
return logger


class TestLoggingHandler(unittest.TestCase):
def test_handler_default_log_level(self):
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(log_emitter_provider=emitter_provider_mock)
# Make sure debug messages are ignored by default
logger.debug("Debug message")
self.assertEqual(emitter_mock.emit.call_count, 0)
Expand All @@ -41,8 +42,9 @@ def test_handler_default_log_level(self):
self.assertEqual(emitter_mock.emit.call_count, 1)

def test_handler_custom_log_level(self):
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(level=logging.ERROR, log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(level=logging.ERROR, log_emitter_provider=emitter_provider_mock)
logger.warning("Warning message test custom log level")
# Make sure any log with level < ERROR is ignored
self.assertEqual(emitter_mock.emit.call_count, 0)
Expand All @@ -51,8 +53,9 @@ def test_handler_custom_log_level(self):
self.assertEqual(emitter_mock.emit.call_count, 2)

def test_log_record_no_span_context(self):
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(log_emitter_provider=emitter_provider_mock)
# Assert emit gets called for warning message
logger.warning("Warning message")
args, _ = emitter_mock.emit.call_args_list[0]
Expand All @@ -67,8 +70,9 @@ def test_log_record_no_span_context(self):

def test_log_record_user_attributes(self):
"""Attributes can be injected into logs by adding them to the LogRecord"""
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(log_emitter_provider=emitter_provider_mock)
# Assert emit gets called for warning message
logger.warning("Warning message", extra={"http.status_code": 200})
args, _ = emitter_mock.emit.call_args_list[0]
Expand All @@ -79,8 +83,9 @@ def test_log_record_user_attributes(self):

def test_log_record_exception(self):
"""Exception information will be included in attributes"""
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(log_emitter_provider=emitter_provider_mock)
try:
raise ZeroDivisionError("division by zero")
except ZeroDivisionError:
Expand Down Expand Up @@ -109,8 +114,9 @@ def test_log_record_exception(self):

def test_log_exc_info_false(self):
"""Exception information will be included in attributes"""
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(log_emitter_provider=emitter_provider_mock)
try:
raise ZeroDivisionError("division by zero")
except ZeroDivisionError:
Expand All @@ -129,8 +135,9 @@ def test_log_exc_info_false(self):
)

def test_log_record_trace_correlation(self):
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
emitter_provider_mock = Mock(spec=LogEmitterProvider)
emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock)
logger = get_logger(log_emitter_provider=emitter_provider_mock)

tracer = trace.TracerProvider().get_tracer(__name__)
with tracer.start_as_current_span("test") as span:
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-sdk/tests/logs/test_multi_log_prcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def force_flush(self, timeout_millis=30000):
class TestLogProcessor(unittest.TestCase):
def test_log_processor(self):
provider = LogEmitterProvider()
log_emitter = provider.get_log_emitter(__name__)
handler = LoggingHandler(log_emitter=log_emitter)
handler = LoggingHandler(log_emitter_provider=provider)

logs_list_1 = []
processor1 = AnotherLogProcessor(Mock(), logs_list_1)
Expand Down

0 comments on commit b8c3306

Please sign in to comment.