我们知道:
Python import
时会首先寻找 sys.path
中列出的路径,类似下面:
sys.path ['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
那么 Python 是如何获取 sys.path
的呢?根据 Python 的文档,首先是当前目录,然后是 PYTHONPATH
环境变量,再之后是安装时设置的默认目录,由 site
模块控制。
Python 在启动的时候会自动引用 site
模块,
它扩展了 sys.path ,其中特定于站点的名称是通过将前缀值 sys.prefix 和 sys.exec_prefix 与几个后缀组合而构建的。使用的前缀值保存在模块级变量 PREFIXES 中以供稍后参考。在 Windows 下,后缀是一个空字符串和 lib/site-packages 。对于类 Unix 平台,值为 lib/python(其中version 由解释器的主要版本号和次要版本号替换,例如 3.5 )和 lib/site-python 。
除了全局站点包路径之外, site 还负责将用户特定的位置添加到导入路径。用户特定的路径都基于 USER_BASE 目录,该目录通常位于当前用户拥有(和可写)的文件系统的一部分中。在 USER_BASE 目录中是一个 site-packages 目录,其路径可以作为 USER_SITE 访问。
site
模块还负责在 sitecustomize
模块中加载由本地站点所有者定义的站点范围的定制。 sitecustomize
的用途包括扩展导入路径并启用覆盖,分析或其他开发工具。
查看下面的例子:
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from logging import getLogger
from os import environ
from os.path import abspath, dirname, pathsep
from pkg_resources import iter_entry_points
from opentelemetry.instrumentation.dependencies import (
get_dist_dependency_conflicts,
)
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
from opentelemetry.instrumentation.environment_variables import (
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
)
from opentelemetry.instrumentation.utils import _python_path_without_directory
from opentelemetry.instrumentation.version import __version__
logger = getLogger(__name__)
def _load_distros() -> BaseDistro:
for entry_point in iter_entry_points("opentelemetry_distro"):
try:
distro = entry_point.load()()
if not isinstance(distro, BaseDistro):
logger.debug(
"%s is not an OpenTelemetry Distro. Skipping",
entry_point.name,
)
continue
logger.debug(
"Distribution %s will be configured", entry_point.name
)
return distro
except Exception as exc: # pylint: disable=broad-except
logger.exception(
"Distribution %s configuration failed", entry_point.name
)
raise exc
return DefaultDistro()
def _load_instrumentors(distro):
package_to_exclude = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, [])
if isinstance(package_to_exclude, str):
package_to_exclude = package_to_exclude.split(",")
# to handle users entering "requests , flask" or "requests, flask" with spaces
package_to_exclude = [x.strip() for x in package_to_exclude]
for entry_point in iter_entry_points("opentelemetry_pre_instrument"):
entry_point.load()()
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
if entry_point.name in package_to_exclude:
logger.debug(
"Instrumentation skipped for library %s", entry_point.name
)
continue
try:
conflict = get_dist_dependency_conflicts(entry_point.dist)
if conflict:
logger.debug(
"Skipping instrumentation %s: %s",
entry_point.name,
conflict,
)
continue
# tell instrumentation to not run dep checks again as we already did it above
distro.load_instrumentor(entry_point, skip_dep_check=True)
logger.debug("Instrumented %s", entry_point.name)
except Exception as exc: # pylint: disable=broad-except
logger.exception("Instrumenting of %s failed", entry_point.name)
raise exc
for entry_point in iter_entry_points("opentelemetry_post_instrument"):
entry_point.load()()
def _load_configurators():
configured = None
for entry_point in iter_entry_points("opentelemetry_configurator"):
if configured is not None:
logger.warning(
"Configuration of %s not loaded, %s already loaded",
entry_point.name,
configured,
)
continue
try:
entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore
configured = entry_point.name
except Exception as exc: # pylint: disable=broad-except
logger.exception("Configuration of %s failed", entry_point.name)
raise exc
def initialize():
# prevents auto-instrumentation of subprocesses if code execs another python process
environ["PYTHONPATH"] = _python_path_without_directory(
environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep
)
try:
distro = _load_distros()
distro.configure()
_load_configurators()
_load_instrumentors(distro)
except Exception: # pylint: disable=broad-except
logger.exception("Failed to auto initialize opentelemetry")
initialize()
在opentelemetry的python项目就是运用了site机制,解决python启动时自动执行instrument的问题。
在sitecustomize中会通过pkg_resources.iter_entry_points 扫描 opentelemetry_instrumentor 加载那些已经安装的instrumentor,比如:django、flask等。