Skip to content

事件协议

本文档定义 SDK 上报到 Collector 的事件数据结构,适用于所有平台 SDK(H5、微信小程序及未来平台)。

通用字段

每条事件必须包含以下通用字段:

字段类型必填说明
eventIdstring事件唯一标识(UUID)
appKeystring应用唯一标识
platformstring平台:web / miniprogram / flutter
typestring事件类型:error / http / performance / breadcrumb
timestampnumber事件发生时间戳(毫秒)
sessionIdstring会话标识
anonymousIdstring匿名用户标识
sdkVersionstringSDK 版本号
urlstring当前页面 URL(H5)或页面路径(小程序)
userAgentstring用户代理信息
breadcrumbsarray关联的面包屑列表

错误事件(type: error)

json
{
  "eventId": "evt_xxx",
  "appKey": "web_abc123",
  "platform": "web",
  "type": "error",
  "timestamp": 1716192000000,
  "sessionId": "sess_xxx",
  "anonymousId": "anon_xxx",
  "sdkVersion": "0.1.0",
  "errorType": "js",
  "message": "Cannot read property 'foo' of undefined",
  "stack": "TypeError: ...\n    at foo (app.js:12:3)",
  "fingerprint": "js:abc123",
  "url": "https://example.com/page",
  "breadcrumbs": []
}

error 专用字段

字段类型说明
errorTypestring错误类型:js / resource / promise / miniprogram
messagestring错误消息
stackstring堆栈信息
fingerprintstring错误指纹,用于聚合相同错误
filenamestring发生错误的文件(如有)
linenonumber行号(如有)
colnonumber列号(如有)

指纹生成规则

fingerprint 用于将相同错误聚合成一个 Issue:

  • JS 错误js:${hash(errorType + 脱敏后的 message + 脱敏后的 stack 首行)}
  • 资源错误resource:${hash(url)}
  • Promise 错误promise:${hash(message)}

hash 算法建议:取前 16 位 MD5 或 murmur3。

HTTP 事件(type: http)

json
{
  "eventId": "evt_xxx",
  "appKey": "web_abc123",
  "platform": "web",
  "type": "http",
  "timestamp": 1716192000000,
  "sessionId": "sess_xxx",
  "anonymousId": "anon_xxx",
  "sdkVersion": "0.1.0",
  "method": "POST",
  "url": "https://api.example.com/users",
  "status": 500,
  "duration": 245,
  "success": false,
  "errorMessage": "Internal Server Error"
}

http 专用字段

字段类型说明
methodstringHTTP 方法:GET / POST / PUT / DELETE 等
urlstring请求 URL(已脱敏)
statusnumberHTTP 状态码
durationnumber请求耗时(毫秒)
successboolean是否成功(status < 400)
errorMessagestring失败原因(超时、网络错误等)

性能事件(type: performance)

json
{
  "eventId": "evt_xxx",
  "appKey": "web_abc123",
  "platform": "web",
  "type": "performance",
  "timestamp": 1716192000000,
  "sessionId": "sess_xxx",
  "anonymousId": "anon_xxx",
  "sdkVersion": "0.1.0",
  "metricName": "LCP",
  "value": 1200,
  "rating": "good"
}

performance 专用字段

字段类型说明
metricNamestring指标名:LCP / FID / CLS / FCP / TTFB / INP
valuenumber指标数值
ratingstring评级:good / needs-improvement / poor

面包屑事件(type: breadcrumb)

面包屑通常作为其他事件的附属数据,也可独立上报:

json
{
  "eventId": "evt_xxx",
  "appKey": "web_abc123",
  "platform": "web",
  "type": "breadcrumb",
  "timestamp": 1716192000000,
  "sessionId": "sess_xxx",
  "anonymousId": "anon_xxx",
  "sdkVersion": "0.1.0",
  "breadcrumbType": "navigation",
  "message": "User navigated to /checkout",
  "data": { "from": "/cart", "to": "/checkout" }
}
字段类型说明
breadcrumbTypestring类型:navigation / click / console / custom
messagestring描述信息
dataobject附加数据

批量上报格式

SDK 通过 POST /api/events/batch 批量上报,请求体格式:

json
{
  "appKey": "web_abc123",
  "events": [
    { /* error event */ },
    { /* http event */ },
    { /* performance event */ }
  ]
}

约束:

  • events 数组最少 1 条,最多 50 条
  • 建议批量上报间隔 3-5 秒,或达到 10 条时立即上报
  • 上报失败时本地队列保留,按指数退避重试

数据脱敏规则

SDK 在上报前必须对以下字段脱敏:

数据位置规则
URL query移除 authorizationpasswordtokensecretcookie 等参数的值
HTTP body不上报 request body 和 response body
堆栈信息移除 URL 中的 query 参数
用户信息使用业务传入的 userId 或匿名 ID,不采集真实身份

Released under the MIT License.