metadata

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

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

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

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

func ExportMetadataToMap(ctx context.Context, keys []string) map[string]interface{}

ExportMetadataToMap 将上下文中的元数据导出到map

func GetBrowserFingerprintFromCtx

func GetBrowserFingerprintFromCtx(ctx context.Context) string

GetBrowserFingerprintFromCtx 从上下文中获取浏览器指纹

func GetCurrencyCodeFromCtx

func GetCurrencyCodeFromCtx(ctx context.Context) string

GetCurrencyCodeFromCtx 从上下文中获取currency_code

func GetDeviceIDFromCtx

func GetDeviceIDFromCtx(ctx context.Context) string

GetDeviceIDFromCtx 从上下文中获取设备id

func GetDeviceTypeFromCtx

func GetDeviceTypeFromCtx(ctx context.Context) string

GetDeviceTypeFromCtx 从上下文中获取设备类型

func GetDomainFromCtx

func GetDomainFromCtx(ctx context.Context) string

GetDomainFromCtx 从上下文中获取域名

func GetIpFromCtx

func GetIpFromCtx(ctx context.Context) string

GetIpFromCtx 从上下文中获取ip (增强版)

func GetMetadata

func GetMetadata[T any](ctx context.Context, key any) (T, bool)

GetMetadata 类型安全的上下文取值

func GetMetadataFromCtx

func GetMetadataFromCtx(ctx context.Context, key any) any

GetMetadataFromCtx 从上下文获取数据

func GetMetadataOrDefault

func GetMetadataOrDefault[T any](ctx context.Context, key any, defaultVal T) T

GetMetadataOrDefault 获取值或返回默认值

func GetParentAgentIdFromCtx

func GetParentAgentIdFromCtx(ctx context.Context) int64

GetParentAgentIdFromCtx 从上下文中获取上级代理ID

func GetRegionFromCtx

func GetRegionFromCtx(ctx context.Context) string

GetRegionFromCtx 从上下文中获取区域

func GetRequestTimeFromCtx

func GetRequestTimeFromCtx(ctx context.Context) time.Time

GetRequestTimeFromCtx 从上下文中获取请求时间

func GetTraceIDFromCtx

func GetTraceIDFromCtx(ctx context.Context) string

GetTraceIDFromCtx 从上下文中获取追踪ID

func GetUidFromCtx

func GetUidFromCtx(ctx context.Context) int64

GetUidFromCtx 从上下文中获取uid (增强版)

func GetUserAgentIdFromCtx

func GetUserAgentIdFromCtx(ctx context.Context) int64

GetUserAgentIdFromCtx 从上下文中获取代理ID

func GetUserPermissionsFromCtx

func GetUserPermissionsFromCtx(ctx context.Context) []string

GetUserPermissionsFromCtx 从上下文中获取用户权限

func GetUserRoleFromCtx

func GetUserRoleFromCtx(ctx context.Context) string

GetUserRoleFromCtx 从上下文中获取用户角色

func GetUsernameFromCtx

func GetUsernameFromCtx(ctx context.Context) string

GetUsernameFromCtx 从上下文中获取username

func HasAnyRole

func HasAnyRole(ctx context.Context, roles ...string) bool

HasAnyRole 检查用户是否拥有指定角色之一

func HasPermission

func HasPermission(ctx context.Context, permission string) bool

HasPermission 检查用户是否拥有指定权限

func IsAuthenticated

func IsAuthenticated(ctx context.Context) bool

IsAuthenticated 检查用户是否已认证

func UpdateRequestClientInfo

func UpdateRequestClientInfo(ctx context.Context, updater func(*RequestClientInfo)) context.Context

UpdateRequestClientInfo 更新上下文中的客户端信息

func WithMetadata

func WithMetadata(ctx context.Context, key, val any) context.Context

WithMetadata 向上下文添加数据

func WithMultiMetadata

func WithMultiMetadata(ctx context.Context, keyVals map[string]interface{}) context.Context

WithMultiMetadata 向上下文批量添加键值对

func WithRequestInfo

func WithRequestInfo(ctx context.Context, ip, deviceID, deviceType string) context.Context

WithRequestInfo 向上下文添加请求信息

func WithTracing

func WithTracing(ctx context.Context, traceID, requestID string) context.Context

WithTracing 添加追踪信息到上下文

func WithUserInfo

func WithUserInfo(ctx context.Context, userId int64, username string) context.Context

WithUserInfo 向上下文添加用户信息

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

Jump to

Keyboard shortcuts

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