Skip to content

事件协议

HealthGuard 的所有 SDK 都遵循统一的事件协议,事件定义在 @health-guard/core 中,使用 Zod Schema 进行校验。

批量上报

SDK 通过 POST /api/events/batch 批量上报事件。

请求体

json
{
  "appKey": "web_abc123",
  "events": [
    { /* event 1 */ },
    { /* event 2 */ }
  ]
}

字段说明

字段类型必填说明
appKeystring应用唯一标识,必须与每个事件的 appKey 一致
eventsarray事件数组,最少 1 条,最多 50 条

基础事件字段

所有事件类型都包含以下公共字段:

字段类型必填说明
eventIdstring事件唯一 ID
appKeystring应用唯一标识
platformstring平台类型,见下方枚举
timestampnumber事件发生时间戳(毫秒)
sessionIdstring会话 ID
anonymousIdstring匿名用户 ID
sdkVersionstringSDK 版本号
releasestring应用版本号
environmentstringdevelopment / test / production
userIdstring业务用户标识
pageUrlstring当前页面 URL
deviceInfoobject设备信息

platform 枚举

  • web
  • wechat-miniprogram
  • alipay-miniprogram
  • flutter
  • uniapp-h5
  • uniapp-wechat
  • uniapp-alipay
  • uniapp-douyin
  • uniapp-app
  • uniapp

environment 枚举

  • development
  • test
  • production

事件类型

error

错误事件用于上报 JS 错误、Promise rejection、资源加载失败、请求失败等。

字段类型必填说明
typestring固定为 error
errorTypestringjs / promise / resource / request / native
messagestring错误摘要
stackstring错误堆栈
filenamestring出错文件(浏览器环境)
linenonumber行号
colnonumber列号
fingerprintstringIssue 聚合指纹
breadcrumbsarray面包屑数组
contextobject自定义上下文,最多 20 个键
pagestring页面路径
scenestring业务场景

http

HTTP 事件用于上报请求耗时、状态码和失败原因。

字段类型必填说明
typestring固定为 http
methodstringHTTP 方法
urlstring请求 URL(已脱敏)
statusnumberHTTP 状态码
durationnumber请求耗时(毫秒)
successboolean是否成功
errorMessagestring错误描述
fingerprintstringIssue 聚合指纹
contextobject自定义上下文,最多 20 个键
requestDataobject请求数据,最多 20 个键
pagestring页面路径
scenestring业务场景

performance

性能事件用于上报自定义性能指标或 Web Vitals 等数据。

字段类型必填说明
typestring固定为 performance
namestring指标名称
valuenumber指标数值
ratingstringgood / needs-improvement / poor

面包屑事件用于显式记录用户操作或状态变更轨迹。

字段类型必填说明
typestring固定为 breadcrumb
breadcrumb.typestringnavigation / click / http / manual
breadcrumb.messagestring面包屑描述
breadcrumb.timestampnumber时间戳
breadcrumb.dataobject附加数据

指纹生成

  • 错误指纹:基于 errorTypemessage、堆栈前 two 行、以及 context 中的 url/method/scene/page 生成。
  • HTTP 指纹:基于 method、URL pathname、statusplatform 生成。

指纹用于在 Collector 端将相似事件聚合为 Issue。

脱敏与限制

  • URL Query 中的敏感键会被替换为 [Filtered],敏感键包括:authorizationauthcookiepasswordsecrettokenaccess_tokenrefresh_token
  • contextrequestData 每层最多 20 个键。
  • request body 和 response body 默认不上报。

相关文档

Released under the MIT License.