Skip to content

Commit 9faa28d

Browse files
committed
Add manual handling of auto instrumentation and code attributes logging
1 parent f4760c2 commit 9faa28d

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,19 @@ def record_factory(*args, **kwargs):
228228
"Disabling logging auto-instrumentation. If you have opentelemetry-instrumentation-logging "
229229
"you don't need to set `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true`"
230230
)
231-
elif (
231+
elif kwargs.get(
232+
"enable_log_auto_istrumentation",
232233
environ.get(OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION, "true")
233234
.strip()
234235
.lower()
235-
) == "true":
236-
log_code_attributes = (
236+
== "true",
237+
):
238+
log_code_attributes = kwargs.get(
239+
"log_code_attributes",
237240
environ.get(OTEL_PYTHON_LOG_CODE_ATTRIBUTES, "false")
238241
.strip()
239242
.lower()
240-
== "true"
243+
== "true",
241244
)
242245
logger_provider = get_logger_provider()
243246
handler = _setup_logging_handler(

instrumentation/opentelemetry-instrumentation-logging/tests/test_logging.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,35 @@ def test_handler_setup_is_called_with_code_attributes_from_env_var(self):
376376
setup_mock.assert_called_once_with(
377377
logger_provider=logger_provider, log_code_attributes=True
378378
)
379+
380+
def test_handler_setup_is_controlled_by_instrumentor_parameter(
381+
self,
382+
):
383+
LoggingInstrumentor().uninstrument()
384+
with self.caplog.at_level(level=logging.WARNING):
385+
LoggingInstrumentor().instrument(
386+
enable_log_auto_istrumentation=False
387+
)
388+
389+
self.assertEqual(len(self.caplog.records), 0)
390+
root_logger = logging.getLogger()
391+
logging_handler_instances = [
392+
handler
393+
for handler in root_logger.handlers
394+
if isinstance(handler, LoggingHandler)
395+
]
396+
self.assertEqual(logging_handler_instances, [])
397+
398+
def test_handler_code_attributes_is_controlled_by_instrumentor_parameter(
399+
self,
400+
):
401+
LoggingInstrumentor().uninstrument()
402+
with mock.patch(
403+
"opentelemetry.instrumentation.logging._setup_logging_handler"
404+
) as setup_mock:
405+
LoggingInstrumentor().instrument(log_code_attributes=True)
406+
407+
logger_provider = get_logger_provider()
408+
setup_mock.assert_called_once_with(
409+
logger_provider=logger_provider, log_code_attributes=True
410+
)

0 commit comments

Comments
 (0)