diff --git a/project/oms/apps/sentry/AppApiMonitor/AppApiMonitor.vue b/project/oms/apps/sentry/AppApiMonitor/AppApiMonitor.vue index f5489ce1..fd808bf4 100644 --- a/project/oms/apps/sentry/AppApiMonitor/AppApiMonitor.vue +++ b/project/oms/apps/sentry/AppApiMonitor/AppApiMonitor.vue @@ -4,13 +4,21 @@ @@ -19,26 +27,57 @@ diff --git a/ui/lib/js/observer.js b/ui/lib/js/observer.js new file mode 100644 index 00000000..d899ab73 --- /dev/null +++ b/ui/lib/js/observer.js @@ -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