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/12/29 02:17:04 UTC

[skywalking-client-js] branch master updated: feat: support intercepting fetch http traffic (#28)

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


The following commit(s) were added to refs/heads/master by this push:
     new 0ab5e55  feat: support intercepting fetch http traffic (#28)
0ab5e55 is described below

commit 0ab5e5552079952a706a98bfcaaf2db4acbdab22
Author: Qiuxia Fan <fi...@outlook.com>
AuthorDate: Tue Dec 29 10:16:57 2020 +0800

    feat: support intercepting fetch http traffic (#28)
---
 README.md                                          |  9 ++++++-
 dist/licenses/LICENSES-fetch.txt                   | 20 ++++++++++++++
 package-lock.json                                  |  5 ++++
 package.json                                       |  3 ++-
 .../constant.ts => interceptors/fetch.js}          | 31 +++-------------------
 src/monitor.ts                                     |  1 -
 src/services/constant.ts                           |  1 +
 src/trace/segment.ts                               |  8 +++---
 8 files changed, 44 insertions(+), 34 deletions(-)

diff --git a/README.md b/README.md
index c535202..f79451b 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ Apache SkyWalking Client JS
 The `skywalking-client-js` runtime library is available at [npm](https://www.npmjs.com/package/skywalking-client-js).
 
 ```
-npm install skywalking-client-js --save
+npm install skywalking-client-js --save-dev
 ```
 
 ## Quick Start
@@ -29,6 +29,7 @@ import ClientMonitor from 'skywalking-client-js';
 ```
 // Report collected data to `http:// + window.location.host + /browser/perfData` in default
 ClientMonitor.register({
+  collector: 'http://127.0.0.1:8080',
   service: 'test-ui',
   pagePath: '/current/page/name',
   serviceVersion: 'v1.0.0',
@@ -63,6 +64,7 @@ Use the `setPerformance` method to report metrics at the moment of page loaded o
 import ClientMonitor from 'skywalking-client-js';
 
 ClientMonitor.setPerformance({
+  collector: 'http://127.0.0.1:8080',
   service: 'browser-app',
   serviceVersion: '1.0.0',
   pagePath: location.href,
@@ -85,6 +87,7 @@ The SDK provides a set page method to manually update the page name when data is
 ```
 app.on('routeChange', function (next) {
   ClientMonitor.setPerformance({
+    collector: 'http://127.0.0.1:8080',
     service: 'browser-app',
     serviceVersion: '1.0.0',
     pagePath: location.href,
@@ -93,6 +96,10 @@ app.on('routeChange', function (next) {
 });   
 ```
 
+## Tracing range of data requests in the broswer
+
+Support tracking these([XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) and [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)) two modes of data requests. At the same time, Support tracking libraries and tools that base on XMLHttpRequest and fetch, such as [Axios](https://github.com/axios/axios), [SuperAgent](https://github.com/visionmedia/superagent), [OpenApi](https://www.openapis.org/) and so on.
+
 # Demo project
 
 Demo project provides instrumented web application with necessary environment, you could just simple use it to see the data SkyWalking collected and how SkyWalking visualizes on the UI. 
diff --git a/dist/licenses/LICENSES-fetch.txt b/dist/licenses/LICENSES-fetch.txt
new file mode 100644
index 0000000..0e319d5
--- /dev/null
+++ b/dist/licenses/LICENSES-fetch.txt
@@ -0,0 +1,20 @@
+Copyright (c) 2014-2016 GitHub, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/package-lock.json b/package-lock.json
index ec5341d..f07185e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -7636,6 +7636,11 @@
       "integrity": "sha512-nqHUnMXmBzT0w570r2JpJxfiSD1IzoI+HGVdd3aZ0yNi3ngvQ4jv1dtHt5VGxfI2yj5yqImPhOK4vmIh2xMbGg==",
       "dev": true
     },
+    "whatwg-fetch": {
+      "version": "3.5.0",
+      "resolved": "https://registry.npm.taobao.org/whatwg-fetch/download/whatwg-fetch-3.5.0.tgz",
+      "integrity": "sha1-YFos0KcUbl2xQeKdHGKrhMDEyGg="
+    },
     "which": {
       "version": "1.3.1",
       "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
diff --git a/package.json b/package.json
index 1f036b4..b38f223 100644
--- a/package.json
+++ b/package.json
@@ -59,6 +59,7 @@
     "web-performance"
   ],
   "dependencies": {
-    "js-base64": "^3.6.0"
+    "js-base64": "^3.6.0",
+    "whatwg-fetch": "^3.5.0"
   }
 }
diff --git a/src/services/constant.ts b/src/interceptors/fetch.js
similarity index 55%
copy from src/services/constant.ts
copy to src/interceptors/fetch.js
index db62e3d..62b9ea6 100644
--- a/src/services/constant.ts
+++ b/src/interceptors/fetch.js
@@ -1,5 +1,3 @@
-import Report from './report';
-
 /**
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -16,31 +14,8 @@ import Report from './report';
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-export enum ErrorsCategory {
-  AJAX_ERROR = 'ajax',
-  RESOURCE_ERROR = 'resource',
-  VUE_ERROR = 'vue',
-  PROMISE_ERROR = 'promise',
-  JS_ERROR = 'js',
-  UNKNOWN_ERROR = 'unknown',
-}
-export enum GradeTypeEnum {
-  INFO = 'Info',
-  WARNING = 'Warning',
-  ERROR = 'Error',
-}
-export enum ReportTypes {
-  ERROR = '/browser/errorLog',
-  ERRORS = '/browser/errorLogs',
-  PERF = '/browser/perfData',
-  SEGMENT = '/v3/segment',
-  SEGMENTS = '/v3/segments',
-}
 
-export const SpanLayer = 'Http';
-export const SpanType = 'Exit';
-export enum ReadyStatus {
-  OPENED = 1,
-  DONE = 4,
+import { fetch } from 'whatwg-fetch';
+export default function windowFetch() {
+  window.fetch = fetch;
 }
-export const ComponentId = 10001; // ajax
diff --git a/src/monitor.ts b/src/monitor.ts
index bbc5f7f..f806c2a 100644
--- a/src/monitor.ts
+++ b/src/monitor.ts
@@ -19,7 +19,6 @@ import { CustomOptionsType } from './types';
 import { JSErrors, PromiseErrors, AjaxErrors, ResourceErrors, VueErrors } from './errors/index';
 import Performance from './performance/index';
 import traceSegment from './trace/segment';
-import uuid from './services/uuid';
 
 const ClientMonitor = {
   customOptions: {
diff --git a/src/services/constant.ts b/src/services/constant.ts
index db62e3d..9e1f172 100644
--- a/src/services/constant.ts
+++ b/src/services/constant.ts
@@ -44,3 +44,4 @@ export enum ReadyStatus {
   DONE = 4,
 }
 export const ComponentId = 10001; // ajax
+export const ServiceTag = '<browser>';
diff --git a/src/trace/segment.ts b/src/trace/segment.ts
index 040530f..6dfe8dc 100644
--- a/src/trace/segment.ts
+++ b/src/trace/segment.ts
@@ -19,18 +19,20 @@ import xhrInterceptor from '../interceptors/xhr';
 import uuid from '../services/uuid';
 import Report from '../services/report';
 import { SegmentFeilds, SpanFeilds } from './type';
-import { SpanLayer, SpanType, ReadyStatus, ComponentId } from '../services/constant';
+import { SpanLayer, SpanType, ReadyStatus, ComponentId, ServiceTag } from '../services/constant';
 import { CustomOptionsType } from '../types';
+import windowFetch from '../interceptors/fetch';
 
 export default function traceSegment(options: CustomOptionsType) {
   const segments = [] as any;
   const segCollector: { event: XMLHttpRequest; startTime: number }[] | any = [];
   // inject interceptor
   xhrInterceptor();
+  windowFetch();
   window.addEventListener('xhrReadyStateChange', (event: CustomEvent) => {
     const segment = {
       traceId: uuid(),
-      service: options.service,
+      service: options.service + ServiceTag,
       spans: [],
       serviceInstance: options.serviceVersion,
       traceSegmentId: uuid(),
@@ -45,7 +47,7 @@ export default function traceSegment(options: CustomOptionsType) {
       });
       const traceIdStr = String(Base64.encode(segment.traceId));
       const segmentId = String(Base64.encode(segment.traceSegmentId));
-      const service = String(Base64.encode(segment.service));
+      const service = String(Base64.encode(segment.service + ServiceTag));
       const instance = String(Base64.encode(segment.serviceInstance));
       const endpoint = String(Base64.encode(options.pagePath));
       const peer = String(Base64.encode(location.href));