一、beforeRequest
该钩子将会在所有的数据上报前执行,来帮助用户在数据上报前对其进行屏蔽和修改,当 beforeRequest 返回 false 就可以不上报该条日志,返回修改过后的 data 就可以实现对上报数据的修改,例如:
注意:SDK 版本应大于等于1.24.44。
const aegis = new Aegis({ id: 'pGUVFTCZyewxxxxx', beforeRequest: function(data) { // 入参msg的数据结构:{logs: {…}, logType: "log"} if (data.logType === 'log' && data.logs.msg.indexOf('otheve.beacon.qq.com') > -1) { // 拦截:日志类型为 log,且内容包含 otheve.beacon.qq.com 的请求 return false; } // 入参data数据结构:{logs: {}, logType: "speed"} if (data.logType === 'speed' && data.logs.url.indexOf('otheve.beacon.qq.com') > -1) { // 拦截:日志类型为 speed,并且接口 url 包含 otheve.beacon.qq.com 的请求 return false; } if (data.logType === 'performance') { // 修改:将性能数据的首屏渲染时间改为2s data.logs.firstScreenTiming = 2000; } return data; }});
其中,msg 将会有以下几个字段:
·logType:日志类型,有以下值:
·custom:自定义测速
·event:自定义事件
·log:日志
·performance:页面测速
·pv:页面PV
·speed:接口和静态资源测速
·vitals:web vitals
·logs:上报的日志内容:
·当 logType 为 'custom' 时,logs 数据类型为 {name: "白屏时间", duration: 3015.7000000178814, ext1: '', ext2: '', ext3: ''}
·当 logType 为 'event' 时,logs 数据类型为 {name: "ios", ext1: "", ext2: "", ext3: ""}
·当 logType 为 'performance' 时,logs 数据类型为 {contentDownload: 2, dnsLookup: 0, domParse: 501, firstScreenTiming: 2315, resourceDownload: 260, ssl: 4, tcp: 4, ttfb: 5}
·当 logType 为 'speed' 时,logs 数据类型为 {connectTime: 0, domainLookup: 0, duration: 508.2, isHttps: true, method: "get", status: 200, type: "tatic", url: "https://xxxxxx", urlQuery: "max_age=1296000"}
·当 logType 为 'vitals' 时,logs 数据类型为 {delta: 1100, entries: [PerformancePaintTiming], id: "v1-1629344653118-4916457684758", name: "CP", value: 1100}
·当 logType 为 'log' 时,logs 数据类型为 {msg: "日志详情", level: '4', ext1: '', ext2: '', ext3: '', trace: ''}
注意:其中 level 枚举值如下
该钩子返回 false 时,本条日志将不会进行上报,该功能可用来过滤某些不需要上报的错误,可以用来过滤不希望上报的日志。
二、afterRequest
该钩子将会在测速数据上报后被执行,例如:
注意:SDK 版本应大于等于1.24.44。
const aegis = new Aegis({ id: "pGUVFTCZyewxxxxx", afterRequest: function(msg) { // {isErr: false, result: Array(1), logType: "log", logs: Array(4)} console.log(msg); }});
其中,msg 将会有以下几个字段:
·isErr:请求上报接口是否错误。
·result:上报接口的返回结果。
·logs:上报的日志内容。
·logType:日志类型,同 beforeRequest 中的 logType。