├── .gitignore ├── .python-version ├── LICENSE ├── README.md ├── part1 └── meta_path1.py ├── part2 └── func_wrapper.py ├── part3 ├── hello.py ├── hook.py └── test.py ├── part4 ├── sitecustomize.py └── usercustomize.py ├── part5 ├── hello.py ├── hook.py ├── sitecustomize.py └── test.py └── part6 ├── agent.py ├── bootstrap ├── __init__.py ├── _hook.py └── sitecustomize.py ├── hello.py └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | -------------------------------------------------------------------------------- /.python-version: -------------------------------------------------------------------------------- 1 | 3.5.1 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 mozillazg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python 探针实现原理 2 | -------------------------------------------------------------------------------- /part1/meta_path1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import sys 3 | 4 | 5 | class MetaPathFinder: 6 | 7 | def find_module(self, fullname, path=None): 8 | print('find_module {}'.format(fullname)) 9 | return MetaPathLoader() 10 | 11 | 12 | class MetaPathLoader: 13 | 14 | def load_module(self, fullname): 15 | print('load_module {}'.format(fullname)) 16 | sys.modules[fullname] = sys 17 | return sys 18 | 19 | sys.meta_path.insert(0, MetaPathFinder()) 20 | 21 | if __name__ == '__main__': 22 | import http 23 | print(http) 24 | print(http.version_info) 25 | -------------------------------------------------------------------------------- /part2/func_wrapper.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import functools 3 | import time 4 | 5 | 6 | def func_wrapper(func): 7 | @functools.wraps(func) 8 | def wrapper(*args, **kwargs): 9 | print('start func') 10 | start = time.time() 11 | result = func(*args, **kwargs) 12 | end = time.time() 13 | print('spent {}s'.format(end - start)) 14 | return result 15 | return wrapper 16 | 17 | 18 | def sleep(n): 19 | time.sleep(n) 20 | return n 21 | 22 | if __name__ == '__main__': 23 | func = func_wrapper(sleep) 24 | print(func(3)) 25 | -------------------------------------------------------------------------------- /part3/hello.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import time 3 | 4 | 5 | def sleep(n): 6 | time.sleep(n) 7 | return n 8 | -------------------------------------------------------------------------------- /part3/hook.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import functools 3 | import importlib 4 | import sys 5 | import time 6 | 7 | _hook_modules = {'hello'} 8 | 9 | 10 | class MetaPathFinder: 11 | 12 | def find_module(self, fullname, path=None): 13 | print('find_module {}'.format(fullname)) 14 | # 其他模块使用默认的 finder 15 | if fullname in _hook_modules: 16 | return MetaPathLoader() 17 | 18 | 19 | class MetaPathLoader: 20 | 21 | def load_module(self, fullname): 22 | print('load_module {}'.format(fullname)) 23 | if fullname in sys.modules: 24 | return sys.modules[fullname] 25 | 26 | # 先从 sys.meta_path 中删除自定义的 finder 27 | # 防止下面执行 import_module 的时候再次触发此 finder 28 | # 从而出现递归调用的问题 29 | finder = sys.meta_path.pop(0) 30 | # 导入 module 31 | module = importlib.import_module(fullname) 32 | 33 | module_hook(fullname, module) 34 | 35 | sys.meta_path.insert(0, finder) 36 | return module 37 | 38 | sys.meta_path.insert(0, MetaPathFinder()) 39 | 40 | 41 | def module_hook(fullname, module): 42 | if fullname == 'hello': 43 | module.sleep = func_wrapper(module.sleep) 44 | 45 | 46 | def func_wrapper(func): 47 | @functools.wraps(func) 48 | def wrapper(*args, **kwargs): 49 | print('start func') 50 | start = time.time() 51 | result = func(*args, **kwargs) 52 | end = time.time() 53 | print('spent {}s'.format(end - start)) 54 | return result 55 | return wrapper 56 | -------------------------------------------------------------------------------- /part3/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import hook 3 | import hello 4 | 5 | hello.sleep(3) 6 | -------------------------------------------------------------------------------- /part4/sitecustomize.py: -------------------------------------------------------------------------------- 1 | print('this is sitecustomize') 2 | -------------------------------------------------------------------------------- /part4/usercustomize.py: -------------------------------------------------------------------------------- 1 | print('this is usercustomize') 2 | -------------------------------------------------------------------------------- /part5/hello.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import time 3 | 4 | 5 | def sleep(n): 6 | time.sleep(n) 7 | return n 8 | -------------------------------------------------------------------------------- /part5/hook.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import functools 3 | import importlib 4 | import sys 5 | import time 6 | 7 | _hook_modules = {'hello'} 8 | 9 | 10 | class MetaPathFinder: 11 | 12 | def find_module(self, fullname, path=None): 13 | print('find_module {}'.format(fullname)) 14 | # 其他模块使用默认的 finder 15 | if fullname in _hook_modules: 16 | return MetaPathLoader() 17 | 18 | 19 | class MetaPathLoader: 20 | 21 | def load_module(self, fullname): 22 | print('load_module {}'.format(fullname)) 23 | if fullname in sys.modules: 24 | return sys.modules[fullname] 25 | 26 | # 先从 sys.meta_path 中删除自定义的 finder 27 | # 防止下面执行 import_module 的时候再次触发此 finder 28 | # 从而出现递归调用的问题 29 | finder = sys.meta_path.pop(0) 30 | # 导入 module 31 | module = importlib.import_module(fullname) 32 | 33 | module_hook(fullname, module) 34 | 35 | sys.meta_path.insert(0, finder) 36 | return module 37 | 38 | sys.meta_path.insert(0, MetaPathFinder()) 39 | 40 | 41 | def module_hook(fullname, module): 42 | if fullname == 'hello': 43 | module.sleep = func_wrapper(module.sleep) 44 | 45 | 46 | def func_wrapper(func): 47 | @functools.wraps(func) 48 | def wrapper(*args, **kwargs): 49 | print('start func') 50 | start = time.time() 51 | result = func(*args, **kwargs) 52 | end = time.time() 53 | print('spent {}s'.format(end - start)) 54 | return result 55 | return wrapper 56 | -------------------------------------------------------------------------------- /part5/sitecustomize.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import hook 3 | -------------------------------------------------------------------------------- /part5/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import hook 3 | import hello 4 | 5 | hello.sleep(3) 6 | -------------------------------------------------------------------------------- /part6/agent.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | import sys 4 | 5 | current_dir = os.path.dirname(os.path.realpath(__file__)) 6 | boot_dir = os.path.join(current_dir, 'bootstrap') 7 | 8 | 9 | def main(): 10 | args = sys.argv[1:] 11 | os.environ['PYTHONPATH'] = boot_dir 12 | # 执行后面的 python 程序命令 13 | # sys.executable 是 python 解释器程序的绝对路径 ``which python`` 14 | # >>> sys.executable 15 | # '/usr/local/var/pyenv/versions/3.5.1/bin/python3.5' 16 | os.execl(sys.executable, sys.executable, *args) 17 | 18 | if __name__ == '__main__': 19 | main() 20 | -------------------------------------------------------------------------------- /part6/bootstrap/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozillazg/apm-python-agent-principle/cd2b16b3c5d39153eeae10a436b57916990ece1d/part6/bootstrap/__init__.py -------------------------------------------------------------------------------- /part6/bootstrap/_hook.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import functools 3 | import importlib 4 | import sys 5 | import time 6 | 7 | _hook_modules = {'hello'} 8 | 9 | 10 | class MetaPathFinder: 11 | 12 | def find_module(self, fullname, path=None): 13 | print('find_module {}'.format(fullname)) 14 | # 其他模块使用默认的 finder 15 | if fullname in _hook_modules: 16 | return MetaPathLoader() 17 | 18 | 19 | class MetaPathLoader: 20 | 21 | def load_module(self, fullname): 22 | print('load_module {}'.format(fullname)) 23 | if fullname in sys.modules: 24 | return sys.modules[fullname] 25 | 26 | # 先从 sys.meta_path 中删除自定义的 finder 27 | # 防止下面执行 import_module 的时候再次触发此 finder 28 | # 从而出现递归调用的问题 29 | finder = sys.meta_path.pop(0) 30 | # 导入 module 31 | module = importlib.import_module(fullname) 32 | 33 | module_hook(fullname, module) 34 | 35 | sys.meta_path.insert(0, finder) 36 | return module 37 | 38 | sys.meta_path.insert(0, MetaPathFinder()) 39 | 40 | 41 | def module_hook(fullname, module): 42 | if fullname == 'hello': 43 | module.sleep = func_wrapper(module.sleep) 44 | 45 | 46 | def func_wrapper(func): 47 | @functools.wraps(func) 48 | def wrapper(*args, **kwargs): 49 | print('start func') 50 | start = time.time() 51 | result = func(*args, **kwargs) 52 | end = time.time() 53 | print('spent {}s'.format(end - start)) 54 | return result 55 | return wrapper 56 | -------------------------------------------------------------------------------- /part6/bootstrap/sitecustomize.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import _hook 3 | -------------------------------------------------------------------------------- /part6/hello.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import time 3 | 4 | 5 | def sleep(n): 6 | time.sleep(n) 7 | return n 8 | -------------------------------------------------------------------------------- /part6/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import sys 3 | import hello 4 | 5 | print(sys.argv) 6 | print(hello.sleep(3)) 7 | --------------------------------------------------------------------------------