# cla-node

> cla node sdk

Latest version **0.0.9** (published 2019-11-09) · 0 weekly downloads

## Install

```sh
npm install cla-node
pnpm add cla-node
yarn add cla-node
bun add cla-node
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2019-11-09 |
| First published | 2019-10-22 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 343.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | aslinwang |

## Links

- npm: https://www.npmjs.com/package/cla-node
- npm.io page: https://npm.io/package/cla-node

## Dependencies (5)

- [request](https://npm.io/package/request.md) ^2.88.0
- [tdigest](https://npm.io/package/tdigest.md) ^0.1.1
- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.0.4
- [grpc-helper](https://npm.io/package/grpc-helper.md) ^0.5.0
- [error-stack-parser](https://npm.io/package/error-stack-parser.md) ^2.0.4

## Recent versions

- 0.0.9 (latest) — 2019-11-09
- 0.0.8 — 2019-11-09
- 0.0.7 — 2019-11-07
- 0.0.6 — 2019-11-05
- 0.0.5 — 2019-10-24
- 0.0.4 — 2019-10-24
- 0.0.3 — 2019-10-23
- 0.0.2 — 2019-10-22
- 0.0.1 — 2019-10-22

## README

# CLA NODE SDK
> Is adapted from [airBrake](https://github.com/airbrake/airbrake-js)

## 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默认会捕获`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文件在维护时**只能新增字段，不能删减字段**

## 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.httpMethod`为`HEAD`的

---
_Source: https://npm.io/package/cla-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
