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

栏目: Node.js · 发布时间: 7年前

内容简介:转眼新年过了,开始了搬砖的日子。在这里给大家拜个晚年,送上一些小知识,希望大家好搬砖!链路追踪工具,base onNode.js >= 8.2.1 required.

转眼新年过了,开始了搬砖的日子。在这里给大家拜个晚年,送上一些小知识,希望大家好搬砖!

klg-tracer

链路追踪工具,base on pandora

Installation

npm install klg-tracer
复制代码

Node.js >= 8.2.1 required.

Features

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

QuickStart

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

TODO

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

二、将 tracer 结果写入 Mongo

app.ts

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/domain

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 表中。

Search:

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

Result:

{
    "_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 withasync_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…

著作权归本文作者所有,未经授权,请勿转载,谢谢。


以上所述就是小编给大家介绍的《AI考拉技术分享-Node基础架构专题(三)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Visual Thinking

Visual Thinking

Colin Ware / Morgan Kaufmann / 2008-4-18 / USD 49.95

Increasingly, designers need to present information in ways that aid their audiences thinking process. Fortunately, results from the relatively new science of human visual perception provide valuable ......一起来看看 《Visual Thinking》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器