简介
使用 elastic-apm-node 封装 egg 插件,完成对请求路由链路日志的上传elk+apm平台,分析链路中逻辑消耗的时间
参考文档:
- http://claude-ray.com/2019/07/12/elastic-apm-node-egg/
- https://code.yeezon.com/zen/egg-apm
- http://www.zmscode.cn/2020/07/09/docker-compose部署eak/
完成功能
- 应用路由链路上传
- 应用错误对象上传
使用
配置
添加插件 config/plugin.js
moduleexports = eggApmAgent: enable: true package: 'egg-apm-agent' // env: [ 'prod' ], // 建议只在生产环境中启用
修改 package.json ,在egg之前加载apm-agent模块
添加启动脚本 start.sh,请注意修改脚本中的配置信息
# !/bin/sh export ELASTIC_APM_SERVICE_NAME=egg-elk-apm # 服务名称 export ELASTIC_APM_SERVER_URL='http://127.0.0.1:8200' # apm-server 地址 export ELASTIC_APM_ENVIRONMENT='development' # 启动环境 export ELASTIC_APM_ACTIVE=true #建议只在生产环境开启apm-agent export ELASTIC_APM_CENTRAL_CONFIG=false #在单点部署的时候,不需要开启轮询获取最新的apm-server配置 export ELASTIC_APM_CAPTURE_BODY="all" echo service start... npm run debug echo service end!!
以下不是必须
可以通过添加 /app/middleware/apmRouter.js 中间件 , 自定义 transaction 名称,之所以不集成到插件中,是因为场景不同可能需要进行修改
'use strict'; { const layer = stack; const protoPath = layer && layerpath; return typeof protoPath === 'string' ? protoPath : 'unknown route';} module { return async { const path = ; ctxappapm; // fix unknown router await ; };};
添加 app.js 加载中间件
'use strict'; module { // 中间件,修正apm transaction unknown router appconfigmiddleware;};
启动
yarn add egg-apm-agent # 默认 debug 模式 sh ./start.sh