0.0.9 • Published 6 years ago
cla-node v0.0.9
CLA NODE SDK
Is adapted from airBrake
Hello Cla
Install cla node sdk
npm install cla-nodeQuick start:
const Cla = require('cla-node')
cosnt cla = new Cla.Notifier({
  appname: 'app-demo',
  grpc: 'ip:port,ip:port'
})Attention:
appnameis required, and used by es as indexgrpcgrpc IP端口,多个用逗号分隔以负载均衡,注意可能会影响日志时序
API
cla.notify(error)
发送error消息 error参数结构
- error is object
 
| field | comment | 
|---|---|
| error | JS Error instance | 
| context | error context | 
| context.userAddr | request ip | 
| context.userAgent | user agent | 
| context.url | express request url | 
| context.httpMethod | http method | 
| context.component | web framework | 
| context.history | log history, up to 20 records | 
| context.route | express route | 
| context.action | express action | 
| context.referer | http referer | 
| params | null allowed | 
| environment | null allowed | 
| session | null allowed | 
- error is not object
 
cla.scope().pushHistory(log)
记录应用中的日志,在发送消息时作为history一起上报 log参数结构
| field | comment | 
|---|---|
| type | log类型 | 
| severity | log级别 | 
| arguments | log内容 | 
cla.routes.start(method, route, statusCode, contentType)与cla.routes.notify(metric)成对使用
监控并上报web框架的路由质量,统计周期为15s 参数结构
| field | comment | 
|---|---|
| method | http method | 
| route | 路由规则 | 
| statusCode | http相应status码 | 
| contentType | http相应content-type | 
| metric | start方法的返回值 | 
metric=cla.scope().metric()/metric.startSpan(spanName)/metric.isRecording()/metric.endSpan(spanName)
需要结合cla.routes.notify使用
Performance Monit
- web框架中,每次路由可能包含的耗时操作有:sql执行、redis存取等,通过metric计算耗时操作,在
routes.notify中带上来,就能分析这些中间件的耗时情况了 
Attention
- 引用和初始化sdk放在程序执行的第一步,让后续操作产生的日志都能通过sdk进行上报
 - sdk默认会捕获
uncaughtException和unhandledRejection,如果发现有些异常没有主动上报,检查业务中是否有try/catch的逻辑 - 统计数据上报,
cla.stat('event', {param: 'from news'}) - 使用
koa或express中间件时,使用makeMiddleware,sdk自动收集并上报请求的cookie中的uuid字段 - 使用
koa或express中间件时,使用makeMiddleware,sdk自动生成requestId并且在response header中以X-Request-Id字段返回;如果在http context中执行console.log({requestId: ctx.requestId})(koa)或console.log({requestId: req.requestId})(express),requestId信息也会出现在console日志中 - 为了保证服务端接收数据的完整性,pb文件在维护时只能新增字段,不能删减字段