我试图弄清楚为什么我的grpc调用不起作用,但我不知道如何打开调试,所以我可以看到通过 grpc 连接发送和接收的数据。
您如何打开 grpc 调用的调试?
您可以将GRPC_TRACE
环境变量设置all
为让 grpc 转储一大堆关于连接正在做什么的数据:
export GRPC_TRACE=all
从评论编辑:显然你还需要设置:
export GRPC_VERBOSITY=DEBUG
在 Golang 中,您需要设置GODEBUG
环境变量才能看到 HTTP2 跟踪,即 gRPC 设置的 headers:
GODEBUG=http2debug=1 # enable verbose HTTP/2 debug logs
GODEBUG=http2debug=2 # ... even more verbose, with frame dumps
然后将输出发送到标准输出。这是一个例子:
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: wrote SETTINGS len=0","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: wrote WINDOW_UPDATE len=4 (conn) incr=983025","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: read SETTINGS len=18, settings: ENABLE_PUSH=0, MAX_CONCURRENT_STREAMS=0, INITIAL_WINDOW_SIZE=1048576","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: read WINDOW_UPDATE len=4 (conn) incr=983041","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: wrote SETTINGS flags=ACK len=0","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: read SETTINGS flags=ACK len=0","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: Framer 0xc42009c0e0: read HEADERS flags=END_HEADERS|PRIORITY stream=3 len=249","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \":authority\" = \"\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \":path\" = \"/internal.push.v1.UnifiedDevicePush/SendMessage\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \":method\" = \"POST\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \":scheme\" = \"http\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \"content-type\" = \"application/grpc\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \"te\" = \"trailers\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \"user-agent\" = \"grpc-java-netty/1.0.3\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \"root-common.xirequestid-bin\" = \"ChIJzE6lBfCTCsYRoIIJujc92JY=\"","time":"2017-06-11T08:52:20Z"}
{"level":"info","msg":"2017/06/11 08:52:20 http2: decoded hpack field header field \"te\" = \"trailers\"","time":"2017-06-11T08:52:20Z"}
导出 GRPC_GO_LOG_VERBOSITY_LEVEL=99
导出 GRPC_GO_LOG_SEVERITY_LEVEL=info
用最新的 grpc go 版本试试这个。
从文档
GRPC_VERBOSITY
用于设置 gRPC 打印的日志消息的最低级别(支持的值为DEBUG
、INFO
和ERROR
)。如果未设置此环境变量,则只会ERROR
打印日志。
另外,检查GRPC_TRACE
有 15+ grpc 环境变量
Windows 用户的注释,引用自docs:
已知限制:GPRC_TRACE=tcp
目前未针对 Windows 实现(您不会看到任何 TCP 跟踪)。
您可以使用调解器工具来调试和跟踪 gRPC 调用。
它和charles一样是一个 GUI 工具,但是如果需要解析 protobuf 消息体,gRPC 服务器需要支持Server Reflection。
还有一个工具grpcdebug
grpcdebug 是一个命令行界面,专注于简化 gRPC 应用程序的调试过程。grpcdebug 通过 gRPC 协议从应用程序中获取 gRPC 库的内部状态,并提供人性化的用户体验来浏览它们。目前,它支持 Channelz/健康检查/CSDS(又名管理服务)。换句话说,它可以获取有关给定 gRPC 通道上已发送或失败的 RPC 数量的统计信息,它可以检查地址解析结果,它可以转储指导 RPC 路由的无效 xDS 配置。
这是一些示例
对于所有 Channelz 命令,您可以添加 --json 以获取原始 Channelz 输出。
grpcdebug localhost:50051 channelz channels --json
grpcdebug localhost:50051 channelz servers --json
grpcdebug localhost:50051 channelz 频道
# Channel ID Target State Calls(Started/Succeeded/Failed) Created Time
# 7 localhost:10001 READY 5136/4631/505 8 minutes ago
grpcdebug localhost:50051 channelz 服务器
# Server ID Listen Addresses Calls(Started/Succeeded/Failed) Last Call Started
# 1 [:::10001] 2852/2530/322 now
# 2 [:::50051] 29/28/0 now
# 3 [:::50052] 4/4/0 26 seconds ago