In this chapter, we will modify the previously introduced HTTP Client&Server example to a GRPC microservice and demonstrate how to develop a simple GRPC server and client using the GoFrame framework and add tracing features to the GRPC microservice.

The example code for this chapter is located at: https://github.com/gogf/gf/tree/master/example/trace/grpc_with_db

Directory Structure

Tracing - GRPC Example - 图1

Protobuf

  1. syntax = "proto3";
  2. package user;
  3. option go_package = "protobuf/user";
  4. // User service for tracing demo.
  5. service User {
  6. rpc Insert(InsertReq) returns (InsertRes) {}
  7. rpc Query(QueryReq) returns (QueryRes) {}
  8. rpc Delete(DeleteReq) returns (DeleteRes) {}
  9. }
  10. message InsertReq {
  11. string Name = 1; // v: required#Please input user name.
  12. }
  13. message InsertRes {
  14. int32 Id = 1;
  15. }
  16. message QueryReq {
  17. int32 Id = 1; // v: min:1#User id is required for querying.
  18. }
  19. message QueryRes {
  20. int32 Id = 1;
  21. string Name = 2;
  22. }
  23. message DeleteReq {
  24. int32 Id = 1; // v:min:1#User id is required for deleting.
  25. }
  26. message DeleteRes {}

Use the gf gen pb command to compile this proto file, which will generate the corresponding grpc interface and data structure files.

GRPC Server

  1. package main
  2. import (
  3. _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/contrib/registry/etcd/v2"
  6. "github.com/gogf/gf/example/trace/grpc_with_db/protobuf/user"
  7. "context"
  8. "fmt"
  9. "time"
  10. "github.com/gogf/gf/contrib/rpc/grpcx/v2"
  11. "github.com/gogf/gf/contrib/trace/otlpgrpc/v2"
  12. "github.com/gogf/gf/v2/database/gdb"
  13. "github.com/gogf/gf/v2/frame/g"
  14. "github.com/gogf/gf/v2/os/gcache"
  15. "github.com/gogf/gf/v2/os/gctx"
  16. )
  17. type Controller struct {
  18. user.UnimplementedUserServer
  19. }
  20. const (
  21. serviceName = "otlp-grpc-server"
  22. endpoint = "tracing-analysis-dc-bj.aliyuncs.com:8090"
  23. traceToken = "******_******"
  24. )
  25. func main() {
  26. grpcx.Resolver.Register(etcd.New("127.0.0.1:2379"))
  27. var ctx = gctx.New()
  28. shutdown, err := otlpgrpc.Init(serviceName, endpoint, traceToken)
  29. if err != nil {
  30. g.Log().Fatal(ctx, err)
  31. }
  32. defer shutdown()
  33. // Set ORM cache adapter with redis.
  34. g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(g.Redis()))
  35. s := grpcx.Server.New()
  36. user.RegisterUserServer(s.Server, &Controller{})
  37. s.Run()
  38. }
  39. // Insert is a route handler for inserting user info into database.
  40. func (s *Controller) Insert(ctx context.Context, req *user.InsertReq) (res *user.InsertRes, err error) {
  41. result, err := g.Model("user").Ctx(ctx).Insert(g.Map{
  42. "name": req.Name,
  43. })
  44. if err != nil {
  45. return nil, err
  46. }
  47. id, _ := result.LastInsertId()
  48. res = &user.InsertRes{
  49. Id: int32(id),
  50. }
  51. return
  52. }
  53. // Query is a route handler for querying user info. It firstly retrieves the info from redis,
  54. // if there's nothing in the redis, it then does db select.
  55. func (s *Controller) Query(ctx context.Context, req *user.QueryReq) (res *user.QueryRes, err error) {
  56. err = g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
  57. Duration: 5 * time.Second,
  58. Name: s.userCacheKey(req.Id),
  59. Force: false,
  60. }).WherePri(req.Id).Scan(&res)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return
  65. }
  66. // Delete is a route handler for deleting specified user info.
  67. func (s *Controller) Delete(ctx context.Context, req *user.DeleteReq) (res *user.DeleteRes, err error) {
  68. err = g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
  69. Duration: -1,
  70. Name: s.userCacheKey(req.Id),
  71. Force: false,
  72. }).WherePri(req.Id).Scan(&res)
  73. return
  74. }
  75. func (s *Controller) userCacheKey(id int32) string {
  76. return fmt.Sprintf(`userInfo:%d`, id)
  77. }

Brief explanation of the server code:

  1. First, the server needs to initialize Jaeger through the jaeger.Init method.

  2. As you can see, the business logic is entirely consistent with the previous HTTP example project, only the access layer is modified for the GRPC protocol.

  3. We still inject Redis cache via the cache adapter:

  1. g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(g.Redis()))
  1. Here, we also enable the cache feature of ORM using the Cache method, which has already been introduced before and will not be elaborated on here.

GRPC Client

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/registry/etcd/v2"
  4. "github.com/gogf/gf/contrib/rpc/grpcx/v2"
  5. "github.com/gogf/gf/contrib/trace/otlpgrpc/v2"
  6. "github.com/gogf/gf/example/trace/grpc_with_db/protobuf/user"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/gtrace"
  9. "github.com/gogf/gf/v2/os/gctx"
  10. )
  11. const (
  12. serviceName = "otlp-grpc-client"
  13. endpoint = "tracing-analysis-dc-bj.aliyuncs.com:8090"
  14. traceToken = "******_******"
  15. )
  16. func main() {
  17. grpcx.Resolver.Register(etcd.New("127.0.0.1:2379"))
  18. var ctx = gctx.New()
  19. shutdown, err := otlpgrpc.Init(serviceName, endpoint, traceToken)
  20. if err != nil {
  21. g.Log().Fatal(ctx, err)
  22. }
  23. defer shutdown()
  24. StartRequests()
  25. }
  26. func StartRequests() {
  27. ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
  28. defer span.End()
  29. client := user.NewUserClient(grpcx.Client.MustNewGrpcClientConn("demo"))
  30. // Baggage.
  31. ctx = gtrace.SetBaggageValue(ctx, "uid", 100)
  32. // Insert.
  33. insertRes, err := client.Insert(ctx, &user.InsertReq{
  34. Name: "john",
  35. })
  36. if err != nil {
  37. g.Log().Fatalf(ctx, `%+v`, err)
  38. }
  39. g.Log().Info(ctx, "insert id:", insertRes.Id)
  40. // Query.
  41. queryRes, err := client.Query(ctx, &user.QueryReq{
  42. Id: insertRes.Id,
  43. })
  44. if err != nil {
  45. g.Log().Errorf(ctx, `%+v`, err)
  46. return
  47. }
  48. g.Log().Info(ctx, "query result:", queryRes)
  49. // Delete.
  50. _, err = client.Delete(ctx, &user.DeleteReq{
  51. Id: insertRes.Id,
  52. })
  53. if err != nil {
  54. g.Log().Errorf(ctx, `%+v`, err)
  55. return
  56. }
  57. g.Log().Info(ctx, "delete id:", insertRes.Id)
  58. // Delete with error.
  59. _, err = client.Delete(ctx, &user.DeleteReq{
  60. Id: -1,
  61. })
  62. if err != nil {
  63. g.Log().Errorf(ctx, `%+v`, err)
  64. return
  65. }
  66. g.Log().Info(ctx, "delete id:", -1)
  67. }

Brief explanation of the client code:

  1. First, the client also needs to initialize Jaeger through the jaeger.Init method.

  2. The client is very simple, internal initialization and setting of default interceptors have been encapsulated by the Katyusha framework, developers need only focus on implementing business logic.

Effect Viewing

Start the Server:

Tracing - GRPC Example - 图2

Start the Client:

Tracing - GRPC Example - 图3

At the end of the client execution here, an error was reported, which we did intentionally, to demonstrate the link information display when GRPC has an error. Let’s open jaeger to check the tracing information:

Tracing - GRPC Example - 图4

You can see that this request involves two services: tracing-grpc-client and tracing-grpc-server, which are the client and the server. The entire request link involves 17 spans, with the client having 5 spans, the server having 12 spans, and producing 2 errors. Let’s click to view the details:

Tracing - GRPC Example - 图5

Let’s click to check the span situation of the last interface call error:

Tracing - GRPC Example - 图6

It looks like a parameter validation error, click to view the request parameters in Events/Logs:

Tracing - GRPC Example - 图7

Checking the Log information in Process, it can be seen that the error occurred because the parameter passed was -1, which did not meet the validation rules, hence an error was returned during data validation.

GRPC Client

Since orm, redis, and logging components have previously introduced link information, here we mainly introduce link information for GRPC Client&Server.

Attributes

Tracing - GRPC Example - 图8

Attribute/TagDescription
net.peer.ipTarget IP of the request.
net.peer.portTarget port of the request.
rpc.grpc.status_codeInternal status code for GRPC, 0 indicates success, non-0 indicates failure.
rpc.serviceName of the RPC service, note that this is RPC not GRPC, as this is a general definition, and the client supports multiple RPC communication protocols, with GRPC being one of them.
rpc.methodName of the RPC method.
rpc.systemType of RPC protocol, such as: grpc, thrift, etc.

Events/Logs

Tracing - GRPC Example - 图9

Event/LogDescription
grpc.metadata.outgoingGRPC client request submitted Metadata information, which may be large.
grpc.request.baggageGRPC client request submitted Baggage information for service-to-service trace information transmission.
grpc.request.messageGRPC client request submitted Message data, which may be large, only 512KB is recorded, and if it exceeds that size, it is ignored. Only valid for Unary request types.
grpc.response.messageGRPC client request received returned Message information, which may be large. Only valid for Unary request types.

GRPC Server

Attributes

Tracing - GRPC Example - 图10

GRPC Server attributes mean the same as GRPC Client, with parsed data being almost identical in the same request.

Events

Tracing - GRPC Example - 图11

The difference in Events for the GRPC Server compared to the GRPC Client is that in the same request, the metadata received by the server is grpc.metadata.incoming, while others are the same as the GRPC Client.