0.1.9 • Published 9 months ago

shuyilink-collector-js v0.1.9

Weekly downloads
-
License
Apache 2.0
Repository
-
Last release
9 months ago

SHUYILINK COLLECTOR JS

shuyilink-collector-js Client-side JavaScript log, exception and tracing library.

  • Provide metrics and error collection to backend.
  • Lightweight
  • Make browser as a start of whole distributed tracing
  • Base on skywalking-client-js

Usage

Install

The shuyilink-collector-js runtime library is available at npm.

npm install shuyilink-collector-js --save

Quick Start

User could use register method to load and report data automatically.

import ClientCollector from 'shuyilink-collector-js';
Collector.register({
   service: 'ipc-web',
   pagePath: window.location.ref,
   serviceVersion: 'v1.0.0',
   enableLogs: true,
   consoleLogLevels: ['Info', 'Warning', 'Error'],
   useUploadLogs: true
})

Parameters

The register method supports the following parameters.

ParameterTypeDescriptionRequiredDefault Value
collectorStringIn default, the collected data would be reported to current domain(/browser/perfData. Then, typically, we recommend you use a Gateway/proxy to redirect the data to the OAP(resthost:restport). If you set this, the data could be reported to another domain, NOTE the Cross-Origin Resource Sharing (CORS) issuse and solution.false-
serviceStringproject ID.true-
serviceVersionStringproject verisontrue-
pagePathStringproject pathtrue-
jsErrorsBooleanSupport js errors monitoringfalsefalse
apiErrorsBooleanSupport API errors monitoringfalsefalse
resourceErrorsBooleanSupport resource errors monitoringfalsefalse
useFmpBooleanCollect FMP (first meaningful paint) data of the first screenfalsefalse
enableSPABooleanMonitor the page hashchange event and report PV, which is suitable for single page application scenariosfalsetrue
autoTracePerfBooleanSupport sending of performance data automatically.falsefalse
vueVueSupport vue errors monitoringfalseundefined
traceSDKInternalBooleanSupport tracing SDK internal RPC.falsefalse
detailModeBooleanSupport tracing http method and url as tags in spans.falsetrue
noTraceOrigins(string | RegExp)[]Origin in the noTraceOrigins list will not be traced.false[]
traceTimeIntervalNumberSupport setting time interval to report segments.false60000
customTagsArrayCustom Tagsfalse-
enableTraceSegmentBoolean是否开启链路追踪falsefalse
enableLogsBoolean是否开启手动日志采集falsetrue
useConsoleLogBoolean是否开启控制台打印日志falsetrue
consoleLogLevelsArray控制台打印的日志等级false-
useUploadLogsBoolean是否开启日志上报falsefalse
logsBufferSizeNumber日志采集器的容量限制false10000
uploadLogsLevelsArray日志上报的等级false-
uploadLogsTimeIntervalNumber日志上报的时间间隔false10000
uploadLogsBatchSizeNumber日志上报的单次条数限制false10000
customUrlForLogsString定制的日志上报接口true-
customUrlForErrorsString定制的错误上报接口(单条)true-
customUrlForErrorString定制的错误上报接口true-
customUrlForPerfString定制的性能分析上报接口true-
customUrlForSegmentString定制的链路追踪上报接口(单条)true-
customUrlForSegmentsString定制的链路追踪上报接口true-

Collect Metrics Manually

Use the setPerformance method to report metrics at the moment of page loaded or any other moment meaningful.

  1. Set the SDK configuration item autoTracePerf to false to turn off automatic reporting performance metrics and wait for manual triggering of escalation.
  2. Call Collector.setPerformance(object) method to report
  • Examples
import Collector from 'shuyilink-collector-js';

Collector.setPerformance({
  collector: 'http://127.0.0.1:12800',
  service: 'browser-app',
  serviceVersion: '1.0.0',
  pagePath: location.href,
  useFmp: true
});

Special scene

SPA Page

In spa (single page application) single page application, the page will be refreshed only once. The traditional method only reports PV once after the page loading, but cannot count the PV of each sub-page, and can't make other types of logs aggregate by sub-page.
The SDK provides two processing methods for spa pages:

  1. Enable spa automatic parsing
    This method is suitable for most single page application scenarios with URL hash as the route.
    In the initialized configuration item, set enableSPA to true, which will turn on the page's hashchange event listening (trigger re reporting PV), and use URL hash as the page field in other data reporting.
  2. Manual reporting
    This method can be used in all single page application scenarios. This method can be used if the first method is invalid.
    The SDK provides a set page method to manually update the page name when data is reported. When this method is called, the page PV will be re reported by default. For details, see setPerformance().
app.on('routeChange', function (next) {
   Collector.setPerformance({
      collector: 'http://127.0.0.1:12800',
      service: 'browser-app',
      serviceVersion: '1.0.0',
      pagePath: location.href,
      useFmp: true
   });
});   

Collect logs manually

It's similar to console.log / console.warn / console.error

Collector.info(`TestLogCollector >> ${count} >> Info`, 'xxxx')

Collector.warn(`TestLogCollector >> ${count} >> Warning`, 'xxxx')

Collector.error(`TestLogCollector >> ${count} >> Error`, 'xxxx')

Tracing range of data requests in the browser

Support tracking these(XMLHttpRequest and 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, SuperAgent, OpenApi and so on.

Catching errors in frames

// Vue
Vue.config.errorHandler = (error) => {
   Collector.reportFrameErrors({
      collector: 'http://127.0.0.1:12800',
      service: 'vue-demo',
      pagePath: '/app',
      serviceVersion: 'v1.0.0',
   }, error);
}

According to different pages or modules, add custom tags to spans.

app.on('routeChange', function () {
   Collector.setCustomTags([
      {key: 'key1', value: 'value1'},
      {key: 'key2', value: 'value2'},
   ]);
});

License

Apache 2.0

0.1.9

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.16

10 months ago

0.0.15

10 months ago

0.0.14

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.9.0

10 months ago