Documentation
¶
Index ¶
- Constants
- func ExportMetadataToMap(ctx context.Context, keys []string) map[string]interface{}
- func GetBrowserFingerprintFromCtx(ctx context.Context) string
- func GetCurrencyCodeFromCtx(ctx context.Context) string
- func GetDeviceIDFromCtx(ctx context.Context) string
- func GetDeviceTypeFromCtx(ctx context.Context) string
- func GetDomainFromCtx(ctx context.Context) string
- func GetIpFromCtx(ctx context.Context) string
- func GetMetadata[T any](ctx context.Context, key any) (T, bool)
- func GetMetadataFromCtx(ctx context.Context, key any) any
- func GetMetadataOrDefault[T any](ctx context.Context, key any, defaultVal T) T
- func GetParentAgentIdFromCtx(ctx context.Context) int64
- func GetRegionFromCtx(ctx context.Context) string
- func GetRequestTimeFromCtx(ctx context.Context) time.Time
- func GetTraceIDFromCtx(ctx context.Context) string
- func GetUidFromCtx(ctx context.Context) int64
- func GetUserAgentIdFromCtx(ctx context.Context) int64
- func GetUserPermissionsFromCtx(ctx context.Context) []string
- func GetUserRoleFromCtx(ctx context.Context) string
- func GetUsernameFromCtx(ctx context.Context) string
- func HasAnyRole(ctx context.Context, roles ...string) bool
- func HasPermission(ctx context.Context, permission string) bool
- func IsAuthenticated(ctx context.Context) bool
- func UpdateRequestClientInfo(ctx context.Context, updater func(*RequestClientInfo)) context.Context
- func WithMetadata(ctx context.Context, key, val any) context.Context
- func WithMultiMetadata(ctx context.Context, keyVals map[string]interface{}) context.Context
- func WithRequestInfo(ctx context.Context, ip, deviceID, deviceType string) context.Context
- func WithTracing(ctx context.Context, traceID, requestID string) context.Context
- func WithUserInfo(ctx context.Context, userId int64, username string) context.Context
- type RequestClientInfo
Constants ¶
const ( // Standard headers (标准 HTTP 头,使用大写) HeaderAuthorization = "Authorization" HeaderUserAgent = "User-Agent" HeaderAcceptLanguage = "Accept-Language" HeaderReferrer = "Referer" HeaderForwardedFor = "X-Forwarded-For" // Custom headers (自定义头,使用 x- 前缀和小写) HeaderPlatform = "x-platform" HeaderOS = "x-os" HeaderBrowser = "x-browser" HeaderMobile = "x-mobile" HeaderDeviceID = "x-device-id" HeaderDeviceType = "x-device-type" HeaderBrowserFingerprint = "x-browser-fingerprint" HeaderRegion = "x-region" HeaderLanguage = "x-language" HeaderTimezone = "x-timezone" HeaderTraceID = "x-trace-id" HeaderRequestID = "x-request-id" HeaderScreenSize = "x-screen-size" HeaderRealIP = "x-real-ip" HeaderOriginalForwardedFor = "x-original-forwarded-for" HeaderClientIP = "x-client-ip" HeaderCFConnectingIP = "x-cf-connecting-ip" HeaderToken = "x-token" )
Header keys
const ( // User related CtxJWTUserId = "uid" // 用户id CtxJWTUsername = "username" // 用户名 CtxUserRole = "user_role" // 用户角色 CtxUserPermissions = "user_permissions" // 用户权限 CtxUserStatus = "user_status" // 用户状态 CtxUserLastLoginTime = "last_login_time" // 最后登录时间 CtxUserAgentId = "agent_id" // 代理ID CtxUserParentAgentId = "parent_agent_id" // 上级代理ID // Request related CtxIp = "ip" // ip CtxDomain = "domain" // 域名 CtxRegion = "region" // 区域 CtxDeviceID = "device_id" // 设备id CtxDeviceType = "device_type" // 设备类型 CtxBrowserFingerprint = "browser_fingerprint" // 浏览器指纹 CtxCurrencyCode = "currency_code" // 币种code CtxRequestClientInfo = "request_client_info" // 请求客户端信息 CtxLanguage = "language" // 语言 CtxTimezone = "timezone" // 时区 CtxSessionID = "session_id" // 会话ID CtxTraceID = "trace_id" // 追踪ID CtxRequestID = "request_id" // 请求ID CtxRequestTime = "request_time" // 请求时间 // Auth related CtxIsAuthenticated = "is_authenticated" // 是否已认证 CtxAuthType = "auth_type" // 认证类型 (JWT, OAuth, etc.) CtxToken = "token" // Token CtxTokenExpiry = "token_expiry" // Token过期时间 CtxIssuer = "issuer" // Token颁发者 )
Context keys
const ( MetricKeyRequestDuration = "rpc_request_duration_ms" // 请求耗时 MetricKeyRequestTotal = "rpc_request_total" // 请求总数 MetricKeyRequestError = "rpc_request_error_total" // 错误请求数 )
Metrics keys
Variables ¶
This section is empty.
Functions ¶
func ExportMetadataToMap ¶
ExportMetadataToMap 将上下文中的元数据导出到map
func GetBrowserFingerprintFromCtx ¶
GetBrowserFingerprintFromCtx 从上下文中获取浏览器指纹
func GetCurrencyCodeFromCtx ¶
GetCurrencyCodeFromCtx 从上下文中获取currency_code
func GetDeviceIDFromCtx ¶
GetDeviceIDFromCtx 从上下文中获取设备id
func GetDeviceTypeFromCtx ¶
GetDeviceTypeFromCtx 从上下文中获取设备类型
func GetDomainFromCtx ¶
GetDomainFromCtx 从上下文中获取域名
func GetMetadata ¶
GetMetadata 类型安全的上下文取值
func GetMetadataFromCtx ¶
GetMetadataFromCtx 从上下文获取数据
func GetMetadataOrDefault ¶
GetMetadataOrDefault 获取值或返回默认值
func GetParentAgentIdFromCtx ¶
GetParentAgentIdFromCtx 从上下文中获取上级代理ID
func GetRegionFromCtx ¶
GetRegionFromCtx 从上下文中获取区域
func GetRequestTimeFromCtx ¶
GetRequestTimeFromCtx 从上下文中获取请求时间
func GetTraceIDFromCtx ¶
GetTraceIDFromCtx 从上下文中获取追踪ID
func GetUserAgentIdFromCtx ¶
GetUserAgentIdFromCtx 从上下文中获取代理ID
func GetUserPermissionsFromCtx ¶
GetUserPermissionsFromCtx 从上下文中获取用户权限
func GetUserRoleFromCtx ¶
GetUserRoleFromCtx 从上下文中获取用户角色
func GetUsernameFromCtx ¶
GetUsernameFromCtx 从上下文中获取username
func HasAnyRole ¶
HasAnyRole 检查用户是否拥有指定角色之一
func HasPermission ¶
HasPermission 检查用户是否拥有指定权限
func UpdateRequestClientInfo ¶
func UpdateRequestClientInfo(ctx context.Context, updater func(*RequestClientInfo)) context.Context
UpdateRequestClientInfo 更新上下文中的客户端信息
func WithMetadata ¶
WithMetadata 向上下文添加数据
func WithMultiMetadata ¶
WithMultiMetadata 向上下文批量添加键值对
func WithRequestInfo ¶
WithRequestInfo 向上下文添加请求信息
func WithTracing ¶
WithTracing 添加追踪信息到上下文
Types ¶
type RequestClientInfo ¶
type RequestClientInfo struct { IP string // 客户端IP地址 Platform string // 平台:Windows、Linux等 OS string // 操作系统 Browser string // 浏览器信息 BrowserVer string // 浏览器版本 IsMobile bool // 是否是手机端 UserAgent string // 完整的User-Agent DeviceID string // 设备ID DeviceType string // 设备类型 AppVersion string // 应用版本 (如果是App) ScreenSize string // 屏幕尺寸 Language string // 语言 Timezone string // 时区 Referrer string // 引荐来源 RequestTime int64 // 请求时间 毫秒 }
RequestClientInfo 客户端信息结构体
func CreateClientInfoFromHeaders ¶
func CreateClientInfoFromHeaders(headers map[string][]string) *RequestClientInfo
CreateClientInfoFromHeaders 从HTTP头创建客户端信息
func GetRequestClientInfoFromCtx ¶
func GetRequestClientInfoFromCtx(ctx context.Context) *RequestClientInfo
GetRequestClientInfoFromCtx 从上下文中获取RequestClientInfo