Pinpoint添加python监控项-flask
一、部署pinpointPy module
1、安装pinpointPy模块
说明: 此模块需要在项目所在的虚拟环境进行安装(如果每个项目都有自己的虚拟环境,则对应的虚拟环境都要安装此模块)
#cd /opt/pinpoint-c-agent/
#git fetch
#git checkout v0.3.1
#source /data/virtualenv/xinyongfei-bi-model-test1/bin/activate 进入项目的虚拟环境
#python setup.py install
......
[100%] Built target pinpoint_common
building 'pinpointPy' extension
creating build/src/PY
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Icommon/include -I/data/virtualenv/pinpointPy/include -I/opt/website/python3.6/include/python3.6m -c src/PY/pinpoint_py.c -o build/src/PY/pinpoint_py.o
creating build/lib.linux-x86_64-3.6
gcc -pthread -shared build/src/PY/pinpoint_py.o -Lcommon/lib -lpinpoint_common -ljsoncpp -lrt -lstdc++ -o build/lib.linux-x86_64-3.6/pinpointPy.cpython-36m-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.6/pinpointPy.cpython-36m-x86_64-linux-gnu.so -> /data/virtualenv/pinpointPy/lib/python3.6/site-packages
running install_egg_info
Writing /data/virtualenv/pinpointPy/lib/python3.6/site-packages/pinpointPy-0.0.1.3-py3.6.egg-info
二、安装部署Collector-agent
1、安装Collector-agent
#/opt/website/python3.6/bin/python3.6 -m venv collector-agent-env 创建Collector-agent的虚拟环境
#source /opt/pinpoint-c-agent/collector-agent-env/bin/activate
#cd /opt/pinpoint-c-agent/collector-agent
(collector-agent-env) [root@sfytest4 collector-agent]# pip3.6 install -r requirements.txt
......
Collecting thrift==0.11.0 (from -r requirements.txt (line 8))
Downloading http://mirrors.aliyun.com/pypi/packages/c6/b4/510617906f8e0c5660e7d96fbc5585113f83ad547a3989b80297ac72a74c/thrift-0.11.0.tar.gz (52kB)
100% |████████████████████████████████| 61kB 40kB/s
Collecting setproctitle==1.1.10 (from -r requirements.txt (line 9))
Requirement already satisfied: setuptools in /opt/pinpoint-c-agent/collector-agent-env/lib/python3.6/site-packages (from protobuf==3.9.1->-r requirements.txt (line 5)) (40.6.2)
Installing collected packages: greenlet, gevent, six, grpcio, protobuf, grpcio-tools, psutil, thrift, setproctitle
Running setup.py install for psutil ... done
Running setup.py install for thrift ... done
Successfully installed gevent-1.4.0 greenlet-0.4.15 grpcio-1.26.0 grpcio-tools-1.26.0 protobuf-3.9.1 psutil-5.6.6 setproctitle-1.1.10 six-1.12.0 thrift-0.11.0
You are using pip version 18.1, however version 20.2b1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
2、配置Collector-agent
1、创建日志目录
#mkdir /data1/logs/collector-agent
2、修改配置文件
#cd /opt/pinpoint-c-agent/collector-agent/conf
# vim collector.conf
[Collector]
AgentID=dev
ApplicationName=dev-app
# 1500 is PHP
ServerType=1500
# CollectorSpanIp=collectorHost
# CollectorSpanPort=port
# CollectorStatIp=collectorHost
# CollectorStatPort=port
# CollectorTcpIp=collectorHost
# CollectorTcpPort=port
#以下内容请对应服务器端的pinpoint-collector-grpc.properties文件配置
collector.grpc.agent.ip=39.106.50.135
collector.grpc.agent.port=9991
collector.grpc.stat.ip=39.106.50.135
collector.grpc.stat.port=9992
collector.grpc.span.ip=39.106.50.135
collector.grpc.span.port=9993
collector.grpc.discardpolicy.maxpendingthreshold=10000
[Common]
#设置项目的服务端口
Web_Port=80
#collector-agent的日志
Log_Level=DEBUG
LOG_DIR=/data1/logs/collector-agent/
[Agent]
#the same as below "pinpoint_php.CollectorHost"
Address=/tmp/collector-agent.sock #sock文件的目录,这里配置的是sock的方式和模块建立通信
3、添加配置文件的环境变量
##配置collector-agent的环境变量
# vim /etc/profile
export COLLECTOR_CONFIG=/opt/pinpoint-c-agent/collector-agent/conf/collector.conf
#source /etc/profile
3、运行collector-agent
(collector-agent-env) [root@sfytest4 collector-agent]#pip install --upgrade pip
(collector-agent-env) [root@sfytest4 collector-agent]#cd /opt/pinpoint-c-agent/collector-agent
(collector-agent-env) [root@sfytest4 collector-agent]#./init_python_env.sh
(collector-agent-env) [root@sfytest4 collector-agent]# python run.py &
(collector-agent-env) [root@sfytest4 collector-agent]# ps -ef |grep "python run.py"
root 6904 4716 0 14:51 pts/0 00:00:00 python run.py
三、项目中添加pinpoint监控任务
说明: flask应用为例
1、准备工作
1、准备前期的配置文件
# cp -r /opt/pinpoint-c-agent/Example/PY/flask/pinpoint /data/code/bi-model-test1/
# cp -r /opt/pinpoint-c-agent/Example/PY/plugins /data/code/bi-model-test1/pinpoint/
2、修改项目配置
#cd /data/code/bi-model-test1
# vim routes.py
from pinpoint.PinPointMiddleWare import PinPointMiddleWare
from pinpoint.plugins.PinpointCommonPlugin import PinpointCommonPlugin
app.wsgi_app = PinPointMiddleWare(app,app.wsgi_app)
@PinpointCommonPlugin('', __name__)
def work():
logger.info('PinpointCommonPlugin works')
return 'PinpointCommonPlugin'
@app.route('/')
@app.route('/index')
def index():
logger.info(f"function index: {app} ,id(app):{id(app)} ,os.getpid(): {os.getpid()}")
w = work()
return f'<h1>It works!{w}<h1>\n'
3、修改项目pinpoint中APP的ID和NAME以及sock配置
#cd /data/code/bi-model-test1/pinpoint/plugins
vim PinpointCommon.py
# user should set below before use
APP_ID ='bi-model-test1' # application id
APP_NAME ='bi-model-test1' # application name
COLLECTOR_HOST='unix:/tmp/collector-agent.sock'

浙公网安备 33010602011771号