Help us shape the future of Go! Take the 2026 Go Developer Survey

grpc

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 7 Imported by: 0

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

View Source
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

func GetRequestID(ctx context.Context) string

GetRequestID 从 gRPC context 中获取 Request ID。

中文说明: - 供 gRPC 服务实现使用; - 如果不存在则返回空字符串。

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID 从 gRPC context 中获取 Trace 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL