AI考拉技术分享-Node基础架构专题(三)

前言

转眼新年过了,开始了搬砖的日子。在这里给你们拜个晚年,送上一些小知识,但愿你们好搬砖!html

klg-tracer

链路追踪工具,base on pandoranode

Installation

npm install klg-tracer
复制代码

Node.js >= 8.2.1 required.git

Features

Pandora 提供基于 OpenTracing 标准的链路追踪信息,在此基础上,klg-tracer 自定义了一些 tags,并支持将 tracer 信息写入 mongo。github

QuickStart

1、配合 Pandora 使用,自定义tags

TODOmongodb

  1. export 拓展好的类
  2. 覆盖 Pandora 的默认配置

2、将 tracer 结果写入 Mongo

app.tstypescript

import {TraceService, Tracer} from 'klg-tracer'

new TraceService().registerHooks({
    httpServer: {
      useKoa:true, // 在 koa 设置钩子,比直接在 http 层设置钩子稳定
      // 过滤器,只记录特定接口, 注意 return true 的才会被过滤
      requestFilter: function (req) {
        const urlParsed = url.parse(req.url, true);
        return urlParsed.pathname.indexOf('product/') === -1;
      }
    }
  }).registerMongoReporter({
    mongoUrl: config.database.mongodb[0].url,
    collectionName: 'tracer'
  });

复制代码

完整的配置能够见 src/domainnpm

interface TracerOptions {
  httpServer?: {
    recordGetParams?: boolean,    // 是否记录 query
    recordPostData?: boolean,     // 是否记录 post data
    recordResponse?: boolean,     // 是否记录 response
    requestFilter?: requestFilter,  // 过滤器
    interceptor?: interceptor       // 中间件 TODO
  },
  httpClient?: {
    enabled: boolean, options?: {
      recordGetParams?: boolean,
      recordPostData?: boolean,
      recordResponse?: boolean
    }
  },
  mongodb?: { enabled: boolean, options?: any }
}
复制代码

启动你的 Web 服务并访问,相关的请求信息将会写入 tracer 表中。json

Search:api

db.tracer.find({name : 'http-server'}).sort({_id : -1})
复制代码

Result:bash

{
    "_id" : ObjectId("5ad99bd3f29cf14de64516b3"),
    "tags" : {
        "httpMethod" : "POST",
        "url" : "/api/v1/account/register",
        "data" : {
            "userId" : "5527da927855af35354c39eb",
            "userRole" : "INVESTOR"
        },
        "response" : {
            "code" : 0,
            "message" : "success",
            "data" : {
                "html" : "html"
            }
        }
    },
    "traceId" : "6e11fe95c2035a7a",
    "name" : "http-server",
    "timestamp" : 1524210643694.0,
    "duration" : 152,
    "createdAt" : ISODate("2018-04-20T07:50:43.874Z"),
    "updatedAt" : ISODate("2018-04-20T07:50:43.874Z"),
    "__v" : 0
}
复制代码

Tracer tags

  1. http server
  • http.method
  • http.path // path
  • http.query // query string
  • http.data // post body, only json
  • http.response
  1. http client
  • http.method
  • http.url // path
  • http.hostname // send to where
  • http.port
  • http.query
  • http.data
  • http.response
  • http.response_size
  • http.status_code
  • http.error_code
  1. mongo todo

Test

$ npm i
$ npm test
复制代码

How it works

tracer

implements session with async_hooks and cls-hooked

hook

serve : hack http createServer method, register listener.

http-client : hack http request method, register listener.

ChangeLog

3.0.0

  • 基于 Pandorajs 重作,目前只提供 http-server http-client mongo 三个位置的监听

1.2.0

  • koa-server hook add requestFilter options

1.1.0

  • koa-server hook add intercept options

1.0.3

  • http-client hook trace request parameters and response

1.0.0

  • add http-server koa-server hook
  • add http-client hook
  • add mongo report

常见问题

1 thenable 函数会 break cls 的上下文,像 mongoose 和 superagent 都是在 prototype 里添加 then function 来支持 Promise 的,全部都会有这个问题。 目前只能经过改变写法来避免这个问题,例如:

break session

await User.findOne({})
复制代码

work

await User.findOne({}).then()
复制代码

详情见此 issue github.com/midwayjs/pa…

2 mongodb nodejs driver 3.0 版本升级了 apm 的实现,Pandorajs 还未支持 详情见此 issue github.com/midwayjs/pa…


著做权归本文做者全部,未经受权,请勿转载,谢谢。

相关文章
相关标签/搜索