skywalking 实现收集基于python的Django项目链路追踪案例
一、python3环境设置
1.1、安装python3
apt-get update
apt install python3-pip -y
pip install "apache-skywalking"
[root@skywalking-agent-07 ~]# pip install "apache-skywalking"
Collecting apache-skywalking WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f29f5b41880>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/3f/2a/8934d1a7d781ec3c16114bad1bc5376bcf506e261c42c6b6718c0418e5a9/apache_skywalking-0.8.0-py3-none-any.whl WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f29f5b41910>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/3f/2a/8934d1a7d781ec3c16114bad1bc5376bcf506e261c42c6b6718c0418e5a9/apache_skywalking-0.8.0-py3-none-any.whl Downloading apache_skywalking-0.8.0-py3-none-any.whl (168 kB) |████████████████████████████████| 168 kB 670 kB/s Collecting packaging Downloading packaging-21.3-py3-none-any.whl (40 kB) |████████████████████████████████| 40 kB 4.2 MB/s Collecting wrapt Downloading wrapt-1.14.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (81 kB) |████████████████████████████████| 81 kB 4.7 MB/s Collecting grpcio-tools Downloading grpcio_tools-1.49.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB) |████████████████████████████████| 2.4 MB 5.0 MB/s Collecting grpcio Downloading grpcio-1.49.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB) |████████████████████████████████| 4.7 MB 41.9 MB/s Collecting pyparsing!=3.0.5,>=2.0.2 Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB) |████████████████████████████████| 98 kB 7.3 MB/s Collecting protobuf<5.0dev,>=4.21.3 Downloading protobuf-4.21.7-cp37-abi3-manylinux2014_x86_64.whl (408 kB) |████████████████████████████████| 408 kB 13.8 MB/s Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from grpcio-tools->apache-skywalking) (45.2.0) Requirement already satisfied: six>=1.5.2 in /usr/lib/python3/dist-packages (from grpcio->apache-skywalking) (1.14.0) Installing collected packages: pyparsing, packaging, wrapt, protobuf, grpcio, grpcio-tools, apache-skywalking Successfully installed apache-skywalking-0.8.0 grpcio-1.49.1 grpcio-tools-1.49.1 packaging-21.3 protobuf-4.21.7 pyparsing-3.0.9 wrapt-1.14.1
1.2、导入模块,验证当前环境是否能正常注册到skywalking server
[root@skywalking-agent-07 ~]# python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from skywalking import agent, config >>> config.init(collector_address='172.16.88.187:11800', service_name='python-app') >>> agent.start() skywalking [MainThread] [WARNING] plugin sw_aiohttp failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_bottle failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_celery failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_django failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_elasticsearch failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_falcon failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_fastapi failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_flask failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_kafka failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_mysqlclient failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_psycopg failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_psycopg2 failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_pymongo failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_pymysql failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_pyramid failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_rabbitmq failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_redis failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_sanic failed to install, please disregard this warning if the corresponding package was not used in your project skywalking [MainThread] [WARNING] plugin sw_tornado failed to install, please disregard this warning if the corresponding package was not used in your project >>>
>>> exit()
[root@skywalking-agent-07 ~]#
二、上传并部署django项目
2.1、上传项目并安装依赖包
[root@skywalking-agent-07 ~]# tar -xf django-test.tgz
[root@skywalking-agent-07 ~]# cd django-test/ [root@skywalking-agent-07 django-test]# ls bin include lib mysite pyvenv.cfg requirements.txt [root@skywalking-agent-07 django-test]# cat requirements.txt apache-skywalking==0.7.0 asgiref==3.4.1 backports.zoneinfo==0.2.1 Django==4.0.1 grpcio==1.43.0 grpcio-tools==1.43.0 packaging==21.3 protobuf==3.19.3 PyMySQL==1.0.2 pyparsing==3.0.6 six==1.16.0 sqlparse==0.4.2 wrapt==1.13.3 [root@skywalking-agent-07 django-test]# pip3 install -r requirements.txt #安装依赖包 Collecting apache-skywalking==0.7.0 Downloading apache_skywalking-0.7.0-py3-none-any.whl (169 kB) |████████████████████████████████| 169 kB 610 kB/s Collecting asgiref==3.4.1 Downloading asgiref-3.4.1-py3-none-any.whl (25 kB) Collecting backports.zoneinfo==0.2.1 Downloading backports.zoneinfo-0.2.1-cp38-cp38-manylinux1_x86_64.whl (74 kB) |████████████████████████████████| 74 kB 2.5 MB/s Collecting Django==4.0.1 Downloading Django-4.0.1-py3-none-any.whl (8.0 MB) |████████████████████████████████| 8.0 MB 4.2 MB/s Collecting grpcio==1.43.0 Downloading grpcio-1.43.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB) |████████████████████████████████| 4.1 MB 27.8 MB/s Collecting grpcio-tools==1.43.0 Downloading grpcio_tools-1.43.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB) |████████████████████████████████| 2.4 MB 10.4 MB/s Requirement already satisfied: packaging==21.3 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 7)) (21.3) Collecting protobuf==3.19.3 Downloading protobuf-3.19.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB) |████████████████████████████████| 1.1 MB 10.6 MB/s Collecting PyMySQL==1.0.2 Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB) |████████████████████████████████| 43 kB 3.9 MB/s Collecting pyparsing==3.0.6 Downloading pyparsing-3.0.6-py3-none-any.whl (97 kB) |████████████████████████████████| 97 kB 12.5 MB/s Collecting six==1.16.0 Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) Collecting sqlparse==0.4.2 Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB) |████████████████████████████████| 42 kB 2.1 MB/s Collecting wrapt==1.13.3 Downloading wrapt-1.13.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (84 kB) |████████████████████████████████| 84 kB 4.7 MB/s Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from grpcio-tools==1.43.0->-r requirements.txt (line 6)) (45.2.0) ERROR: launchpadlib 1.10.13 requires testresources, which is not installed. Installing collected packages: wrapt, six, grpcio, protobuf, grpcio-tools, apache-skywalking, asgiref, backports.zoneinfo, sqlparse, Django, PyMySQL, pyparsing Attempting uninstall: wrapt Found existing installation: wrapt 1.14.1 Uninstalling wrapt-1.14.1: Successfully uninstalled wrapt-1.14.1 Attempting uninstall: six Found existing installation: six 1.14.0 Not uninstalling six at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'six'. No files were found to uninstall. Attempting uninstall: grpcio Found existing installation: grpcio 1.49.1 Uninstalling grpcio-1.49.1: Successfully uninstalled grpcio-1.49.1 Attempting uninstall: protobuf Found existing installation: protobuf 4.21.7 Uninstalling protobuf-4.21.7: Successfully uninstalled protobuf-4.21.7 Attempting uninstall: grpcio-tools Found existing installation: grpcio-tools 1.49.1 Uninstalling grpcio-tools-1.49.1: Successfully uninstalled grpcio-tools-1.49.1 Attempting uninstall: apache-skywalking Found existing installation: apache-skywalking 0.8.0 Uninstalling apache-skywalking-0.8.0: Successfully uninstalled apache-skywalking-0.8.0 Attempting uninstall: pyparsing Found existing installation: pyparsing 3.0.9 Uninstalling pyparsing-3.0.9: Successfully uninstalled pyparsing-3.0.9 Successfully installed Django-4.0.1 PyMySQL-1.0.2 apache-skywalking-0.7.0 asgiref-3.4.1 backports.zoneinfo-0.2.1 grpcio-1.43.0 grpcio-tools-1.43.0 protobuf-3.19.3 pyparsing-3.0.6 six-1.16.0 sqlparse-0.4.2 wrapt-1.13.3 [root@skywalking-agent-07 django-test]# pip3 install -r requirements.txt #再次确认是否遗漏少装 Requirement already satisfied: apache-skywalking==0.7.0 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 1)) (0.7.0) Requirement already satisfied: asgiref==3.4.1 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 2)) (3.4.1) Requirement already satisfied: backports.zoneinfo==0.2.1 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 3)) (0.2.1) Requirement already satisfied: Django==4.0.1 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 4)) (4.0.1) Requirement already satisfied: grpcio==1.43.0 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 5)) (1.43.0) Requirement already satisfied: grpcio-tools==1.43.0 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 6)) (1.43.0) Requirement already satisfied: packaging==21.3 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 7)) (21.3) Requirement already satisfied: protobuf==3.19.3 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 8)) (3.19.3) Requirement already satisfied: PyMySQL==1.0.2 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 9)) (1.0.2) Requirement already satisfied: pyparsing==3.0.6 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 10)) (3.0.6) Requirement already satisfied: six==1.16.0 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 11)) (1.16.0) Requirement already satisfied: sqlparse==0.4.2 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 12)) (0.4.2) Requirement already satisfied: wrapt==1.13.3 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 13)) (1.13.3) Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from grpcio-tools==1.43.0->-r requirements.txt (line 6)) (45.2.0) [root@skywalking-agent-07 django-test]#
2.2、创建django项目mysite
[root@skywalking-agent-07 django-test]# rm -fr mysite/
[root@skywalking-agent-07 django-test]# django-admin startproject mysite
2.3、创建应用
[root@skywalking-agent-07 django-test]# cd mysite/
[root@skywalking-agent-07 mysite]# python3 manage.py startapp myapp
2.4、初始化数据库
[root@skywalking-agent-07 mysite]# python3 manage.py makemigrations
[root@skywalking-agent-07 mysite]# python3 manage.py migrate
2.5、创建管理员, 用于登录
[root@skywalking-agent-07 mysite]# python3 manage.py createsuperuser
[root@skywalking-agent-07 mysite]# python3 manage.py createsuperuser Username (leave blank to use 'root'): root Email address: admin123@qq.com Password: Password (again): This password is too common. Bypass password validation and create user anyway? [y/N]: y Superuser created successfully. [root@skywalking-agent-07 mysite]#
用户密码:root 1qaz@WSX
2.6、运行项目
设置相关环境变量
[root@skywalking-agent-07 mysite]# export SW_AGENT_NAME='python-app1'
[root@skywalking-agent-07 mysite]# export SW_AGENT_NAMESPACE='python-app1'
[root@skywalking-agent-07 mysite]# export SW_AGENT_COLLECTOR_BACKEND_SERVICES='172.16.88.187:11800'
[root@skywalking-agent-07 mysite]# vi mysite/settings.py
28 ALLOWED_HOSTS = ['127.0.0.1','172.16.88.194']
启动项目
[root@skywalking-agent-07 mysite]# sw-python -d run python3 manage.py runserver 172.16.88.194:80
skywalking-cli [DEBUG] Args received Namespace(command=['python3', 'manage.py', 'runserver', '172.16.88.194:80'], debug=True, option='run') skywalking-cli [DEBUG] SkyWalking Python agent with CLI option 'run' and command ['python3', 'manage.py', 'runserver', '172.16.88.194:80'] skywalking-cli [DEBUG] SkyWalking Python agent `runner` received command ['python3', 'manage.py', 'runserver', '172.16.88.194:80'] skywalking-cli [DEBUG] Adding sitecustomize.py to PYTHONPATH skywalking-cli [DEBUG] Updated PYTHONPATH - /usr/local/lib/python3.8/dist-packages/skywalking/bootstrap/loader skywalking-cli [DEBUG] New process starting with file - `python3` args - `['python3', 'manage.py', 'runserver', '172.16.88.194:80']` skywalking-loader [MainThread] [DEBUG] ---------------sitecustomize.py--------------- skywalking-loader [MainThread] [DEBUG] Successfully imported sitecustomize.py from `/usr/local/lib/python3.8/dist-packages/skywalking/bootstrap/loader/sitecustomize.py` skywalking-loader [MainThread] [DEBUG] You are inside working dir - /root/django-test/mysite skywalking-loader [MainThread] [DEBUG] Using Python version - 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] skywalking-loader [MainThread] [DEBUG] Using executable at - /usr/bin/python3 skywalking-loader [MainThread] [DEBUG] System Base Python executable location /usr skywalking-loader [MainThread] [DEBUG] Original sitecustomize module not found, skipping. skywalking-loader [MainThread] [DEBUG] SkyWalking Python Agent starting, loader finished. skywalking [MainThread] [WARNING] failed to install plugin sw_aiohttp skywalking [MainThread] [WARNING] failed to install plugin sw_celery skywalking [MainThread] [WARNING] failed to install plugin sw_elasticsearch skywalking [MainThread] [WARNING] failed to install plugin sw_falcon skywalking [MainThread] [WARNING] failed to install plugin sw_flask skywalking [MainThread] [WARNING] failed to install plugin sw_kafka skywalking [MainThread] [WARNING] failed to install plugin sw_psycopg2 skywalking [MainThread] [WARNING] failed to install plugin sw_pymongo skywalking [MainThread] [WARNING] failed to install plugin sw_pyramid skywalking [MainThread] [WARNING] failed to install plugin sw_rabbitmq skywalking [MainThread] [WARNING] failed to install plugin sw_redis skywalking [MainThread] [WARNING] failed to install plugin sw_sanic skywalking [MainThread] [WARNING] failed to install plugin sw_tornado skywalking-loader [MainThread] [DEBUG] ---------------sitecustomize.py--------------- skywalking-loader [MainThread] [DEBUG] Successfully imported sitecustomize.py from `/usr/local/lib/python3.8/dist-packages/skywalking/bootstrap/loader/sitecustomize.py` skywalking-loader [MainThread] [DEBUG] You are inside working dir - /root/django-test/mysite skywalking-loader [MainThread] [DEBUG] Using Python version - 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] skywalking-loader [MainThread] [DEBUG] Using executable at - /usr/bin/python3 skywalking-loader [MainThread] [DEBUG] System Base Python executable location /usr skywalking-loader [MainThread] [DEBUG] Original sitecustomize module not found, skipping. skywalking-loader [MainThread] [DEBUG] SkyWalking Python Agent starting, loader finished. skywalking [MainThread] [WARNING] failed to install plugin sw_aiohttp skywalking [MainThread] [WARNING] failed to install plugin sw_celery skywalking [MainThread] [WARNING] failed to install plugin sw_elasticsearch skywalking [MainThread] [WARNING] failed to install plugin sw_falcon skywalking [MainThread] [WARNING] failed to install plugin sw_flask skywalking [MainThread] [WARNING] failed to install plugin sw_kafka skywalking [MainThread] [WARNING] failed to install plugin sw_psycopg2 skywalking [MainThread] [WARNING] failed to install plugin sw_pymongo skywalking [MainThread] [WARNING] failed to install plugin sw_pyramid skywalking [MainThread] [WARNING] failed to install plugin sw_rabbitmq skywalking [MainThread] [WARNING] failed to install plugin sw_redis skywalking [MainThread] [WARNING] failed to install plugin sw_sanic skywalking [MainThread] [WARNING] failed to install plugin sw_tornado Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). October 04, 2022 - 03:48:47 Django version 4.0.1, using settings 'mysite.settings' Starting development server at http://172.16.88.194:80/ Quit the server with CONTROL-C.
2.7、检查并验证项目是否正常运行
2.8、验证skywalking server端数据采集