This is a creation in Article, where the information may have evolved or changed.
Built-in trace
Grpc default provides the client and server trace log, unfortunately does not provide a custom interface, currently can only view the basic event log and request log, for basic request status Review is also very helpful, the client and the server is basically the same, here is the server opened trace for example, To modify the server code for the Hello project:
Server/main.go
Package Mainimport ("NET" "Net/http" PB "Git.vodjk.com/go-grpc/example/proto"//introduce compilation-generated packages "golang.org/x/net/c Ontext "Golang.org/x/net/trace"//Introduction Trace Package "Google.golang.org/grpc" "Google.golang.org/grpc/grpclog") const ( Address GRPC addresses = "127.0.0.1:50052")//define HelloService and implement the agreed interface type HelloService struct{}//helloservice. . var HelloService = Helloservice{}func (H helloservice) SayHello (CTX context. Context, in *PB. Hellorequest) (*PB. Helloreply, error) {resp: = new (Pb). helloreply) resp. Message = "Hello" + in. Name + "." Return resp, Nil}func main () {listen, err: = Net. Listen ("TCP", Address) if err! = Nil {grpclog. Fatalf ("Failed to listen:%v", err)}//Instantiate Grpc Server s: = Grpc. NewServer ()//Registered HelloService PB. Registerhelloserver (S, helloservice)//Turn on trace Go starttrace () grpclog. Println ("Listen on" + Address) S.serve (Listen)}func starttrace () {trace. Authrequest = func (req *http. Request) (ANY, sensitive bool) {return true, true} go http. Listenandserve (": 50051", nil) grpclog. Println ("Trace Listen on 50051")}
Here we open an HTTP service listening on port 50051 to see the trace information of the GRPC request
Run:
go run main.goListen on 127.0.0.1:50052 Trace listen on 50051// 进入client目录执行一次客户端请求
Server-side Event view
Visit: localhost:50051/debug/events, results
You can see the event information for services and services that are registered on the server to start properly, and this is the only event in the default trace
Request Log Information view
Visit: localhost:50051/debug/requests, results
The most recent request status can be shown here, including the requested service, parameters, time-consuming, response, and convenient for simple state viewing, with the default value showing the last 10 records and cannot be modified.
Reference
Sample code for this series