|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from logging import getLogger |
| 16 | +from os import environ |
| 17 | + |
| 18 | +from pkg_resources import iter_entry_points |
| 19 | + |
| 20 | +from opentelemetry.instrumentation.dependencies import ( |
| 21 | + get_dist_dependency_conflicts, |
| 22 | +) |
| 23 | +from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro |
| 24 | +from opentelemetry.instrumentation.environment_variables import ( |
| 25 | + OTEL_PYTHON_CONFIGURATOR, |
| 26 | + OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, |
| 27 | + OTEL_PYTHON_DISTRO, |
| 28 | +) |
| 29 | +from opentelemetry.instrumentation.version import __version__ |
| 30 | + |
| 31 | +logger = getLogger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +def _load_distros() -> BaseDistro: |
| 35 | + distro_name = environ.get(OTEL_PYTHON_DISTRO, None) |
| 36 | + for entry_point in iter_entry_points("opentelemetry_distro"): |
| 37 | + try: |
| 38 | + if distro_name is None or distro_name == entry_point.name: |
| 39 | + distro = entry_point.load()() |
| 40 | + if not isinstance(distro, BaseDistro): |
| 41 | + logger.debug( |
| 42 | + "%s is not an OpenTelemetry Distro. Skipping", |
| 43 | + entry_point.name, |
| 44 | + ) |
| 45 | + continue |
| 46 | + logger.debug( |
| 47 | + "Distribution %s will be configured", entry_point.name |
| 48 | + ) |
| 49 | + return distro |
| 50 | + except Exception as exc: # pylint: disable=broad-except |
| 51 | + logger.exception( |
| 52 | + "Distribution %s configuration failed", entry_point.name |
| 53 | + ) |
| 54 | + raise exc |
| 55 | + return DefaultDistro() |
| 56 | + |
| 57 | + |
| 58 | +def _load_instrumentors(distro): |
| 59 | + package_to_exclude = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, []) |
| 60 | + if isinstance(package_to_exclude, str): |
| 61 | + package_to_exclude = package_to_exclude.split(",") |
| 62 | + # to handle users entering "requests , flask" or "requests, flask" with spaces |
| 63 | + package_to_exclude = [x.strip() for x in package_to_exclude] |
| 64 | + |
| 65 | + for entry_point in iter_entry_points("opentelemetry_pre_instrument"): |
| 66 | + entry_point.load()() |
| 67 | + |
| 68 | + for entry_point in iter_entry_points("opentelemetry_instrumentor"): |
| 69 | + if entry_point.name in package_to_exclude: |
| 70 | + logger.debug( |
| 71 | + "Instrumentation skipped for library %s", entry_point.name |
| 72 | + ) |
| 73 | + continue |
| 74 | + |
| 75 | + try: |
| 76 | + conflict = get_dist_dependency_conflicts(entry_point.dist) |
| 77 | + if conflict: |
| 78 | + logger.debug( |
| 79 | + "Skipping instrumentation %s: %s", |
| 80 | + entry_point.name, |
| 81 | + conflict, |
| 82 | + ) |
| 83 | + continue |
| 84 | + |
| 85 | + # tell instrumentation to not run dep checks again as we already did it above |
| 86 | + distro.load_instrumentor(entry_point, skip_dep_check=True) |
| 87 | + logger.debug("Instrumented %s", entry_point.name) |
| 88 | + except Exception as exc: # pylint: disable=broad-except |
| 89 | + logger.exception("Instrumenting of %s failed", entry_point.name) |
| 90 | + raise exc |
| 91 | + |
| 92 | + for entry_point in iter_entry_points("opentelemetry_post_instrument"): |
| 93 | + entry_point.load()() |
| 94 | + |
| 95 | + |
| 96 | +def _load_configurators(): |
| 97 | + configurator_name = environ.get(OTEL_PYTHON_CONFIGURATOR, None) |
| 98 | + configured = None |
| 99 | + for entry_point in iter_entry_points("opentelemetry_configurator"): |
| 100 | + if configured is not None: |
| 101 | + logger.warning( |
| 102 | + "Configuration of %s not loaded, %s already loaded", |
| 103 | + entry_point.name, |
| 104 | + configured, |
| 105 | + ) |
| 106 | + continue |
| 107 | + try: |
| 108 | + if ( |
| 109 | + configurator_name is None |
| 110 | + or configurator_name == entry_point.name |
| 111 | + ): |
| 112 | + entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore |
| 113 | + configured = entry_point.name |
| 114 | + else: |
| 115 | + logger.warning( |
| 116 | + "Configuration of %s not loaded because %s is set by %s", |
| 117 | + entry_point.name, |
| 118 | + configurator_name, |
| 119 | + OTEL_PYTHON_CONFIGURATOR, |
| 120 | + ) |
| 121 | + except Exception as exc: # pylint: disable=broad-except |
| 122 | + logger.exception("Configuration of %s failed", entry_point.name) |
| 123 | + raise exc |
0 commit comments