Skip to content

Commit 25e7bf3

Browse files
committed
example: debugging
1 parent b74673a commit 25e7bf3

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

Diff for: examples/features/debugging/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Debugging
2+
3+
gRPC has put substantial logging instruments on critical paths of gRPC to help users debug issues.
4+
The [Log Levels](https://github.com/grpc/grpc-go/blob/master/Documentation/log_levels.md) doc describes
5+
what each log level means in the gRPC context.
6+
7+
We also provides a runtime debugging tool, Channelz, to help users with live debugging.
8+
9+
## Logs
10+
To turn on the logs for debugging, run the code with the following environment variable:
11+
`GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info`
12+
13+
## Channelz
14+
See the channelz blog post here ([link](https://grpc.io/blog/a_short_introduction_to_channelz)) for
15+
details about how to use channelz service to debug live program.

Diff for: examples/features/debugging/client/main.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
*
3+
* Copyright 2018 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"log"
23+
"net"
24+
"os"
25+
"os/signal"
26+
"time"
27+
28+
"golang.org/x/net/context"
29+
"google.golang.org/grpc"
30+
"google.golang.org/grpc/channelz/service"
31+
pb "google.golang.org/grpc/examples/helloworld/helloworld"
32+
"google.golang.org/grpc/resolver"
33+
"google.golang.org/grpc/resolver/manual"
34+
)
35+
36+
const (
37+
defaultName = "world"
38+
)
39+
40+
func main() {
41+
/***** Set up the server serving channelz service. *****/
42+
lis, err := net.Listen("tcp", ":50050")
43+
if err != nil {
44+
log.Fatalf("failed to listen: %v", err)
45+
}
46+
s := grpc.NewServer()
47+
service.RegisterChannelzServiceToServer(s)
48+
go s.Serve(lis)
49+
defer s.Stop()
50+
51+
/***** Initialize manual resolver and Dial *****/
52+
r, rcleanup := manual.GenerateAndRegisterManualResolver()
53+
defer rcleanup()
54+
// Set up a connection to the server.
55+
conn, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName("round_robin"))
56+
if err != nil {
57+
log.Fatalf("did not connect: %v", err)
58+
}
59+
defer conn.Close()
60+
// Manually provide resolved addresses for the target.
61+
r.NewAddress([]resolver.Address{{Addr: ":10001"}, {Addr: ":10002"}, {Addr: ":10003"}})
62+
63+
c := pb.NewGreeterClient(conn)
64+
65+
// Contact the server and print out its response.
66+
name := defaultName
67+
if len(os.Args) > 1 {
68+
name = os.Args[1]
69+
}
70+
71+
/***** Make 100 SayHello RPCs *****/
72+
for i := 0; i < 100; i++ {
73+
// Setting a 150ms timeout on the RPC.
74+
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
75+
defer cancel()
76+
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
77+
if err != nil {
78+
log.Printf("could not greet: %v", err)
79+
} else {
80+
log.Printf("Greeting: %s", r.Message)
81+
}
82+
}
83+
84+
/***** Wait for CTRL+C to exit *****/
85+
// Unless you exit the program with CTRL+C, channelz data will be available for querying.
86+
// Users can take time to examine and learn about the info provided by channelz.
87+
ch := make(chan os.Signal, 1)
88+
signal.Notify(ch, os.Interrupt)
89+
// Block until a signal is received.
90+
<-ch
91+
}

Diff for: examples/features/debugging/server/main.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
*
3+
* Copyright 2018 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"log"
23+
"net"
24+
"os"
25+
"os/signal"
26+
"time"
27+
28+
"golang.org/x/net/context"
29+
"google.golang.org/grpc"
30+
"google.golang.org/grpc/channelz/service"
31+
pb "google.golang.org/grpc/examples/helloworld/helloworld"
32+
"google.golang.org/grpc/internal/grpcrand"
33+
)
34+
35+
var (
36+
ports = []string{":10001", ":10002", ":10003"}
37+
)
38+
39+
// server is used to implement helloworld.GreeterServer.
40+
type server struct{}
41+
42+
// SayHello implements helloworld.GreeterServer
43+
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
44+
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
45+
}
46+
47+
// slow server is used to simulate a server that has a variable delay in its response.
48+
type slowServer struct{}
49+
50+
// SayHello implements helloworld.GreeterServer
51+
func (s *slowServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
52+
// Delay 100ms ~ 200ms before replying
53+
time.Sleep(time.Duration(100+grpcrand.Intn(100)) * time.Millisecond)
54+
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
55+
}
56+
57+
func main() {
58+
/***** Set up the server serving channelz service. *****/
59+
lis, err := net.Listen("tcp", ":50051")
60+
if err != nil {
61+
log.Fatalf("failed to listen: %v", err)
62+
}
63+
defer lis.Close()
64+
s := grpc.NewServer()
65+
service.RegisterChannelzServiceToServer(s)
66+
go s.Serve(lis)
67+
defer s.Stop()
68+
69+
/***** Start three GreeterServers(with one of them to be the slowServer). *****/
70+
var listeners []net.Listener
71+
var svrs []*grpc.Server
72+
for i := 0; i < 3; i++ {
73+
lis, err := net.Listen("tcp", ports[i])
74+
if err != nil {
75+
log.Fatalf("failed to listen: %v", err)
76+
}
77+
listeners = append(listeners, lis)
78+
s := grpc.NewServer()
79+
svrs = append(svrs, s)
80+
if i == 2 {
81+
pb.RegisterGreeterServer(s, &slowServer{})
82+
} else {
83+
pb.RegisterGreeterServer(s, &server{})
84+
}
85+
go s.Serve(lis)
86+
}
87+
88+
/***** Wait for CTRL+C to exit *****/
89+
ch := make(chan os.Signal, 1)
90+
signal.Notify(ch, os.Interrupt)
91+
// Block until a signal is received.
92+
<-ch
93+
for i := 0; i < 3; i++ {
94+
svrs[i].Stop()
95+
listeners[i].Close()
96+
}
97+
}

0 commit comments

Comments
 (0)