From 363c6dda80f948dcbf6bfcd53bbd3d070aeb6be3 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 13 Feb 2023 09:18:53 -0800 Subject: [PATCH 1/4] protected --- CHANGELOG.md | 2 + azure-monitor-opentelemetry-distro/README.md | 12 +++--- .../monitor/opentelemetry/distro/__init__.py | 38 ++++++++++++++----- .../{configurator.py => _configurator.py} | 0 .../distro/{distro.py => _distro.py} | 0 .../monitor/opentelemetry/distro/_types.py | 23 +++++++++++ .../opentelemetry/distro/util/__init__.py | 5 ++- .../monitor/opentelemetry/distro/version.py | 7 ---- azure-monitor-opentelemetry-distro/setup.py | 4 +- .../tests/configuration/test_util.py | 4 +- .../tests/instrumentation/test_django.py | 20 ++++++++++ 11 files changed, 87 insertions(+), 28 deletions(-) rename azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/{configurator.py => _configurator.py} (100%) rename azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/{distro.py => _distro.py} (100%) create mode 100644 azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py delete mode 100644 azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/version.py create mode 100644 azure-monitor-opentelemetry-distro/tests/instrumentation/test_django.py diff --git a/CHANGELOG.md b/CHANGELOG.md index e9fc96be..bb22619e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ ([#241](https://github.com/microsoft/ApplicationInsights-Python/pull/241)) - Defaulting logging env var for auto-instrumentation. Added logging samples. ([#240](https://github.com/microsoft/ApplicationInsights-Python/pull/240)) +- Move symbols to protected + ([#240](https://github.com/microsoft/ApplicationInsights-Python/pull/240)) ## [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 c7b27076..58441ee5 100644 --- a/azure-monitor-opentelemetry-distro/README.md +++ b/azure-monitor-opentelemetry-distro/README.md @@ -46,13 +46,13 @@ 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. -* disable_metrics - If set to `True`, disables collection and export of metric telemetry. -* disable_tracing - If set to `True`, disables collection and export of distributed tracing telemetry. -* 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. +* disable_logging - If set to `True`, disables collection and export of logging telemetry. Defaults to `False`. +* disable_metrics - If set to `True`, disables collection and export of metric telemetry. Defaults to `False`. +* disable_tracing - If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`. +* logging_level - Specifies the [logging level][logging_level] of the logs you would like to collect for your logging pipeline. Defaults to logging.NOTSET. +* logger_name = Specifies the [logger name][logger_name_hierarchy_doc] under which logging will be instrumented. Defaults to "" which corresponds to the root logger. * logging_export_interval_millis - Specifies the logging export interval in milliseconds. Defaults to 5000. -* metric_readers - Specifies the [metric readers][ot_metric_reader] that you would like to use for your metrics pipeline. Accepts a list of [metric readers][ot_sdk_python_metric_reader]. +* metric_readers - Specifies the [metric readers][ot_metric_reader] that you would like to use for your metric pipeline. Accepts a list of [metric readers][ot_sdk_python_metric_reader]. * views - Specifies the list of [views][opentelemetry_specification_view] to configure for the metric pipeline. See [here][ot_sdk_python_view_examples] for example usage. * 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. * tracing_export_interval_millis - Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. 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 02918594..00fa58c8 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py @@ -5,9 +5,10 @@ # -------------------------------------------------------------------------- import importlib from logging import NOTSET, getLogger -from typing import Any, Dict +from typing import Dict -from azure.monitor.opentelemetry.distro.util import get_configurations +from azure.monitor.opentelemetry.distro.util import _get_configurations +from azure.monitor.opentelemetry.distro._types import ConfigurationValue from azure.monitor.opentelemetry.exporter import ( ApplicationInsightsSampler, AzureMonitorLogExporter, @@ -42,14 +43,33 @@ ) -def configure_azure_monitor(**kwargs): +def configure_azure_monitor(**kwargs) -> None: """ This function works as a configuration layer that allows the end user to configure OpenTelemetry and Azure monitor components. The configuration can be done via arguments passed to this function. + :keyword str connection_string: Connection string for your Application Insights resource. + :keyword Sequence[str] connection_string: Specifies the libraries with instrumentations to be enabled. + :keyword str service_name: Specifies the service name. + :keyword str service_namespace: Specifies the service namespace. + :keyword str service_instance_id: Specifies the service instance id. + :keyword bool disable_logging: If set to `True`, disables collection and export of logging telemetry. Defaults to `False`. + :keyword bool disable_metrics: If set to `True`, disables collection and export of metric telemetry. Defaults to `False`. + :keyword bool disable_tracing: If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`. + :keyword int logging_level: Specifies the logging of the logs you would like to collect for your logging pipeline. + :keyword str logger_name: Specifies the logger name under which logging will be instrumented. Defaults to "" which corresponds to the root logger. + :keyword int logging_export_interval_millis: Specifies the logging export interval in milliseconds. Defaults to 5000. + :keyword Sequence[MetricReader] metric_readers: Specifies the metric readers that you would like to use for your metric pipeline. + :keyword Sequence[View] views: Specifies the list of views to configure for the metric pipeline. + :keyword float sampling_ratio: Specifies the ratio of distributed tracing telemetry to be sampled. Accepted values are in the range [0,1]. Defaults to 1.0, meaning no telemetry is sampled out. + :keyword int tracing_export_interval_millis: Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. + :keyword Dict[str, Any] _config: Specifies a dictionary of kwargs that will be applied to configuration for instrumentation . + :keyword bool disable_offline_storage: Boolean value to determine whether to disable storing failed telemetry records for retry. Defaults to `False`. + :keyword str storage_directory: Storage directory in which to store retry files. Defaults to `/Microsoft/AzureMonitor/opentelemetry-python-`. + :rtype: None """ - configurations = get_configurations(**kwargs) + configurations = _get_configurations(**kwargs) disable_tracing = configurations.get("disable_tracing", False) disable_logging = configurations.get("disable_logging", False) @@ -77,7 +97,7 @@ def configure_azure_monitor(**kwargs): _setup_instrumentations(configurations) -def _get_resource(configurations: Dict[str, Any]) -> Resource: +def _get_resource(configurations: Dict[str, ConfigurationValue]) -> Resource: service_name = configurations.get("service_name", "") service_namespace = configurations.get("service_namespace", "") service_instance_id = configurations.get("service_instance_id", "") @@ -90,7 +110,7 @@ def _get_resource(configurations: Dict[str, Any]) -> Resource: ) -def _setup_tracing(resource: Resource, configurations: Dict[str, Any]): +def _setup_tracing(resource: Resource, configurations: Dict[str, ConfigurationValue]): sampling_ratio = configurations.get("sampling_ratio", 1.0) tracing_export_interval_millis = configurations.get( "tracing_export_interval_millis", 5000 @@ -108,7 +128,7 @@ def _setup_tracing(resource: Resource, configurations: Dict[str, Any]): get_tracer_provider().add_span_processor(span_processor) -def _setup_logging(resource: Resource, configurations: Dict[str, Any]): +def _setup_logging(resource: Resource, configurations: Dict[str, ConfigurationValue]): logger_name = configurations.get("logger_name", "") logging_level = configurations.get("logging_level", NOTSET) logging_export_interval_millis = configurations.get( @@ -128,7 +148,7 @@ def _setup_logging(resource: Resource, configurations: Dict[str, Any]): getLogger(logger_name).addHandler(handler) -def _setup_metrics(resource: Resource, configurations: Dict[str, Any]): +def _setup_metrics(resource: Resource, configurations: Dict[str, ConfigurationValue]): views = configurations.get("views", ()) metric_readers = configurations.get("metric_readers", []) metric_exporter = AzureMonitorMetricExporter(**configurations) @@ -141,7 +161,7 @@ def _setup_metrics(resource: Resource, configurations: Dict[str, Any]): set_meter_provider(meter_provider) -def _setup_instrumentations(configurations: Dict[str, Any]): +def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]): instrumentations = configurations.get("instrumentations", []) instrumentation_configs = {} diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/configurator.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_configurator.py similarity index 100% rename from azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/configurator.py rename to azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_configurator.py diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_distro.py similarity index 100% rename from azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py rename to azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_distro.py diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py new file mode 100644 index 00000000..874656f3 --- /dev/null +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py @@ -0,0 +1,23 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from typing import Sequence, Union + +from opentelemetry.sdk.metrics.export import MetricReader +from opentelemetry.sdk.metrics.view import View + +ConfigurationValue = Union[ + str, + bool, + int, + float, + Sequence[str], + Sequence[bool], + Sequence[int], + Sequence[float], + Sequence[MetricReader], + Sequence[View], +] diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py index 7ca337ae..65c6707b 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py @@ -4,10 +4,11 @@ # license information. # -------------------------------------------------------------------------- -from typing import Any, Dict +from typing import Dict +from azure.monitor.opentelemetry.distro._types import ConfigurationValue -def get_configurations(**kwargs) -> Dict[str, Any]: +def _get_configurations(**kwargs) -> Dict[str, ConfigurationValue]: configurations = {} for key, val in kwargs.items(): diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/version.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/version.py deleted file mode 100644 index 24ef9d09..00000000 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/version.py +++ /dev/null @@ -1,7 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License in the project root for -# license information. -# -------------------------------------------------------------------------- - -VERSION = "1.0.0b8" diff --git a/azure-monitor-opentelemetry-distro/setup.py b/azure-monitor-opentelemetry-distro/setup.py index 7ef63ee5..8a445974 100644 --- a/azure-monitor-opentelemetry-distro/setup.py +++ b/azure-monitor-opentelemetry-distro/setup.py @@ -94,10 +94,10 @@ ], entry_points={ "opentelemetry_distro": [ - "azure_monitor_opentelemetry_distro = azure.monitor.opentelemetry.distro.distro:AzureMonitorDistro" + "azure_monitor_opentelemetry_distro = azure.monitor.opentelemetry.distro._distro:AzureMonitorDistro" ], "opentelemetry_configurator": [ - "azure_monitor_opentelemetry_configurator = azure.monitor.opentelemetry.distro.configurator:AzureMonitorConfigurator" + "azure_monitor_opentelemetry_configurator = azure.monitor.opentelemetry.distro._configurator:AzureMonitorConfigurator" ], }, ) diff --git a/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py b/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py index f4c7ed8b..ca1897d0 100644 --- a/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py +++ b/azure-monitor-opentelemetry-distro/tests/configuration/test_util.py @@ -14,12 +14,12 @@ import unittest -from azure.monitor.opentelemetry.distro.util import get_configurations +from azure.monitor.opentelemetry.distro.util import _get_configurations class TestUtil(unittest.TestCase): def test_get_configurations(self): - configurations = get_configurations( + configurations = _get_configurations( connection_string="test_cs", disable_logging="test_disable_logging", disable_tracing="test_disable_tracing", diff --git a/azure-monitor-opentelemetry-distro/tests/instrumentation/test_django.py b/azure-monitor-opentelemetry-distro/tests/instrumentation/test_django.py new file mode 100644 index 00000000..42992967 --- /dev/null +++ b/azure-monitor-opentelemetry-distro/tests/instrumentation/test_django.py @@ -0,0 +1,20 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import unittest + +from opentelemetry.instrumentation.django import DjangoInstrumentor + + +class TestDjangoInstrumentation(unittest.TestCase): + def test_instrument(self): + try: + DjangoInstrumentor().instrument() + except Exception as ex: # pylint: disable=broad-except + print(ex) + self.fail( + f"Unexpected exception raised when instrumenting {DjangoInstrumentor.__name__}" + ) From 3237532f7dcdb1d5b24980ef1b1a61c77dda6527 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 13 Feb 2023 14:50:41 -0800 Subject: [PATCH 2/4] test --- CHANGELOG.md | 2 +- azure-monitor-opentelemetry-distro/tests/test_distro.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c9dbf2a..4ef33485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ ([#242](https://github.com/microsoft/ApplicationInsights-Python/pull/242)) - Update to azure-monitor-opentelemetry-exporter 1.0.0b12 ([#243](https://github.com/microsoft/ApplicationInsights-Python/pull/243)) -- Move symbols to protected +- Move symbols to protected, add docstring for api ([#244](https://github.com/microsoft/ApplicationInsights-Python/pull/244)) ## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26 diff --git a/azure-monitor-opentelemetry-distro/tests/test_distro.py b/azure-monitor-opentelemetry-distro/tests/test_distro.py index 50a4f530..83a4bd26 100644 --- a/azure-monitor-opentelemetry-distro/tests/test_distro.py +++ b/azure-monitor-opentelemetry-distro/tests/test_distro.py @@ -6,7 +6,7 @@ class TestDistro(TestCase): @patch( - "azure.monitor.opentelemetry.distro.distro.AzureDiagnosticLogging.enable" + "azure.monitor.opentelemetry.distro._distro.AzureDiagnosticLogging.enable" ) # TODO: Enabled when duplciate logging issue is solved # @patch( @@ -18,9 +18,9 @@ def test_configure(self, mock_diagnostics): distro.configure() self.assertEqual(mock_diagnostics.call_count, 2) - # TODO: Enabled when duplciate logging issue is solved + # TODO: Enabled when duplicate logging issue is solved # @patch( - # "azure.monitor.opentelemetry.distro.distro.AzureDiagnosticLogging.enable" + # "azure.monitor.opentelemetry.distro._distro.AzureDiagnosticLogging.enable" # ) # @patch( # "azure.monitor.opentelemetry.distro._diagnostic_logging._EXPORTER_DIAGNOSTICS_ENABLED", From 29f539c91b2d66089e90fbd274fd018dec3b5964 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 13 Feb 2023 15:06:08 -0800 Subject: [PATCH 3/4] test --- CHANGELOG.md | 2 +- azure-monitor-opentelemetry-distro/setup.py | 2 ++ azure-monitor-opentelemetry-distro/tests/test_distro.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ef33485..e279565f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ ([#242](https://github.com/microsoft/ApplicationInsights-Python/pull/242)) - Update to azure-monitor-opentelemetry-exporter 1.0.0b12 ([#243](https://github.com/microsoft/ApplicationInsights-Python/pull/243)) -- Move symbols to protected, add docstring for api +- Move symbols to protected, add docstring for api, pin opentelemtry-api/sdk versions ([#244](https://github.com/microsoft/ApplicationInsights-Python/pull/244)) ## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26 diff --git a/azure-monitor-opentelemetry-distro/setup.py b/azure-monitor-opentelemetry-distro/setup.py index 0c88dad7..d94e8606 100644 --- a/azure-monitor-opentelemetry-distro/setup.py +++ b/azure-monitor-opentelemetry-distro/setup.py @@ -91,6 +91,8 @@ "opentelemetry-instrumentation-requests~=0.36b0", "opentelemetry-instrumentation-flask~=0.36b0", "opentelemetry-instrumentation-psycopg2~=0.36b0", + "opentelemetry-api==1.15.0", + "opentelemetry-sdk==1.15.0", ], entry_points={ "opentelemetry_distro": [ diff --git a/azure-monitor-opentelemetry-distro/tests/test_distro.py b/azure-monitor-opentelemetry-distro/tests/test_distro.py index 83a4bd26..0bb89791 100644 --- a/azure-monitor-opentelemetry-distro/tests/test_distro.py +++ b/azure-monitor-opentelemetry-distro/tests/test_distro.py @@ -1,7 +1,7 @@ from unittest import TestCase from unittest.mock import patch -from azure.monitor.opentelemetry.distro.distro import AzureMonitorDistro +from azure.monitor.opentelemetry.distro._distro import AzureMonitorDistro class TestDistro(TestCase): From 82c3a7639190989819c768806ee46f30263a82d3 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 13 Feb 2023 15:08:40 -0800 Subject: [PATCH 4/4] lint --- .../azure/monitor/opentelemetry/distro/__init__.py | 14 ++++++++++---- .../monitor/opentelemetry/distro/util/__init__.py | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) 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 63f90b81..cacfad3f 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py @@ -7,8 +7,8 @@ from logging import NOTSET, getLogger from typing import Dict -from azure.monitor.opentelemetry.distro.util import _get_configurations from azure.monitor.opentelemetry.distro._types import ConfigurationValue +from azure.monitor.opentelemetry.distro.util import _get_configurations from azure.monitor.opentelemetry.exporter import ( ApplicationInsightsSampler, AzureMonitorLogExporter, @@ -106,7 +106,9 @@ def _get_resource(configurations: Dict[str, ConfigurationValue]) -> Resource: ) -def _setup_tracing(resource: Resource, configurations: Dict[str, ConfigurationValue]): +def _setup_tracing( + resource: Resource, configurations: Dict[str, ConfigurationValue] +): sampling_ratio = configurations.get("sampling_ratio", 1.0) tracing_export_interval_millis = configurations.get( "tracing_export_interval_millis", 5000 @@ -124,7 +126,9 @@ def _setup_tracing(resource: Resource, configurations: Dict[str, ConfigurationVa get_tracer_provider().add_span_processor(span_processor) -def _setup_logging(resource: Resource, configurations: Dict[str, ConfigurationValue]): +def _setup_logging( + resource: Resource, configurations: Dict[str, ConfigurationValue] +): logger_name = configurations.get("logger_name", "") logging_level = configurations.get("logging_level", NOTSET) logging_export_interval_millis = configurations.get( @@ -144,7 +148,9 @@ def _setup_logging(resource: Resource, configurations: Dict[str, ConfigurationVa getLogger(logger_name).addHandler(handler) -def _setup_metrics(resource: Resource, configurations: Dict[str, ConfigurationValue]): +def _setup_metrics( + resource: Resource, configurations: Dict[str, ConfigurationValue] +): views = configurations.get("views", ()) metric_readers = configurations.get("metric_readers", []) metric_exporter = AzureMonitorMetricExporter(**configurations) diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py index 65c6707b..87090feb 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py @@ -8,6 +8,7 @@ from azure.monitor.opentelemetry.distro._types import ConfigurationValue + def _get_configurations(**kwargs) -> Dict[str, ConfigurationValue]: configurations = {}