From ddee2e82b3f9e6db2bb20bfc3d824a5eedae8628 Mon Sep 17 00:00:00 2001 From: aixianling Date: Thu, 9 Feb 2023 11:53:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E6=8F=90=E4=BA=A4=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E7=9B=91=E6=8E=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?--=E8=A7=82=E5=AF=9F=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sentry/AppApiMonitor/AppApiMonitor.vue | 65 +++++++++++++++---- ui/lib/js/observer.js | 39 +++++++++++ 2 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 ui/lib/js/observer.js 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