Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Reverse default behavior of instrumentations and implement configuration for exclusion
([#253](https://github.com/microsoft/ApplicationInsights-Python/pull/253))

## [1.0.0b10](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b10) - 2023-02-23

- Fix source and wheel distribution, include MANIFEST.in and use `pkgutils` style `__init__.py`
Expand Down
11 changes: 8 additions & 3 deletions azure-monitor-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The Azure Monitor Distro of [Opentelemetry Python][ot_sdk_python] provides multi
This distro automatically installs the following libraries:

* [Azure Monitor OpenTelemetry exporters][azure_monitor_opentelemetry_exporters]

## Officially supported instrumentations

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.

* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests]
* [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django]
* [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask]
Expand Down Expand Up @@ -42,10 +47,10 @@ pip install azure-monitor-opentelemetry --pre
You can use `configure_azure_monitor` to set up instrumentation for your app to Azure Monitor. `configure_azure_monitor` supports the following optional arguments:

* 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.
* instrumentations - Specifies the libraries with [instrumentations][ot_instrumentations] that you would like to use. Accepts a comma separated list. e.g. `["requests", "flask"]`
* disable_logging - If set to `True`, disables collection and export of logging telemetry. Defaults to `False`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove instrumentations

* 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`.
* 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"]`
* resource - Specified the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. See [this][ot_sdk_python_resource] for default behavior.
* 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.
Expand All @@ -70,13 +75,12 @@ configure_azure_monitor(

#### Instrumentation configurations

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.
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.

```python
...
configure_azure_monitor(
connection_string="<your-connection-string>",
instrumentations=["flask", "requests"],
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
requests_config={"excluded_urls": "http://example.com"},
)
Expand All @@ -101,6 +105,7 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
[application_insights_namespace]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview
[application_insights_sampling]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling
[connection_string_doc]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string
[distro_feature_request]: https://github.com/microsoft/ApplicationInsights-Python/issues/new
[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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
AzureMonitorMetricExporter,
AzureMonitorTraceExporter,
)
from azure.monitor.opentelemetry.util import _get_configurations
from azure.monitor.opentelemetry.util.configurations import _get_configurations
from opentelemetry._logs import get_logger_provider, set_logger_provider
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
Expand Down Expand Up @@ -152,7 +152,9 @@ def _setup_metrics(


def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
instrumentations = configurations.get("instrumentations", [])
exclude_instrumentations = configurations.get(
"exclude_instrumentations", []
)
instrumentation_configs = {}

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

for lib_name in instrumentations:
if lib_name in _SUPPORTED_INSTRUMENTED_LIBRARIES:
try:
importlib.import_module(lib_name)
except ImportError:
_logger.warning(
"Unable to import %s. Please make sure it is installed.",
lib_name,
)
continue
instr_lib_name = "opentelemetry.instrumentation." + lib_name
try:
module = importlib.import_module(instr_lib_name)
instrumentor_name = "{}Instrumentor".format(
lib_name.capitalize()
)
class_ = getattr(module, instrumentor_name)
config = instrumentation_configs.get(lib_name, {})
class_().instrument(**config)
except ImportError:
_logger.warning(
"Unable to import %s. Please make sure it is installed.",
instr_lib_name,
)
except Exception as ex:
_logger.warning(
"Exception occured when instrumenting: %s.",
lib_name,
exc_info=ex,
)
else:
for lib_name in _SUPPORTED_INSTRUMENTED_LIBRARIES:
if lib_name in exclude_instrumentations:
_logger.debug("Instrumentation skipped for library %s", lib_name)
continue
# Check if library is installed
try:
importlib.import_module(lib_name)
except ImportError:
_logger.warning(
"Unable to import %s. Please make sure it is installed.",
lib_name,
)
continue
instr_lib_name = "opentelemetry.instrumentation." + lib_name
# Import and instrument the instrumentation
try:
module = importlib.import_module(instr_lib_name)
instrumentor_name = "{}Instrumentor".format(lib_name.capitalize())
class_ = getattr(module, instrumentor_name)
config = instrumentation_configs.get(lib_name, {})
class_().instrument(**config)
except ImportError:
_logger.warning(
"Unable to import %s. Please make sure it is installed.",
instr_lib_name,
)
except Exception as ex:
_logger.warning(
"Instrumentation not supported for library: %s.", lib_name
"Exception occured when instrumenting: %s.",
lib_name,
exc_info=ex,
)
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ def _configure_auto_instrumentation() -> None:
# "azure.monitor.opentelemetry.exporter"
# )
# AzureDiagnosticLogging.enable(_exporter_logger)
# TODO: Uncomment when logging is out of preview
# environ.setdefault(OTEL_LOGS_EXPORTER,
# "azure_monitor_opentelemetry_exporter")
environ.setdefault(
OTEL_METRICS_EXPORTER, "azure_monitor_opentelemetry_exporter"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,3 @@
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------

from typing import Dict

from azure.monitor.opentelemetry._types import ConfigurationValue


def _get_configurations(**kwargs) -> Dict[str, ConfigurationValue]:
configurations = {}

for key, val in kwargs.items():
configurations[key] = val

return configurations


# TODO: Add env var configuration
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------

from typing import Dict

from azure.monitor.opentelemetry._types import ConfigurationValue


def _get_configurations(**kwargs) -> Dict[str, ConfigurationValue]:
configurations = {}

for key, val in kwargs.items():
configurations[key] = val

return configurations


# TODO: Add env var configuration
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
connection_string="<your-connection-string>",
disable_logging=True,
disable_metrics=True,
instrumentations=["psycopg2"],
tracing_export_interval_millis=15000,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# Configure Azure monitor collection telemetry pipeline
configure_azure_monitor(
connection_string="<your-connection-string>",
instrumentations=["django"],
disable_logging=True,
disable_metrics=True,
tracing_export_interval_millis=15000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
connection_string="<your-connection-string>",
disable_logging=True,
disable_metrics=True,
instrumentations=["flask"],
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
tracing_export_interval_millis=15000,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
connection_string="<your-connection-string>",
disable_logging=True,
disable_metrics=True,
instrumentations=["requests"],
requests_config={"excluded_urls": "http://example.com"},
tracing_export_interval_millis=15000,
)
Expand Down
Loading