grpc-gateway icon indicating copy to clipboard operation
grpc-gateway copied to clipboard

Support OpenCensus trace propagation

Open tmc opened this issue 7 years ago • 10 comments

If not direct support we should have clear documentation on how to pass traces across http<>grpc boundaries.

tmc avatar Jun 19 '18 04:06 tmc

I was actually just looking at this. I don't quite see an avenue yet but I'll take a deeper look later today.

matt0x6F avatar Jul 11 '18 19:07 matt0x6F

Also very interested in this. Has anyone got it working?

Azuka avatar Sep 05 '18 23:09 Azuka

Does anyone have any examples with this?

jon-whit avatar Sep 24 '18 15:09 jon-whit

I haven't used opencensus with the gateway myself but I imagine it's just a matter of creating an interceptor that extracts the trace from the incoming metadata and stuffs it back in the context?

johanbrandhorst avatar Sep 24 '18 19:09 johanbrandhorst

Can be set on the clients grpc options and the http handlers like:

conn, _ := grpc.Dial(addr, grpc.WithStatsHandler(&ocgrpc.ClientHandler{})
mux  := &ochttp.Handler{Handler: runtime.NewServeMux()}

servicepb.RegisterRouteHandler(ctx, mux, conn)

emcfarlane avatar Jan 15 '19 16:01 emcfarlane

@afking Any example on this. I am facing this issue and seeing different spans(ochttp,ocsql) for the same request. Thanks

dharmjit avatar Jan 24 '19 16:01 dharmjit

If your spans are not contiguous you must not be passing the context along properly. Are you making sure to use the database/sql methods that use context?

johanbrandhorst avatar Jan 24 '19 16:01 johanbrandhorst

I might be making some silly mistake. Below is the sample of the code of the server

// RunServer runs HTTP/REST gateway
func RunServer(ctx context.Context, grpcPort, httpPort string) error {
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	mux := runtime.NewServeMux()
	opts := []grpc.DialOption{grpc.WithInsecure()}
	if err := v1.RegisterAuthServiceHandlerFromEndpoint(ctx, mux, "localhost:"+grpcPort, opts); err != nil {
		// log.Fatalf("failed to start HTTP gateway: %v", err)
		logger.Log.Fatal("failed to start HTTP gateway", zap.String("reason", err.Error()))
	}
	srv := &http.Server{
		Addr:    ":" + httpPort,
		Handler: &ochttp.Handler{Handler: 
			// middleware.AddLogger(logger.Log, mux)},
			mux},
	}
	// log.Println("starting HTTP/REST gateway...")
	logger.Log.Info("starting HTTP/REST gateway...")
	return srv.ListenAndServe()
}

and below is the implementation of the rpc method

func (s *authServiceServer) Login(ctx context.Context, req *v1.LoginRequest) (*v1.LoginResponse,error){
	// get SQL connection from pool
	c, err := s.connect(ctx)

	if err != nil {
		return nil, err
	}
	defer c.Close()

	// query User by ID and Password
	cCtx, cSpan := trace.StartSpan(ctx, "Select")
	rows, err := c.QueryContext(cCtx, "SELECT count(1) FROM user123")
	if err != nil {
		return nil, status.Error(codes.Unknown, "failed to select from user-> "+err.Error())
	}
	cSpan.End()
	fmt.Printf("number of rows 1:%d",getCount(rows))
	defer rows.Close()
	return &v1.LoginResponse{Username:"1"},nil
}

dharmjit avatar Jan 24 '19 17:01 dharmjit

This is not the place for this type of debugging, perhaps join the #opencensus channel on the gophers slack? https://invite.slack.golangbridge.org/

johanbrandhorst avatar Jan 24 '19 17:01 johanbrandhorst

Ahh okk I will post it there. This was somewhat related to grpc-gateway so I posted here.

dharmjit avatar Jan 24 '19 17:01 dharmjit