Node 监控SDK
介绍
1. install
mnpm i -S nodejsmonitmnpm --unsafe-perm=true --allow-root
2. use
Express:
const express = require("express");
const app = express();
const nodejsmonit = require("nodejsmonitmnpm");
app.use(
nodejsmonit({
callback: function (time, req) {
console.log(`${req.method} ${req.url} res time ${time}`);
}
})
);
Koa:
import Koa from 'koa'
const app = Koa();
const nodejsmonit = require("@mc/nodejsmonit");
app.use(
nodejsmonit({
type:'koa',
callback: function (time, req) {
console.log(`${req.method} ${req.url} res time ${time}`);
}
})
);
3 OPTION
- type [koa || express defalut express]
- env [default process.env.NODE_ENV]
- rootDir [项目根目录 default process.cwd()]
- header [设置header 为空或者false 则不设置 defautl X-Response-Time]
- appCode [ops 项目名称]
- debug [调试模式]
- callback [响应回调 default empty]
- force [default false 强制上传忽略环境变量]
- dynamicUrl[defalut [] 动态地址 用于匹配:
/api/123/common/3423
, 设置DEMO:/api/*/commom/*
]
4 调试
1, 项目根目录增加 devops 目录 创建配置文件:appmeta.properties
devops.app.id=3028
devops.app.code=helpdesk-api
devops.env.id=3
devops.env.code=test
devops.group.id=4232
devops.group.code=default
devops.instance.id=helpdesk-api-7
devops.instance.db.primary=23354
devops.local.ip=192.168.248.169
devops.datacenter.code=az
2,
const express = require("express");
const app = express();
const nodejsmonit = require("@mc/nodejsmonit");
app.use(
nodejsmonit({
debug: true, // 开启调试模式
callback: function (time, req) {
console.log(`${req.method} ${req.url} res time ${time}`);
}
})
);
3,
pm2 start app.js // 必须用pm2 启动
注意:默认只有生产环境会上报监控数据