2

I'm calling GRPC endpoints from Ruby.

The proto endpoints are for example rpc SayHello (HelloRequest) returns (HelloReply), and implemented in Go, so they take context when calling from Go. The generated client supports client.SayHello(ctx context.Context, request *HelloRequest).

However when calling from Ruby it's just client.SayHello(request), where request's type is HelloRequest. How do I pass in the context? I want to do this because the Go endpoint implementations use the context in a variety of ways such as logging.

onepiece
  • 3,279
  • 8
  • 44
  • 63
  • The `Context` is created when a request is received in the go app. You shouldn't be sending a request context. The handler(endpoint) must create its own [context](https://pkg.go.dev/context). – ryuk May 08 '22 at 12:40
  • 1
    It seems that you want to pass some non-request-values ​​into the grpc request. It is actually a problem of the grpc protocol. The grpc request contains not only a request body, but also a request header, which in golang is https://pkg.go.dev/google.golang.org/grpc@v1.46.0/metadata#MD I am not sure whether this request header can be used in ruby's grpc library, and how to use it. – beiping96 May 08 '22 at 13:44
  • The overall process should be that ruby ​​adds some additional parameters such as log-id into the grpc request header when requesting, then golang can get this parameters from grpc request header by `Interceptor` https://pkg.go.dev/google.golang.org/grpc#UnaryServerInterceptor . And then store it in the context.Context. Hopes that can be helpful. – beiping96 May 08 '22 at 13:44

0 Answers0