|
3 | 3 | # Licensed under the MIT License. See License in the project root for
|
4 | 4 | # license information.
|
5 | 5 | # --------------------------------------------------------------------------
|
6 |
| -import importlib |
7 | 6 | from logging import NOTSET, getLogger
|
8 | 7 | from typing import Dict
|
9 | 8 |
|
|
16 | 15 | )
|
17 | 16 | from azure.monitor.opentelemetry.util.configurations import _get_configurations
|
18 | 17 | 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 |
19 | 22 | from opentelemetry.metrics import set_meter_provider
|
20 | 23 | from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
|
21 | 24 | from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
|
|
25 | 28 | from opentelemetry.sdk.trace import TracerProvider
|
26 | 29 | from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
27 | 30 | from opentelemetry.trace import get_tracer_provider, set_tracer_provider
|
| 31 | +from pkg_resources import iter_entry_points |
28 | 32 |
|
29 | 33 | _logger = getLogger(__name__)
|
30 | 34 |
|
@@ -164,32 +168,31 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
|
164 | 168 | lib_name = k.partition(_INSTRUMENTATION_CONFIG_SUFFIX)[0]
|
165 | 169 | instrumentation_configs[lib_name] = v
|
166 | 170 |
|
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: |
170 | 175 | 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) |
179 | 178 | continue
|
180 |
| - instr_lib_name = "opentelemetry.instrumentation." + lib_name |
181 |
| - # Import and instrument the instrumentation |
182 | 179 | 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 |
186 | 192 | 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) |
193 | 196 | except Exception as ex:
|
194 | 197 | _logger.warning(
|
195 | 198 | "Exception occured when instrumenting: %s.",
|
|
0 commit comments