Documentation
¶
Overview ¶
Package grpc provides gRPC interceptors for gorp framework. Includes trace/request ID propagation, Prometheus metrics collection. Supports both unary and stream interceptors for client and server.
gRPC 拦截器包,提供 gorp 框架的 gRPC 拦截器能力。 包括 trace/request ID 传播、Prometheus 指标采集。 支持客户端和服务端的一元和流拦截器。
Index ¶
- Constants
- func GetRequestID(ctx context.Context) string
- func GetTraceID(ctx context.Context) string
- func StreamClientInterceptor() grpc.StreamClientInterceptor
- func StreamServerInterceptor() grpc.StreamServerInterceptor
- func UnaryClientInterceptor() grpc.UnaryClientInterceptor
- func UnaryServerInterceptor() grpc.UnaryServerInterceptor
Constants ¶
const ( // TraceIDKey 是 gRPC metadata 中 trace id 的 key TraceIDKey = "x-trace-id" // RequestIDKey 是 gRPC metadata 中 request id 的 key RequestIDKey = "x-request-id" )
Variables ¶
This section is empty.
Functions ¶
func GetRequestID ¶
GetRequestID 从 gRPC context 中获取 Request ID。
中文说明: - 供 gRPC 服务实现使用; - 如果不存在则返回空字符串。
func StreamClientInterceptor ¶
func StreamClientInterceptor() grpc.StreamClientInterceptor
StreamClientInterceptor 创建一个 gRPC 客户端流拦截器,用于向 metadata 中注入 trace id。
中文说明: - 作用与 UnaryClientInterceptor 一致,但覆盖流式调用; - 这样无论是一元 RPC 还是流 RPC,都能复用同一套 trace/request id 透传约定。
func StreamServerInterceptor ¶
func StreamServerInterceptor() grpc.StreamServerInterceptor
StreamServerInterceptor 创建一个 gRPC 服务端流拦截器,用于从 metadata 中提取 trace id 并收集指标。
中文说明: - 功能与 UnaryServerInterceptor 相同,但用于流式 RPC。
func UnaryClientInterceptor ¶
func UnaryClientInterceptor() grpc.UnaryClientInterceptor
UnaryClientInterceptor 创建一个 gRPC 客户端一元拦截器,用于向 metadata 中注入 trace id。
中文说明: - 用于 gRPC 客户端调用时自动注入 trace id; - 实现跨服务链路追踪; - 如果 context 中已有 trace id,则透传;否则生成新的。
func UnaryServerInterceptor ¶
func UnaryServerInterceptor() grpc.UnaryServerInterceptor
UnaryServerInterceptor 创建一个 gRPC 服务端一元拦截器,用于从 metadata 中提取 trace id 并收集指标。
中文说明: - 从 incoming metadata 中读取 trace id 和 request id; - 如果不存在,则生成新的 id; - 将 id 存入 context,供后续处理使用; - 同时将 id 写入 outgoing metadata,实现跨服务透传; - 收集 Prometheus 指标:请求总数、耗时、当前处理数。
Types ¶
This section is empty.