sasdn-zipkin

1.0.12 • Public • Published

SASDN-Zipkin

use zipkin-transport-kafkapackage, send trace message to kafka and consume from it.

Install

$ npm install --save sasdn-zipkin

Examples

gRPC

gRPC Server Middleware

import {RpcApplication} from 'sasdn';
import {GrpcImpl} from 'sasdn-zipkin';
 
// { 127.0.0.1:9092 } is remote kafka collector url
GrpcImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',    // your service name
  port: 0      // your service port
});
 
const app = new RpcApplication();
app.use(new GrpcImpl().createMiddleware());
app.bind('127.0.0.1:8080').start();

gRPC Client Proxy

import {GrpcImpl} from 'sasdn-zipkin';
import {OrderServiceClient} from './proto/order/order_grpc_pb';
 
// { 127.0.0.1:9092 } is remote kafka collector url
GrpcImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',    // your service name
  port: 0                    // your service port
});
GrpcImpl.setReceiverServiceInfo({
  serviceName: 'ms-order',   // connect service name
  host: '127.0.0.1',         // connect service address
  port: 9090                 // connect service port
});
 
const grpcClient = new OrderServiceClient('127.0.0.1:9090', grpc.credentials.createInsecure());
const proxyClient = new GrpcImpl().createClient(grpcClient, ctx);

Koa

Koa Server Middleware

import * as Koa from 'koa';
import {KoaImpl} from 'sasdn-zipkin';
 
// { 127.0.0.1:9092 } is remote kafka collector url
KoaImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',   // your service name
  port: 0                   // your service port
});
 
const app = new Koa();
app.use(new KoaImpl().createMiddleware());
app.listen('127.0.0.1', 8080);

Typeorm

Typeorm Client Proxy

import {TypeOrmImpl} from 'sasdn-zipkin';
import {User as UserEntity} from './entity/User';
import {createConnection} from 'typeorm';
 
// build entities
const entities = [];
entities.push({UserEntity: UserEntity});
 
// { 127.0.0.1:9092 } is remote kafka collector url
TypeOrmImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',    // your service name
  port: 0                    // your service port
});
TypeOrmImpl.setReceiverServiceInfo({
  serviceName: 'sqlite'
});
  
const conn = await createConnection({
  type: 'sqlite',
  database: './User.db',
  entities: entities,
});
const proxyConn = new TypeOrmImpl().createClient(conn, ctx);

在 zipkin trace 日志中添加自定义数据

SASDN-Zipkin 允许用户在 trace 日志中添加自定义数据,但是为服务端添加自定义数据与为客户端添加自定义数据两种方案,他们执行顺序和触发日志发送的时机是完全不一样的,下面就开始介绍如何添加自定义数据。

为服务端添加自定义数据

通过上面的代码,可以知道服务端是通过 middleware 的方式添加 zipkin trace 的,如果需要添加新的自定义参数,则需要创建新的middleware,并启用。所有日志都是全局的,每个请求都会经过middleware即必然会进行记录.

在 grpc server 的 zipkin trace 记录添加自定义数据:请求中的元数据
import {RpcApplication} from 'sasdn';
import {GrpcImpl, ZIPKIN_EVENT} from 'sasdn-zipkin';
 
const zipkinImpl = new GrpcImpl();
GrpcImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',
  port: 0
});
 
const app = new RpcApplication();
app.use(async (ctx, next) => {
  // 在 Zipkin Event:Server Receive 时,记录请求中的元数据
  zipkinImpl.setCustomizedRecords(ZIPKIN_EVENT.SERVER_RECV, {
    metadata: JSON.stringify(ctx.call.metadata.getMap()) // 自定义数据的 value 值的类型必须是 string
  });
  await next();
});
app.use(zipkinTmpl.createMiddleware());
app.bind('127.0.0.1:8080').start();
在 koa server 的 zipkin trace 记录中添加自定义数据:接口返回状态码
import * as Koa from 'koa';
import {KoaImpl} from 'sasdn-zipkin';
 
const zipkinImpl = new KoaImpl();
// { 127.0.0.1:9092 } is remote kafka collector url
KoaImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',
  port: 0
});
 
const app = new Koa();
app.use(zipkinImpl.createMiddleware());
app.use(async (ctx, next) => {
  // 在 Zipkin Event:Server Send 中,记录请求返回状态码
  zipkinImpl.setCustomizedRecords(ZIPKIN_EVENT.SERVER_SEND, {
    httpCode: ctx.response.status.toString()// 自定义数据的 value 值的类型必须是 string
  });
  await next();
});
app.listen('127.0.0.1', 8080);

为客户端添加自定义数据

为客户端添加自定义数据的方式相对简单,只需要在 createClient 后,执行 Client 的远程调用前,将自定义数据通过 setCustomizedRecords 方法加入到缓存中即可。

在 grpc client的 zipkin trace 记录添加自定义数据:当前时间
import {GrpcImpl, ZIPKIN_EVENT} from 'sasdn-zipkin';
import {OrderServiceClient} from './proto/order/order_grpc_pb';
 
const zipkinImpl = new GrpcImpl();
// { 127.0.0.1:9092 } is remote kafka collector url
GrpcImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',
  port: 0
});
GrpcImpl.setReceiverServiceInfo({
  serviceName: 'ms-order',
  host: '127.0.0.1',
  port: 9090
});
 
const grpcClient = new OrderServiceClient('127.0.0.1:9090', grpc.credentials.createInsecure());
const proxyClient = zipkinImpl.createClient(grpcClient, ctx);
 
// 在 Zipkin Event:Client Send 时,记录当前时间
zipkinImpl.setCustomizedRecords(ZIPKIN_EVENT.CLIENT_SEND, {
    time: new Date().getTime().toString()
});
 
proxyClient.getOrder(request, (err: Error, res: Order) => {
  if (err) {
    throw err;
  }
  console.log(res);
});
在 typeorm client的 zipkin trace 记录添加自定义数据:日志信息
import {TypeOrmImpl} from 'sasdn-zipkin';
import {User as UserEntity} from './entity/User';
import {createConnection} from 'typeorm';
 
// build entities
const entities = [];
entities.push({UserEntity: UserEntity});
 
TypeOrmImpl.init('127.0.0.1:9092', {
  serviceName: 'ms-user',
  port: 0
});
TypeOrmImpl.setReceiverServiceInfo({
  serviceName: 'sqlite'
});
 
const zipkinImpl = new TypeOrmImpl();
const conn = await createConnection({
  type: 'sqlite',
  database: './User.db',
  entities: entities,
});
const proxyConn = zipkinImpl.createClient(conn, ctx);
 
// 在 Zipkin Event:Client Receive 时,记录一个字符串
zipkinImpl.setCustomizedRecords(ZIPKIN_EVENT.CLIENT_RECV, {
    log: "MS-USER > sqlite query finish!"
});
const userEntity = await proxyConn.getRepository(UserEntity)
    .createQueryBuilder('user')
    .where('user.id=:id', {id: request.getId()})
    .getOne();

Readme

Keywords

none

Package Sidebar

Install

npm i sasdn-zipkin

Weekly Downloads

4

Version

1.0.12

License

MIT

Unpacked Size

62.6 kB

Total Files

19

Last publish

Collaborators

  • agreatfool