Skip to content

Commit cdf507e

Browse files
authored
Merge pull request #253 from lzchen/entry
2 parents 5f582d8 + 5a38963 commit cdf507e

File tree

12 files changed

+193
-160
lines changed

12 files changed

+193
-160
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Reverse default behavior of instrumentations and implement configuration for exclusion
6+
([#253](https://github.com/microsoft/ApplicationInsights-Python/pull/253))
7+
58
## [1.0.0b10](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b10) - 2023-02-23
69

710
- Fix source and wheel distribution, include MANIFEST.in and use `pkgutils` style `__init__.py`

azure-monitor-opentelemetry/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The Azure Monitor Distro of [Opentelemetry Python][ot_sdk_python] provides multi
55
This distro automatically installs the following libraries:
66

77
* [Azure Monitor OpenTelemetry exporters][azure_monitor_opentelemetry_exporters]
8+
9+
## Officially supported instrumentations
10+
11+
The following OpenTelemetry instrumentations come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code.
12+
813
* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests]
914
* [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django]
1015
* [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask]
@@ -42,10 +47,10 @@ pip install azure-monitor-opentelemetry --pre
4247
You can use `configure_azure_monitor` to set up instrumentation for your app to Azure Monitor. `configure_azure_monitor` supports the following optional arguments:
4348

4449
* connection_string - The [connection string][connection_string_doc] for your Application Insights resource. The connection string will be automatically populated from the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable if not explicitly passed in.
45-
* instrumentations - Specifies the libraries with [instrumentations][ot_instrumentations] that you would like to use. Accepts a comma separated list. e.g. `["requests", "flask"]`
4650
* disable_logging - If set to `True`, disables collection and export of logging telemetry. Defaults to `False`.
4751
* disable_metrics - If set to `True`, disables collection and export of metric telemetry. Defaults to `False`.
4852
* disable_tracing - If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`.
53+
* exclude_instrumentations - By default, all supported [instrumentations](#officially-supported-instrumentations) are enabled to collect telemetry. Specify instrumentations you do not want to enable to collect telemetry by passing in a comma separated list of instrumented library names. e.g. `["requests", "flask"]`
4954
* resource - Specified the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. See [this][ot_sdk_python_resource] for default behavior.
5055
* logging_level - Specifies the [logging level][logging_level] of the logs you would like to collect for your logging pipeline. Defaults to logging.NOTSET.
5156
* logger_name = Specifies the [logger name][logger_name_hierarchy_doc] under which logging will be instrumented. Defaults to "" which corresponds to the root logger.
@@ -70,13 +75,12 @@ configure_azure_monitor(
7075

7176
#### Instrumentation configurations
7277

73-
You can pass in instrumentation specific configuration into `configure_azure_monitor` with the key `<instrumented-library-name>_config` and value as a dictionary representing `kwargs` for the corresponding instrumentation. Note the instrumented library must also be enabled through the `instrumentations` configuration.
78+
You can pass in instrumentation specific configuration into `configure_azure_monitor` with the key `<instrumented-library-name>_config` and value as a dictionary representing `kwargs` for the corresponding instrumentation.
7479

7580
```python
7681
...
7782
configure_azure_monitor(
7883
connection_string="<your-connection-string>",
79-
instrumentations=["flask", "requests"],
8084
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
8185
requests_config={"excluded_urls": "http://example.com"},
8286
)
@@ -101,6 +105,7 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
101105
[application_insights_namespace]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview
102106
[application_insights_sampling]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling
103107
[connection_string_doc]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string
108+
[distro_feature_request]: https://github.com/microsoft/ApplicationInsights-Python/issues/new
104109
[exporter_configuration_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter#configuration
105110
[logging_level]: https://docs.python.org/3/library/logging.html#levels
106111
[logger_name_hierarchy_doc]: https://docs.python.org/3/library/logging.html#logger-objects

azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
AzureMonitorMetricExporter,
1515
AzureMonitorTraceExporter,
1616
)
17-
from azure.monitor.opentelemetry.util import _get_configurations
17+
from azure.monitor.opentelemetry.util.configurations import _get_configurations
1818
from opentelemetry._logs import get_logger_provider, set_logger_provider
1919
from opentelemetry.metrics import set_meter_provider
2020
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
@@ -152,7 +152,9 @@ def _setup_metrics(
152152

153153

154154
def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
155-
instrumentations = configurations.get("instrumentations", [])
155+
exclude_instrumentations = configurations.get(
156+
"exclude_instrumentations", []
157+
)
156158
instrumentation_configs = {}
157159

158160
# Instrumentation specific configs
@@ -162,37 +164,35 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
162164
lib_name = k.partition(_INSTRUMENTATION_CONFIG_SUFFIX)[0]
163165
instrumentation_configs[lib_name] = v
164166

165-
for lib_name in instrumentations:
166-
if lib_name in _SUPPORTED_INSTRUMENTED_LIBRARIES:
167-
try:
168-
importlib.import_module(lib_name)
169-
except ImportError:
170-
_logger.warning(
171-
"Unable to import %s. Please make sure it is installed.",
172-
lib_name,
173-
)
174-
continue
175-
instr_lib_name = "opentelemetry.instrumentation." + lib_name
176-
try:
177-
module = importlib.import_module(instr_lib_name)
178-
instrumentor_name = "{}Instrumentor".format(
179-
lib_name.capitalize()
180-
)
181-
class_ = getattr(module, instrumentor_name)
182-
config = instrumentation_configs.get(lib_name, {})
183-
class_().instrument(**config)
184-
except ImportError:
185-
_logger.warning(
186-
"Unable to import %s. Please make sure it is installed.",
187-
instr_lib_name,
188-
)
189-
except Exception as ex:
190-
_logger.warning(
191-
"Exception occured when instrumenting: %s.",
192-
lib_name,
193-
exc_info=ex,
194-
)
195-
else:
167+
for lib_name in _SUPPORTED_INSTRUMENTED_LIBRARIES:
168+
if lib_name in exclude_instrumentations:
169+
_logger.debug("Instrumentation skipped for library %s", lib_name)
170+
continue
171+
# Check if library is installed
172+
try:
173+
importlib.import_module(lib_name)
174+
except ImportError:
175+
_logger.warning(
176+
"Unable to import %s. Please make sure it is installed.",
177+
lib_name,
178+
)
179+
continue
180+
instr_lib_name = "opentelemetry.instrumentation." + lib_name
181+
# Import and instrument the instrumentation
182+
try:
183+
module = importlib.import_module(instr_lib_name)
184+
instrumentor_name = "{}Instrumentor".format(lib_name.capitalize())
185+
class_ = getattr(module, instrumentor_name)
186+
config = instrumentation_configs.get(lib_name, {})
187+
class_().instrument(**config)
188+
except ImportError:
189+
_logger.warning(
190+
"Unable to import %s. Please make sure it is installed.",
191+
instr_lib_name,
192+
)
193+
except Exception as ex:
196194
_logger.warning(
197-
"Instrumentation not supported for library: %s.", lib_name
195+
"Exception occured when instrumenting: %s.",
196+
lib_name,
197+
exc_info=ex,
198198
)

azure-monitor-opentelemetry/azure/monitor/opentelemetry/autoinstrumentation/_distro.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ def _configure_auto_instrumentation() -> None:
5050
# "azure.monitor.opentelemetry.exporter"
5151
# )
5252
# AzureDiagnosticLogging.enable(_exporter_logger)
53-
# TODO: Uncomment when logging is out of preview
54-
# environ.setdefault(OTEL_LOGS_EXPORTER,
55-
# "azure_monitor_opentelemetry_exporter")
5653
environ.setdefault(
5754
OTEL_METRICS_EXPORTER, "azure_monitor_opentelemetry_exporter"
5855
)

azure-monitor-opentelemetry/azure/monitor/opentelemetry/util/__init__.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,3 @@
33
# Licensed under the MIT License. See License in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6-
7-
from typing import Dict
8-
9-
from azure.monitor.opentelemetry._types import ConfigurationValue
10-
11-
12-
def _get_configurations(**kwargs) -> Dict[str, ConfigurationValue]:
13-
configurations = {}
14-
15-
for key, val in kwargs.items():
16-
configurations[key] = val
17-
18-
return configurations
19-
20-
21-
# TODO: Add env var configuration
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
from typing import Dict
8+
9+
from azure.monitor.opentelemetry._types import ConfigurationValue
10+
11+
12+
def _get_configurations(**kwargs) -> Dict[str, ConfigurationValue]:
13+
configurations = {}
14+
15+
for key, val in kwargs.items():
16+
configurations[key] = val
17+
18+
return configurations
19+
20+
21+
# TODO: Add env var configuration

azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
connection_string="<your-connection-string>",
1212
disable_logging=True,
1313
disable_metrics=True,
14-
instrumentations=["psycopg2"],
1514
tracing_export_interval_millis=15000,
1615
)
1716

azure-monitor-opentelemetry/samples/tracing/django/sample/example/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# Configure Azure monitor collection telemetry pipeline
1111
configure_azure_monitor(
1212
connection_string="<your-connection-string>",
13-
instrumentations=["django"],
1413
disable_logging=True,
1514
disable_metrics=True,
1615
tracing_export_interval_millis=15000,

azure-monitor-opentelemetry/samples/tracing/server_flask.py renamed to azure-monitor-opentelemetry/samples/tracing/http_flask.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
connection_string="<your-connection-string>",
1212
disable_logging=True,
1313
disable_metrics=True,
14-
instrumentations=["flask"],
1514
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
1615
tracing_export_interval_millis=15000,
1716
)

azure-monitor-opentelemetry/samples/tracing/client.py renamed to azure-monitor-opentelemetry/samples/tracing/http_requests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
connection_string="<your-connection-string>",
1717
disable_logging=True,
1818
disable_metrics=True,
19-
instrumentations=["requests"],
2019
requests_config={"excluded_urls": "http://example.com"},
2120
tracing_export_interval_millis=15000,
2221
)

0 commit comments

Comments
 (0)