cla-node
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

CLA NODE SDK

Is adapted from airBrake

Hello Cla

Install cla node sdk

npm install cla-node

Quick start:

const Cla = require('cla-node')
cosnt cla = new Cla.Notifier({
  appname: 'app-demo',
  grpc: 'ip:port,ip:port'
})

Attention:

  • appname is required, and used by es as index
  • grpc grpc 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默认会捕获uncaughtExceptionunhandledRejection,如果发现有些异常没有主动上报,检查业务中是否有try/catch的逻辑
  • 统计数据上报,cla.stat('event', {param: 'from news'})
  • 使用koaexpress中间件时,使用makeMiddleware,sdk自动收集并上报请求的cookie中的uuid字段
  • 使用koaexpress中间件时,使用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文件在维护时只能新增字段,不能删减字段

TODO

  • 自定义instrument开发文档
  • [?]node sdk与web sdk的uid共享(web sdk将uuid写入cookie中)
  • [√]加入requestId,一次request的整个流程都带上这个id
  • [√]加入日志级别level
  • [√]stat字段支持:(上报运营数据:pv、点击流等)
  • 调用链
  • [√]优化日志字段,去掉不重要或取不到的字段
  • [√]lint code
  • 更新cla-node sdk的部分到fbi-project-fly-node模板中
  • 日志过滤
    • context.userAgent中包含curl的,这种日志大概率是健康检查探测包
    • context.httpMethodHEAD

Readme

Keywords

none

Package Sidebar

Install

npm i cla-node

Weekly Downloads

1

Version

0.0.9

License

none

Unpacked Size

351 kB

Total Files

37

Last publish

Collaborators

  • aslinwang