You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2020/08/26 02:53:48 UTC

[skywalking-client-js] 28/48: refactor: data structure

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-client-js.git

commit b15e9791021e2eb4f48ae279abb8ff6f5758f0a7
Author: Qiuxia Fan <fi...@outlook.com>
AuthorDate: Wed Jan 8 12:33:33 2020 +0800

    refactor: data structure
---
 src/errors/ajax.ts      |  6 ++++--
 src/errors/js.ts        |  5 +++--
 src/errors/promise.ts   |  5 +++--
 src/monitor.ts          |  8 ++++----
 src/services/base.ts    | 17 +++++++++++------
 src/services/types.d.ts |  3 +--
 src/types.d.ts          |  2 +-
 7 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/src/errors/ajax.ts b/src/errors/ajax.ts
index 6143fe2..e0d0616 100644
--- a/src/errors/ajax.ts
+++ b/src/errors/ajax.ts
@@ -19,16 +19,18 @@ import Base from '../services/base';
 import { GradeTypeEnum, ErrorsCategory } from '../services/constant';
 
 class AjaxErrors extends Base {
-  public handleError(options: {reportUrl: string}) {
+  public handleError(options: {reportUrl: string; serviceName: string}) {
     if (!window.XMLHttpRequest) {
       return;
     }
+    this.reportUrl = options.reportUrl;
+    this.serviceName = options.serviceName;
+
     const xhrSend = XMLHttpRequest.prototype.send;
     const xhrEvent = (event: any) => {
       try {
         if (event && event.currentTarget && event.currentTarget.status !== 200) {
           this.logInfo = {
-            reportUrl: options.reportUrl,
             category: ErrorsCategory.AJAX_ERROR,
             grade: GradeTypeEnum.ERROR,
             errorUrl: event.target.responseURL,
diff --git a/src/errors/js.ts b/src/errors/js.ts
index 121d19f..e6b81f8 100644
--- a/src/errors/js.ts
+++ b/src/errors/js.ts
@@ -19,10 +19,11 @@ import Base from '../services/base';
 import { GradeTypeEnum, ErrorsCategory } from '../services/constant';
 
 class JSErrors extends Base {
-  public handleErrors(options: {reportUrl: string}) {
+  public handleErrors(options: {reportUrl: string; serviceName: string}) {
     window.onerror = (message, url, line, col, error) => {
+      this.reportUrl = options.reportUrl;
+      this.serviceName = options.serviceName;
       this.logInfo = {
-        reportUrl: options.reportUrl,
         category: ErrorsCategory.JS_ERROR,
         grade: GradeTypeEnum.ERROR,
         errorUrl: url,
diff --git a/src/errors/promise.ts b/src/errors/promise.ts
index 8767510..01d2209 100644
--- a/src/errors/promise.ts
+++ b/src/errors/promise.ts
@@ -19,18 +19,19 @@ import Base from '../services/base';
 import { GradeTypeEnum, ErrorsCategory } from '../services/constant';
 
 class PromiseErrors extends Base {
-  public handleErrors(options: {reportUrl: string}) {
+  public handleErrors(options: {reportUrl: string; serviceName: string}) {
     window.addEventListener('unhandledrejection', (event) => {
       try {
         let url = '';
         if (!event || !event.reason) {
           return;
         }
+        this.reportUrl = options.reportUrl;
+        this.serviceName = options.serviceName;
         if (event.reason.config && event.reason.config.url) {
           url = event.reason.config.url;
         }
         this.logInfo = {
-          reportUrl: options.reportUrl,
           category: ErrorsCategory.PROMISE_ERROR,
           grade: GradeTypeEnum.ERROR,
           errorUrl: url,
diff --git a/src/monitor.ts b/src/monitor.ts
index 582f1b4..a164606 100644
--- a/src/monitor.ts
+++ b/src/monitor.ts
@@ -30,23 +30,23 @@ const ClientMonitor = {
   } as CustomOptionsType,
 
   register(options: CustomOptionsType) {
-    const reportUrl = options.reportUrl;
+    const { serviceName, reportUrl } = options;
 
     this.customOptions = options;
     if (this.customOptions.jsErrors) {
       this.customOptions.jsErrors = options.jsErrors;
-      JSErrors.handleErrors({reportUrl});
+      JSErrors.handleErrors({reportUrl, serviceName});
     }
     if (this.customOptions.promiseErrors) {
       this.customOptions.promiseErrors = options.promiseErrors || this.customOptions.promiseErrors;
-      PromiseErrors.handleErrors({reportUrl});
+      PromiseErrors.handleErrors({reportUrl, serviceName});
     }
     if (this.customOptions.resourceErrors) {
       this.customOptions.resourceErrors = options.resourceErrors;
     }
     if (this.customOptions.ajaxErrors) {
       this.customOptions.ajaxErrors = options.ajaxErrors || this.customOptions.ajaxErrors;
-      AjaxErrors.handleError({reportUrl});
+      AjaxErrors.handleError({reportUrl, serviceName});
     }
   },
 };
diff --git a/src/services/base.ts b/src/services/base.ts
index a8b0c6f..fa655c9 100644
--- a/src/services/base.ts
+++ b/src/services/base.ts
@@ -19,7 +19,9 @@ import { ErrorsCategory, GradeTypeEnum } from './constant';
 import { errorInfoFeilds } from './types';
 
 export default class Base {
-  public logInfo: errorInfoFeilds & {reportUrl: string} = {
+  public reportUrl: string;
+  public serviceName: string;
+  public logInfo: errorInfoFeilds = {
     category: ErrorsCategory.UNKNOW_ERROR,
     grade: GradeTypeEnum.INFO,
     errorUrl: '',
@@ -27,7 +29,6 @@ export default class Base {
     col: 0,
     errorInfo: '',
     message: '',
-    reportUrl: '',
   };
 
   public traceInfo() {
@@ -42,13 +43,13 @@ export default class Base {
       if (!this.logInfo.message) {
         return;
       }
-      if (this.logInfo.reportUrl && this.logInfo.errorUrl &&
-        this.logInfo.errorUrl.toLowerCase().includes(this.logInfo.reportUrl.toLowerCase())) {
+      if (this.reportUrl && this.logInfo.errorUrl &&
+        this.logInfo.errorUrl.toLowerCase().includes(this.reportUrl.toLowerCase())) {
         return;
       }
       const errorInfo = this.handleErrorInfo();
 
-      Task.addTask(this.logInfo.reportUrl, errorInfo);
+      Task.addTask(this.reportUrl, errorInfo);
 
     } catch (error) {
       // console.log(error);
@@ -73,6 +74,10 @@ export default class Base {
       ...this.logInfo,
       message,
     };
-    return recordInfo;
+    return {
+      errorLogs: recordInfo,
+      serviceName: this.serviceName,
+      reportUrl: this.reportUrl,
+    };
   }
 }
diff --git a/src/services/types.d.ts b/src/services/types.d.ts
index b027d5e..f1ae5a6 100644
--- a/src/services/types.d.ts
+++ b/src/services/types.d.ts
@@ -15,8 +15,7 @@
  * limitations under the License.
  */
 export interface TraceFields {
-  reportUrl: string;
-  serviceName?: string;
+  serviceName: string;
   errorInfo: errorInfoFeilds;
   performanceInfo?: performanceInfoFields;
   resources?: any;
diff --git a/src/types.d.ts b/src/types.d.ts
index 0d6b2c3..e916ee6 100644
--- a/src/types.d.ts
+++ b/src/types.d.ts
@@ -17,7 +17,7 @@
 
 export interface CustomOptionsType {
   reportUrl: string;
-  modulesName?: string;
+  serviceName?: string;
   jsErrors: boolean;
   promiseErrors: boolean;
   consoleErrors: boolean;