From 27e3b54978827c15c3173d745c5644632eecc580 Mon Sep 17 00:00:00 2001 From: jerevoss Date: Fri, 27 Jan 2023 11:46:17 -0800 Subject: [PATCH] Add configuration of logging instrumentation root --- CHANGELOG.md | 2 ++ azure-monitor-opentelemetry-distro/README.md | 6 ++++-- .../azure/monitor/opentelemetry/distro/__init__.py | 3 ++- .../tests/configuration/test_configure.py | 6 +++++- .../tests/configuration/test_util.py | 2 ++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e75b05a..30dcea6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ ([#228](https://github.com/microsoft/ApplicationInsights-Python/pull/228)) - Removing diagnostic logging from its module's logger. ([#225](https://github.com/microsoft/ApplicationInsights-Python/pull/225)) +- Configure logging instrumenation root + ([#227](https://github.com/microsoft/ApplicationInsights-Python/pull/227)) ## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26 diff --git a/azure-monitor-opentelemetry-distro/README.md b/azure-monitor-opentelemetry-distro/README.md index e70037e3..ecd12c8e 100644 --- a/azure-monitor-opentelemetry-distro/README.md +++ b/azure-monitor-opentelemetry-distro/README.md @@ -37,8 +37,9 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to * service_name = Specifies the [service][service_semantic_convention_doc] name. * service_namespace = Specifies the [service][service_semantic_convention_doc] namespace. * service_instance_id = Specifies the [service][service_semantic_convention_doc] instance id. -* disable_logging = If set to `True`, disables collection and export of logging telemetry. -* logging_level = Specifies the [logging level][logging_level] of the Opentelemetry Logging Handler. Ex: logging.WARNING. +* disable_logging = If set to `True`, disables collection and export of logging telemetry. Defaults to False. +* logging_level = Specifies the [logging level][logging_level] of the Opentelemetry Logging Handler. Defaults to logging.NOTSET. +* logger_name = Specifies the [logger name][logger_name_hierarchy_doc] under which all logging will be instrumented. Defaults to "" which corresponds to the root logger. * logging_export_interval_millis = Specifies the export interval of the logging exporter in milliseconds. Defaults to 30,000. * disable_tracing = If set to `True`, disables collection and export of distributed tracing telemetry. * sampling_ratio = Specifies the ratio of distributed tracing telemetry to be [sampled][application_insights_sampling]. Accepted values are in the range [0,1]. Defaults to 1.0, meaning no telemetry is sampled out. @@ -67,6 +68,7 @@ To use this package, you must have: [connection_string_doc]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string [exporter_configuration_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter#configuration [logging_level]: https://docs.python.org/3/library/logging.html#levels +[logger_name_hierarchy_doc]: https://docs.python.org/3/library/logging.html#logger-objects [ot_instrumentations]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation [ot_python_docs]: https://opentelemetry.io/docs/instrumentation/python/ [ot_sdk_python]: https://github.com/open-telemetry/opentelemetry-python diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py index dccff7be..45f2a8c6 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py @@ -99,6 +99,7 @@ def _setup_tracing(resource: Resource, configurations: Dict[str, Any]): def _setup_logging(resource: Resource, configurations: Dict[str, Any]): + logger_name = configurations.get("logger_name", "") logging_level = configurations.get("logging_level", NOTSET) logging_export_interval_millis = configurations.get( "logging_export_interval_millis", 30000 @@ -114,7 +115,7 @@ def _setup_logging(resource: Resource, configurations: Dict[str, Any]): handler = LoggingHandler( level=logging_level, logger_provider=get_logger_provider() ) - getLogger().addHandler(handler) + getLogger(logger_name).addHandler(handler) def _setup_instrumentations(configurations: Dict[str, Any]): diff --git a/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py b/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py index ec7c3016..0a36328a 100644 --- a/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py +++ b/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py @@ -52,6 +52,7 @@ def test_configure_azure_monitor( "disable_tracing": False, "logging_export_interval_millis": 10000, "logging_level": "test_logging_level", + "logger_name": "test_logger_name", "service_name": "test_service_name", "service_namespace": "test_namespace", "service_instance_id": "test_id", @@ -91,6 +92,7 @@ def test_configure_azure_monitor_disable_tracing( "disable_tracing": True, "logging_export_interval_millis": 10000, "logging_level": "test_logging_level", + "logger_name": "test_logger_name", "service_name": "test_service_name", "service_namespace": "test_namespace", "service_instance_id": "test_id", @@ -130,6 +132,7 @@ def test_configure_azure_monitor_disable_logging( "disable_tracing": False, "logging_export_interval_millis": 10000, "logging_level": "test_logging_level", + "logger_name": "test_logger_name", "service_name": "test_service_name", "service_namespace": "test_namespace", "service_instance_id": "test_id", @@ -272,6 +275,7 @@ def test_setup_logging( "disable_logging": False, "logging_export_interval_millis": 10000, "logging_level": "test_logging_level", + "logger_name": "test_logger_name", } _setup_logging(resource_mock, configurations) @@ -288,7 +292,7 @@ def test_setup_logging( logging_handler_mock.assert_called_once_with( level="test_logging_level", logger_provider=lp_init_mock ) - get_logger_mock.assert_called_once_with() + get_logger_mock.assert_called_once_with("test_logger_name") logger_mock.addHandler.assert_called_once_with( logging_handler_init_mock ) diff --git a/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py b/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py index 01678e9f..eb8a8308 100644 --- a/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py +++ b/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py @@ -24,6 +24,7 @@ def test_get_configurations(self): disable_logging="test_disable_logging", disable_tracing="test_disable_tracing", logging_level="test_logging_level", + logger_name="test_logger_name", service_name="test_service_name", service_namespace="test_namespace", service_instance_id="test_id", @@ -40,6 +41,7 @@ def test_get_configurations(self): configurations["disable_tracing"], "test_disable_tracing" ) self.assertEqual(configurations["logging_level"], "test_logging_level") + self.assertEqual(configurations["logger_name"], "test_logger_name") self.assertEqual(configurations["service_name"], "test_service_name") self.assertEqual(configurations["service_namespace"], "test_namespace") self.assertEqual(configurations["service_instance_id"], "test_id")