先提交一下前端监控机器人--观察者

This commit is contained in:
aixianling
2023-02-09 11:53:35 +08:00
parent 6a5078dbb7
commit ddee2e82b3
2 changed files with 91 additions and 13 deletions

39
ui/lib/js/observer.js Normal file
View File

@@ -0,0 +1,39 @@
/**
* 获取符合要求的请求
* @param entries 监测的请求对象
* @param type 设置满足条件的请求类型
* @returns {PerformanceEntry[]}
*/
const getRequests = (entries = performance.getEntriesByType('resource'), type = ['xmlhttprequest']) =>
entries?.filter(e => type.includes(e.initiatorType)) || []
/**
* 观察者工具对象,用于前端接口监测
*/
class Observer {
constructor() {
this.saveLogs(getRequests())
this.ins = new PerformanceObserver((list, ob) => {
const watchLogs = getRequests(list.getEntriesByType("resource"))
this.saveLogs(watchLogs)
})
this.ins.observe({entryTypes: ["resource"]})
}
saveLogs(list = []) {
list.map(e => {
if (!/sockjs/.test(e.name)) {
const api = {
status: e.responseStatus,
path: e.name,
url: location.href,
nodeProcess: process.env.NODE_ENV,
}
console.log(api)
// http.post("/node/monitorApi/addOrUpdate", api)
}
})
}
}
export default Observer