22 lines
535 B
Go
22 lines
535 B
Go
package stream
|
|
|
|
import "context"
|
|
|
|
type requestIDKey struct{}
|
|
|
|
// WithRequestID 将请求追踪 ID 注入 Context,供下游分类错误与日志关联。
|
|
func WithRequestID(ctx context.Context, id string) context.Context {
|
|
return context.WithValue(ctx, requestIDKey{}, id)
|
|
}
|
|
|
|
// RequestIDFromContext 从 Context 提取请求追踪 ID,未设置时返回空串。
|
|
func RequestIDFromContext(ctx context.Context) string {
|
|
if ctx == nil {
|
|
return ""
|
|
}
|
|
if v, ok := ctx.Value(requestIDKey{}).(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|