Skip to content

Commit 9a3a8b6

Browse files
authored
Merge pull request #254 from lzchen/instr
2 parents cdf507e + d3cecac commit 9a3a8b6

File tree

3 files changed

+193
-191
lines changed

3 files changed

+193
-191
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Reverse default behavior of instrumentations and implement configuration for exclusion
66
([#253](https://github.com/microsoft/ApplicationInsights-Python/pull/253))
7+
- Use entrypoints instead of importlib to load instrumentations
8+
([#254](https://github.com/microsoft/ApplicationInsights-Python/pull/254))
79

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

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Licensed under the MIT License. See License in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6-
import importlib
76
from logging import NOTSET, getLogger
87
from typing import Dict
98

@@ -16,6 +15,10 @@
1615
)
1716
from azure.monitor.opentelemetry.util.configurations import _get_configurations
1817
from opentelemetry._logs import get_logger_provider, set_logger_provider
18+
from opentelemetry.instrumentation.dependencies import (
19+
get_dist_dependency_conflicts,
20+
)
21+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
1922
from opentelemetry.metrics import set_meter_provider
2023
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
2124
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
@@ -25,6 +28,7 @@
2528
from opentelemetry.sdk.trace import TracerProvider
2629
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2730
from opentelemetry.trace import get_tracer_provider, set_tracer_provider
31+
from pkg_resources import iter_entry_points
2832

2933
_logger = getLogger(__name__)
3034

@@ -164,32 +168,31 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
164168
lib_name = k.partition(_INSTRUMENTATION_CONFIG_SUFFIX)[0]
165169
instrumentation_configs[lib_name] = v
166170

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)
171+
# use pkg_resources for now until https://github.com/open-telemetry/opentelemetry-python/pull/3168 is merged
172+
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
173+
lib_name = entry_point.name
174+
if lib_name not in _SUPPORTED_INSTRUMENTED_LIBRARIES:
170175
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-
)
176+
if lib_name in exclude_instrumentations:
177+
_logger.debug("Instrumentation excluded for library %s", lib_name)
179178
continue
180-
instr_lib_name = "opentelemetry.instrumentation." + lib_name
181-
# Import and instrument the instrumentation
182179
try:
183-
module = importlib.import_module(instr_lib_name)
184-
instrumentor_name = "{}Instrumentor".format(lib_name.capitalize())
185-
class_ = getattr(module, instrumentor_name)
180+
# Check if dependent libraries/version are installed
181+
conflict = get_dist_dependency_conflicts(entry_point.dist)
182+
if conflict:
183+
_logger.debug(
184+
"Skipping instrumentation %s: %s",
185+
entry_point.name,
186+
conflict,
187+
)
188+
continue
189+
# Load the instrumentor via entrypoint
190+
instrumentor: BaseInstrumentor = entry_point.load()
191+
# Call instrument() with configuration
186192
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+
# tell instrumentation to not run dep checks again as we already did it above
194+
config["skip_dep_check"] = True
195+
instrumentor().instrument(**config)
193196
except Exception as ex:
194197
_logger.warning(
195198
"Exception occured when instrumenting: %s.",

0 commit comments

Comments
 (0)