grpc-gateway
grpc-gateway copied to clipboard
Support OpenCensus trace propagation
If not direct support we should have clear documentation on how to pass traces across http<>grpc boundaries.
I was actually just looking at this. I don't quite see an avenue yet but I'll take a deeper look later today.
Also very interested in this. Has anyone got it working?
Does anyone have any examples with this?
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?
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)
@afking Any example on this. I am facing this issue and seeing different spans(ochttp,ocsql) for the same request. Thanks
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?
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
}
This is not the place for this type of debugging, perhaps join the #opencensus channel on the gophers slack? https://invite.slack.golangbridge.org/
Ahh okk I will post it there. This was somewhat related to grpc-gateway so I posted here.