New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@tuhu/mp-monitor

Package Overview
Dependencies
Maintainers
3
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tuhu/mp-monitor

监控SDK

  • 0.1.2
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
3
Weekly downloads
 
Created
Source

mp-monitor

监控SDK

安装

yarn add @tuhu/mp-monitor

使用

// app.ts
import Monitor from '@tuhu/mp-monitor';

Monitor({
  // 关闭调试日志
  debug: false,
  // 标识不同应用,c端小程序使用 wx_app, 与神策保持一致
  appId: 'xxx',
  // 当前发包的版本,没有可以不填
  packageVersion: 'x.0.2',
  onError(data) {
    // 神策上报script error
    sensors.track('minError', data);
  },
  onRequestEnd(data) {
    // 神策上报request性能数据
    sensors.track('min_performance_monitor', data);
  }
});

参数说明

  • debug

    开启调试。

  • appId

    指定应用的标识。

  • packageVersion

    当如果需要监控不同发布的版本时填写。

  • onError

    监听小程序错误事件。

    优先使用 wx.onError 监听, 如果没有wx.onError则使用 JApp.mixin 监听onError。

    如果没有无法使用 wx.onError也没有使用 JGB 框架,则自己监听 App.onError 事件:

    import { processError } from '@tuhu/mp-monitor'
    
    App({
      onError(err) {
        const data = processError(err);
        sensors.track('minError', data);
      }
    })
    
  • onRequestEnd

    监听请求的生命周期,并在一个请求生命周期结束时回调。

    优先使用 wx.request 拦截,如果无法使用则使用 jgb.intercept 拦截。

    如果无法使用wx.request也没有使用 JGB 框架,则需要自己实现请求拦截, 参考拦截 wx.request

    import { MonitorRequest, hook } from '@tuhu/mp-monitor'
    
    let oldRequest = wx.request;
    Object.defineProperty(wx, "request", {
      configurable: true,
      get() {
        return (opts: IRequestOptions) => {
          const monitorRequest = new MonitorRequest();
          const { url } = opts;
          // 域名黑名单,不上报
          if (hostblackbox.some(burl => url.includes(burl))) {
            return oldRequest(opts);
          }
    
          monitorRequest.collectFromRquestOptions(opts);
    
          hook(opts, "success", res => {
            monitorRequest.collectFromResponse(res);
          });
    
          hook(opts, "fail", res => {
            monitorRequest.collectFromResponse(res);
          });
    
          hook(opts, "complete", () => {
            const data = monitorRequest.processData();
            // 上报数据
            sensors.track('min_performance_monitor', data);
          });
    
          return oldRequest(opts);
        };
      },
      set(value) {
        oldRequest = value;
      }
    });
    
  • addBlackBox

    增加域名黑名单, 对应域名不做监控上报

    import { addBlackBox } from '@tuhu/mp-monitor'
    
    addBlackBox(['hi.tuhu.cn'])
    

FAQs

Package last updated on 30 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc