npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sasdn-zipkin

v1.0.12

Published

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

Downloads

8

Readme

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();