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

zipkin-instrumentation-grpc

v0.0.8

Published

Interceptor for instrumenting GRPC calls.

Downloads

182

Readme

zipkin-instrumentation-grpc

This lib is the interceptor for Zipkin to intercept your GRPC calls(unary/stream).

Project test status

  • [x] Tested with Node.js GRPC client <-> Node.js GRPC server

  • [x] Tested with Node.js GRPC client <-> Go GRPC server

How to use

  • Init Zipkin GRPC interceptor

    // Your zipkin base url, like: http://localhost:65534
    const zipkinBaseUrl = 'YOUR_ZIPKIN_BASE_URL';
    
    const CLSContext = require('zipkin-context-cls');
    
    const {Tracer, BatchRecorder, ConsoleRecorder} = require('zipkin');
    const {HttpLogger} = require('zipkin-transport-http');
    
    const recorder = new BatchRecorder({
      logger: new HttpLogger({
        endpoint: `${zipkinBaseUrl}/api/v1/spans`
      })
    });
    
    // `ConsoleRecorder` will be very helpful when you want to debug where is going wrong.
    // const recorder = new ConsoleRecorder();
    
    const ctxImpl = new CLSContext('zipkin');
    
    const tracer = new Tracer({ctxImpl, recorder});
    
    global.ZIPKIN_GRPC_INTCP = new (require('zipkin-instrumentation-grpc'))(tracer);
  • Right before your GRPC client is ready to call the remote GRPC service

    const metadata = global.ZIPKIN_GRPC_INTCP.beforeClientDoGrpcCall({
      serviceName: process.env.MS_SERVICE_TAG,
      remoteGrpcServiceName: 'signInV1',
      xB3Sampled: '1'
    });
    
    // Then do your GRPC call
    yourGrpcClient.remoteGrpcService(reqBody, metadata, (err, resData) => {
      // Your business logic as before ...
    });
  • Upon your GRPC server received the call from your GRPC client

    // Apparently you need to initialize the `ZIPKIN_GRPC_INTCP` for each of your distributed GRPC service.
    
    // Do this at the first line upon the GRPC server received the call from the GRPC client.
    const metadata = global.ZIPKIN_GRPC_INTCP.uponServerRecvGrpcCall({
      serviceName: process.env.MS_SERVICE_TAG,
      grpcMetadataFromIncomingCtx: call.metadata
    });
  • Upon your GRPC server has finished all the respond.

    // Do this upon your GRPC server has finished all the respond.
    global.ZIPKIN_GRPC_INTCP.uponServerFinishRespond();
  • After everything of this GRPC call has been finished at your GRPC client.

    global.ZIPKIN_GRPC_INTCP.afterGrpcCallFinish();
  • For GRPC server side is written in GO.

    func initTracerForZipkin(zipkinUrl, hostPort, serviceName string) {
      if collector, e := zipkin.NewHTTPCollector(zipkinUrl); e == nil {
        if tracer, e0 := zipkin.NewTracer(zipkin.NewRecorder(collector, false, hostPort, serviceName)); e0 == nil {
          opentracing.InitGlobalTracer(tracer)
        } else {
          util.Logger.Panic("Could not init tracer for Zipkin.")
          panic(e0)
        }
      } else {
        util.Logger.Panic("Could not init collector for Zipkin.")
        panic(e)
      }
    }
    
    func main() {
      hostname, _ := os.Hostname()
    
      initTracerForZipkin("YOUR_ZIPKIN_BASE_URL/api/v1/spans", hostname, os.Getenv("MS_SERVICE_TAG"))
    }
    
    // Utils
    type metadataReader struct {
      *metadata.MD
    }
    
    func (mr metadataReader) ForeachKey(handler func(key, val string) error) error {
      for k, vals := range *mr.MD {
        for _, v := range vals {
          if e := handler(k, v); e != nil {
            return e
          }
        }
      }
      return nil
    }
    
    func getIncomingCtxFromGRPC(ctx context.Context, tracer opentracing.Tracer, opName string) context.Context {
      md, _ := metadata.FromIncomingContext(ctx)
    
      if incomingCtx, e := tracer.Extract(opentracing.TextMap, metadataReader{&md}); e == nil && e != opentracing.ErrSpanContextNotFound {
        span := tracer.StartSpan(opName, ext.RPCServerOption(incomingCtx))
        return opentracing.ContextWithSpan(ctx, span)
      } else {
        util.Logger.Panic("Opentracing tracer could not extract metadata from incoming context.")
        panic(e)
      }
    }
    
    // In actual service.
    ctx := getIncomingCtxFromGRPC(stream.Context(), opentracing.GlobalTracer(), "LsyncV1")
    
    if span := opentracing.SpanFromContext(ctx); span != nil {
      defer span.Finish()
    
      ext.SpanKindRPCServer.Set(span)
      span.SetTag("serviceType", "Go GRPC")
    }

Maintainer

Feel free to @lnshi on Github for any issue of this lib 🙂