Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Root logger configuration #227

Merged
merged 1 commit into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions azure-monitor-opentelemetry-distro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Expand Down