Skip to main content

LoongSuite .pth site bootstrap for OpenTelemetry auto-instrumentation (opt-in via env)

Project description

loongsuite-site-bootstrap

不修改业务代码、不改用 loongsuite-instrument 启动命令的前提下,通过 site-packages 中的 .pth在解释器早期触发一次 import,从而在进程内执行与 opentelemetry-instrumentsitecustomizeinitialize())等价的 OpenTelemetry 自动注入逻辑。

loongsuite-bootstrap 的职责不同:本包只负责启动早期注入,不会安装任何 Instrumentation 包。可与 loongsuite-bootstrap 配合使用(推荐),也可按需自行 pip install 所需的 opentelemetry-instrumentation-* / loongsuite-instrumentation-*

安装

pip install loongsuite-site-bootstrap

随后请安装 Instrumentation 与 Exporter。推荐用 loongsuite-bootstrap 安装:

loongsuite-bootstrap -a install --latest
# 或仅安装当前环境已存在依赖对应的埋点
loongsuite-bootstrap -a install --latest --auto-detect

也可以手动安装,例如:

pip install opentelemetry-exporter-otlp loongsuite-instrumentation-langchain

配置优先级

  1. 进程环境变量(导出、容器 env、启动参数等):已存在的键不会被 bootstrap-config.json 覆盖。
  2. ~/.loongsuite/bootstrap-config.json:仅在 LOONGSUITE_PYTHON_SITE_BOOTSTRAP 实际处于开启状态时才会读取;对文件中出现的键,仅当环境中尚未设置时写入 os.environ(便于默认带上 OTLP、exporter 等,子进程可继承)。若环境中已显式设置该开关且为“关闭”含义,则不读 JSON、也不做自动注入。

JSON 根节点须为对象;键必须为字符串。值的类型会转成字符串再写入环境变量:booltrue / falseint / float → 十进制字符串,str → 原样,null 跳过,其它类型 → 紧凑 JSON 字符串。

示例 ~/.loongsuite/bootstrap-config.json

{
  "LOONGSUITE_PYTHON_SITE_BOOTSTRAP": "True",
  "OTEL_SERVICE_NAME": "my-app",
  "OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4317"
}

若文件不存在、无法读取或 JSON 无效,则跳过文件(无效时可能打出一条 logging 告警);不影响 Python 正常启动。

启用方式

默认不执行任何 OTel 逻辑(避免影响环境中所有 Python 进程)。可在 bootstrap-config.json 或环境变量 中开启后,本包在进程启动早期执行自动注入:

export LOONGSUITE_PYTHON_SITE_BOOTSTRAP=True

True 字符串视为开启(不区分大小写,如 true / TRUE);其它任何取值(含空字符串等)均视为关闭。

启用时会(若尚未设置)默认:

  • OTEL_PYTHON_DISTRO=loongsuite
  • OTEL_PYTHON_CONFIGURATOR=loongsuite

从而使用 loongsuite-distro 中的 LoongSuiteDistro / LoongSuiteConfigurator(与 loongsuite-instrument + OTEL_PYTHON_DISTRO=loongsuite 一致)。上述两项仍使用 setdefault,在 JSON 补齐之后执行;若环境或 JSON 已为同名变量赋值,则保持已有取值。

行为说明

  • 安装后 wheel 会在 site-packages 根目录释放 loongsuite-site-bootstrap.pth,其中含一行 import loongsuite_site_bootstrap,依赖 CPython site.pthimport 行的标准行为。
  • 不使用 python -S(禁用 site)时才会生效。
  • 作用范围是当前 Python 环境内所有启用了本 bootstrap 的进程,不仅是某一应用入口。
  • LOONGSUITE_PYTHON_SITE_BOOTSTRAP 已开启且 initialize() 成功结束后,会向 stdout 打印一行英文:loongsuite-site-bootstrap: started successfully (OpenTelemetry auto-instrumentation initialized).(本包自带一个仅绑定在 loongsuite_site_bootstrap logger 上的 StreamHandler,不依赖应用是否已配置 logging。)

卸载

pip uninstall loongsuite-site-bootstrap

卸载后 .pth 会随包移除;若曾手动复制 .pth,需自行清理。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loongsuite_site_bootstrap-0.4.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file loongsuite_site_bootstrap-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for loongsuite_site_bootstrap-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6da595d53777ca65e76ad86aa8710e27b9badc8646b5f89d16c20b866d29ca91
MD5 b7670ad9b640f4941fe0588cbb717693
BLAKE2b-256 a3825a5c68df04cbc50c1a3859c82e7990a15cd62fddec18b19f61a5292a9eea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page