UNPKG

@vectorx/cloud-toolkit

Version:

VectorX Cloud Toolkit

135 lines (134 loc) 5.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.APM = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const uuid_1 = __importDefault(require("uuid")); const logger_1 = require("../logger"); const device_1 = require("./device"); const deviceInfo = (0, device_1.getDeviceInfo)(); const getUserAgent = () => { var _a, _b; const electronStr = typeof ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.electron) !== "undefined" ? `Electron/${(_b = process === null || process === void 0 ? void 0 : process.versions) === null || _b === void 0 ? void 0 : _b.electron}` : ""; const nodeStr = `Node/${process.version}`; const os_version = `${deviceInfo.os_platform}/${deviceInfo.os_version}`; const cpu = `CPU/${deviceInfo.cpu_speed}mhz (${deviceInfo.cpu_model}; ${deviceInfo.os_arch})`; const mem = `Memory/${deviceInfo.machine_memory}mb`; return [electronStr, nodeStr, os_version, cpu, mem] .map((i) => { var _a; return (_a = i === null || i === void 0 ? void 0 : i.trim) === null || _a === void 0 ? void 0 : _a.call(i); }) .filter(Boolean) .join(" ") .trim(); }; const CONTEXT = { context_nameTracker: "nodeT", context_platform: deviceInfo.os_platform, context_appVersion: "0.0.1", context_osVersion: deviceInfo.os_version, context_deviceModel: "", context_deviceId: (0, device_1.machineId)(), context_package: "", context_networkType: "unknow", context_matchedPath: "cli", context_route: "xhsmp://cli", context_userAgent: getUserAgent(), context_artifactName: "miniprogram", context_networkQuality: "UNKNOWN", context_artifactVersion: "UNKNOWN", context_userId: "", }; const ENDPOINT = { development: "https://apm-fe.xiaohongshu.com/api/data", production: "https://apm-fe.xiaohongshu.com/api/data", }; class APM { constructor() { this.reportQueue = []; this.allowReport = false; this.eventSeq = 1; this.context = Object.assign({}, CONTEXT); this.baseData = {}; this.timer = null; } setContext(userId, base = {}) { this.context = Object.assign(Object.assign({}, this.context), { context_userId: userId }); this.baseData = base; this.startReport(); } startReport() { this.allowReport = true; this.bactchReport(); } getSequence() { return { context_sdkSeqId: this.eventSeq++, context_sdkSessionId: uuid_1.default.v4(), context_pageSessionId: uuid_1.default.v4(), clientTime: Date.now(), }; } _request(params, retry = true) { const list = Array.isArray(params) ? params : [params]; const merged = list.map((i) => { return Object.assign(Object.assign(Object.assign({}, this.context), i.suquence), { measurement_name: i.measurement_name, measurement_data: Object.assign(Object.assign({}, this.baseData), i.measurement_data) }); }); logger_1.logger.debug(`apm before send`, `${JSON.stringify(merged)}`, "warning"); return (0, node_fetch_1.default)(ENDPOINT.development, { method: "post", body: JSON.stringify(merged), headers: { accept: "*/*", "accept-language": "zh-CN,zh;q=0.9", batch: "true", "biz-type": "apm_fe", "cache-control": "no-cache", "content-type": "application/json", pragma: "no-cache", }, }) .then((res) => res.json()) .then((res) => { if (res === null || res === void 0 ? void 0 : res.success) { logger_1.logger.debug("apm send success", ""); } else { throw new Error(res === null || res === void 0 ? void 0 : res.msg); } }) .catch((err) => { if (retry) { setTimeout(() => this._request(merged, false), 1000); } logger_1.logger.log(`apm send failed`, `${(err === null || err === void 0 ? void 0 : err.message) || null}`, "error"); }); } bactchReport() { const queue = [...this.reportQueue]; this.reportQueue = []; while (queue.length) { this._request(queue.splice(0, 5)); } } report(measurement_name, measurement_data = {}) { const params = { suquence: this.getSequence(), measurement_name, measurement_data, }; this.reportQueue.push(params); clearTimeout(this.timer); if (this.allowReport) { if (this.reportQueue.length >= 10) { this.bactchReport(); } else { this.timer = setTimeout(() => { this.bactchReport(); }, 200); } } } } exports.APM = APM;