requestid

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const Header = "X-Request-ID"

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) string

FromContext extracts the request ID from ctx.

func HTTPMiddleware

func HTTPMiddleware(next http.Handler) http.Handler

HTTPMiddleware 注入或透传 X-Request-ID。 优先级:上游 Header > OTel trace ID > UUIDv7。 ID 会设置到响应 Header 并注入 request context。

func NewContext

func NewContext(ctx context.Context, id string) context.Context

NewContext returns a copy of ctx with the given request ID attached.

func NewHTTPMiddleware added in v0.7.7

func NewHTTPMiddleware(opts ...Option) func(http.Handler) http.Handler

NewHTTPMiddleware 创建支持自定义选项的 requestid HTTP 中间件。

webserver.New(addr, mux, webserver.WithMiddleware(
    requestid.NewHTTPMiddleware(requestid.WithIDGenerator(myGen)),
))

func NewStreamServerInterceptor added in v0.7.7

func NewStreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor

NewStreamServerInterceptor 创建支持自定义选项的 gRPC stream 拦截器。

func NewUnaryServerInterceptor added in v0.7.7

func NewUnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor

NewUnaryServerInterceptor 创建支持自定义选项的 gRPC unary 拦截器。

func StreamServerInterceptor

func StreamServerInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error

StreamServerInterceptor 为 gRPC streaming 调用透传或生成 x-request-id。

func UnaryServerInterceptor

func UnaryServerInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)

UnaryServerInterceptor 为 gRPC unary 调用透传或生成 x-request-id。

Types

type IDGenerator added in v0.7.7

type IDGenerator func(ctx context.Context) string

IDGenerator 根据当前 context 生成请求 ID。

type Option added in v0.7.7

type Option func(*config)

Option 配置 requestid 中间件的可选参数。

func WithIDGenerator added in v0.7.7

func WithIDGenerator(gen IDGenerator) Option

WithIDGenerator 自定义 ID 生成函数,替代默认的 trace_id > UUIDv7 策略。

Jump to

Keyboard shortcuts

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