Created
July 29, 2019 03:25
-
-
Save castaneai/46f7a266867ae5b9b3999ce406aed94e to your computer and use it in GitHub Desktop.
[gRPC Channelz] show trace on client connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"github.com/golang/protobuf/ptypes" | |
"google.golang.org/grpc/reflection" | |
"log" | |
"net" | |
"os" | |
"time" | |
pb "github.com/castaneai/grpc-book/src/chap01/hello-grpc" | |
"google.golang.org/grpc" | |
channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" | |
channelzsvc "google.golang.org/grpc/channelz/service" | |
) | |
const ( | |
address = "localhost:50051" | |
) | |
func main() { | |
s := grpc.NewServer() | |
reflection.Register(s) | |
channelzsvc.RegisterChannelzServiceToServer(s) | |
lis, err := net.Listen("tcp", ":5000") | |
if err != nil { | |
log.Fatalf("failed to listen: %v", err) | |
} | |
go func() { | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | |
defer cancel() | |
client(ctx) | |
conn, err := grpc.Dial("localhost:5000", grpc.WithInsecure()) | |
zc := channelzpb.NewChannelzClient(conn) | |
res, err := zc.GetTopChannels(ctx, &channelzpb.GetTopChannelsRequest{StartChannelId: 0}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if res.Channel != nil && len(res.Channel) > 0 { | |
ch := res.Channel[0] | |
log.Printf("--- [channel (id: %v)]", ch.Ref.ChannelId) | |
for _, ev := range ch.GetData().GetTrace().Events { | |
ts, err := ptypes.Timestamp(ev.Timestamp) | |
if err != nil { | |
log.Fatalf("ts parse error: %+v", ev.Timestamp) | |
} | |
log.Printf("[%v] %+v", ts, ev.Description) | |
} | |
} | |
}() | |
if err := s.Serve(lis); err != nil { | |
log.Fatalf("failed to serve: %v", err) | |
} | |
} | |
func client(ctx context.Context) { | |
conn, err := grpc.Dial(address, grpc.WithInsecure()) | |
if err != nil { | |
log.Fatalf("did not connect: %v", err) | |
} | |
defer conn.Close() | |
c := pb.NewGreeterClient(conn) | |
name := os.Args[1] | |
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name}) | |
if err != nil { | |
log.Fatalf("could not greet: %v", err) | |
} | |
log.Printf("Greeting: %s", r.Message) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment