Created
February 3, 2026 07:24
-
-
Save tedmax100/8ba6994de823e73c9919a3480d645d4b to your computer and use it in GitHub Desktop.
python_otel_zero_code_instrument_list_simulate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """檢查當前環境中可用的 OpenTelemetry instrumentation - 模擬 opentelemetry-bootstrap""" | |
| import re | |
| import importlib.metadata | |
| from opentelemetry.instrumentation.bootstrap_gen import libraries | |
| def get_installed_packages(): | |
| """獲取所有已安裝的套件及其版本""" | |
| packages = {} | |
| for dist in importlib.metadata.distributions(): | |
| try: | |
| name = dist.metadata['Name'] | |
| version = dist.metadata.get('Version', '') | |
| packages[name.lower().replace('_', '-')] = { | |
| 'name': name, | |
| 'version': version | |
| } | |
| except (KeyError, AttributeError): | |
| continue | |
| return packages | |
| def parse_library_name(library_spec): | |
| """從 libray 規範中提取包名 (例如: 'openai >= 1.26.0' -> 'openai')""" | |
| # 移除版本要求,只保留包名 | |
| match = re.match(r'^([a-zA-Z0-9_-]+)', library_spec.strip()) | |
| if match: | |
| return match.group(1).lower().replace('_', '-') | |
| return None | |
| def parse_instrumentation_name(inst_spec): | |
| """從 instrumentation 規範中提取包名""" | |
| match = re.match(r'^([a-zA-Z0-9_-]+)', inst_spec.strip()) | |
| if match: | |
| return match.group(1).lower().replace('_', '-') | |
| return None | |
| def main(): | |
| installed_packages = get_installed_packages() | |
| print("=" * 80) | |
| print("🔍 OpenTelemetry Bootstrap 檢查 (模擬 opentelemetry-bootstrap -a install)") | |
| print("=" * 80) | |
| # 分析所有支持的 instrumentation | |
| already_installed = [] | |
| need_to_install = [] | |
| not_applicable = [] | |
| for lib_info in libraries: | |
| lib_spec = lib_info.get('library', '') | |
| inst_spec = lib_info.get('instrumentation', '') | |
| lib_name = parse_library_name(lib_spec) | |
| inst_name = parse_instrumentation_name(inst_spec) | |
| if not lib_name or not inst_name: | |
| continue | |
| has_library = lib_name in installed_packages | |
| has_instrumentation = inst_name in installed_packages | |
| if has_library and has_instrumentation: | |
| already_installed.append({ | |
| 'library': lib_spec, | |
| 'library_name': lib_name, | |
| 'instrumentation': inst_spec, | |
| 'inst_name': inst_name | |
| }) | |
| elif has_library and not has_instrumentation: | |
| need_to_install.append({ | |
| 'library': lib_spec, | |
| 'library_name': lib_name, | |
| 'instrumentation': inst_spec, | |
| 'inst_name': inst_name | |
| }) | |
| # 顯示結果 | |
| print(f"\n✅ 已安裝且已配置 instrumentation ({len(already_installed)} 個):") | |
| print("-" * 80) | |
| if already_installed: | |
| for item in already_installed: | |
| lib_ver = installed_packages[item['library_name']]['version'] | |
| inst_ver = installed_packages[item['inst_name']]['version'] | |
| print(f" ✓ {item['library_name']} ({lib_ver})") | |
| print(f" └─ {item['inst_name']} ({inst_ver})") | |
| else: | |
| print(" (無)") | |
| print(f"\n💡 需要安裝的 instrumentation ({len(need_to_install)} 個):") | |
| print("-" * 80) | |
| if need_to_install: | |
| print("你已安裝以下庫,但缺少對應的 OpenTelemetry instrumentation:\n") | |
| for item in need_to_install: | |
| lib_ver = installed_packages[item['library_name']]['version'] | |
| print(f" • {item['library_name']} ({lib_ver})") | |
| print(f" └─ 需要: {item['instrumentation']}") | |
| print(f"\n🚀 快速安裝命令:") | |
| print("-" * 80) | |
| inst_list = [item['instrumentation'] for item in need_to_install] | |
| # 分批顯示(避免命令太長) | |
| if len(inst_list) <= 5: | |
| print(f" pip install {' '.join(inst_list)}") | |
| else: | |
| for inst in inst_list: | |
| print(f" pip install {inst}") | |
| else: | |
| print(" ✓ 所有已安裝的庫都已配置好對應的 instrumentation!") | |
| print(f"\n📊 統計:") | |
| print("-" * 80) | |
| print(f" 已配置: {len(already_installed)} 個") | |
| print(f" 需安裝: {len(need_to_install)} 個") | |
| print(f" 支持總數: {len(libraries)} 個") | |
| print("=" * 80) | |
| print("\n💡 提示:") | |
| print(" 這個腳本模擬了 'opentelemetry-bootstrap -a requirements' 的功能") | |
| print(" 使用 'pip install <package>' 來安裝缺少的 instrumentation") | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """獲取並顯示 OpenTelemetry 自動埋點插件清單""" | |
| import json | |
| import sys | |
| from typing import List, Dict, Any | |
| def get_instrumentation_libraries(limit: int = None) -> List[Dict[str, Any]]: | |
| """ | |
| 獲取 OpenTelemetry 自動埋點插件清單 | |
| Args: | |
| limit: 限制返回的插件數量,None 表示不限制 | |
| Returns: | |
| 插件清單 | |
| """ | |
| try: | |
| from opentelemetry.instrumentation.bootstrap_gen import libraries | |
| except ImportError as e: | |
| print(f"錯誤:無法導入 OpenTelemetry 模組:{e}", file=sys.stderr) | |
| print("請執行:pip install opentelemetry-instrumentation", file=sys.stderr) | |
| sys.exit(1) | |
| if not libraries: | |
| print("警告:未找到任何插件", file=sys.stderr) | |
| return [] | |
| return libraries[:limit] if limit else libraries | |
| def display_libraries(libraries: List[Dict[str, Any]]) -> None: | |
| """ | |
| 顯示插件清單 | |
| Args: | |
| libraries: 插件清單 | |
| """ | |
| total = len(libraries) | |
| print(f"--- OTel 自動埋點插件清單 (共 {total} 筆) ---") | |
| print(json.dumps(libraries, indent=2, ensure_ascii=False)) | |
| print(f"\n總計:{total} 個插件") | |
| def main(): | |
| """主函數""" | |
| # 可以根據需要修改限制數量,None 表示顯示全部 | |
| limit = None # 改為 100 可以只顯示前 100 筆 | |
| libraries = get_instrumentation_libraries(limit) | |
| display_libraries(libraries) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment