geca_monitor 1.0.0-beta.20260416174938

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +187 -0
  2. package/package.json +42 -0
package/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # VueMonitor SDK
2
+
3
+ Vue 前端性能与异常监控 SDK,支持 Vue2/Vue3 项目。
4
+
5
+ ## 特性
6
+
7
+ - ✅ 自动拦截 axios 请求,采集接口性能数据
8
+ - ✅ 自动捕获 Vue 错误、JS 错误、Promise 异常
9
+ - ✅ 异步非阻塞上报,不影响业务性能
10
+ - ✅ 支持批量上报,减少请求次数
11
+ - ✅ 使用 `sendBeacon`,页面关闭也能上报
12
+ - ✅ 轻量级,无外部依赖(除了 axios)
13
+ - ✅ 支持采样率配置
14
+ - ✅ 多租户数据隔离
15
+
16
+ ## 安装
17
+
18
+ ### 方式 1:npm 安装(推荐)
19
+
20
+ ```bash
21
+ npm install geca_monitor axios
22
+ # 或
23
+ yarn add geca_monitor axios
24
+ # 或
25
+ pnpm add geca_monitor axios
26
+ ```
27
+
28
+ ### 方式 2:直接复制源码
29
+
30
+ 将 `sdk/src` 目录复制到你的项目中,然后按需引入。
31
+
32
+ ### 方式 3:CDN 引用
33
+
34
+ ```html
35
+ <script src="https://your-cdn.com/vue-monitor.min.js"></script>
36
+ <script>
37
+ const monitor = VueMonitor.createMonitor({ appId: 'xxx', reportUrl: 'xxx' });
38
+ monitor.init();
39
+ </script>
40
+ ```
41
+
42
+ ## 使用方法
43
+
44
+ ### Vue 2 项目
45
+
46
+ ```javascript
47
+ // main.js
48
+ import Vue from 'vue';
49
+ import axios from 'axios';
50
+ import createMonitor from 'geca_monitor';
51
+
52
+ // 创建监控实例
53
+ const monitor = createMonitor({
54
+ appId: 'your-app-id', // 必填:项目 ID
55
+ reportUrl: 'https://your-api-domain/api/report', // 必填:上报地址
56
+ slowThreshold: 1000, // 可选:慢接口阈值,默认 1000ms
57
+ env: 'prod', // 可选:环境标识
58
+ sampleRate: 1, // 可选:采样率,默认 1 (100%)
59
+ });
60
+
61
+ // 初始化
62
+ monitor.init();
63
+
64
+ // 安装 axios 拦截器
65
+ monitor.useAxios(axios);
66
+
67
+ // Vue 2 自动捕获错误(SDK 自动处理)
68
+
69
+ new Vue({
70
+ // ...
71
+ });
72
+ ```
73
+
74
+ ### Vue 3 项目
75
+
76
+ ```javascript
77
+ // main.js
78
+ import { createApp } from 'vue';
79
+ import axios from 'axios';
80
+ import createMonitor from 'geca_monitor';
81
+ import App from './App.vue';
82
+
83
+ const app = createApp(App);
84
+
85
+ // 创建监控实例
86
+ const monitor = createMonitor({
87
+ appId: 'your-app-id',
88
+ reportUrl: 'https://your-api-domain/api/report',
89
+ slowThreshold: 1000,
90
+ env: 'prod',
91
+ });
92
+
93
+ // 初始化
94
+ monitor.init();
95
+
96
+ // 安装 axios 拦截器
97
+ monitor.useAxios(axios);
98
+
99
+ // 设置 Vue 3 错误处理器
100
+ monitor.setupVue3Error(app);
101
+
102
+ app.mount('#app');
103
+ ```
104
+
105
+ ### 手动上报异常
106
+
107
+ ```javascript
108
+ // 在业务代码中手动上报
109
+ try {
110
+ // 一些业务逻辑
111
+ } catch (error) {
112
+ monitor.reportError(error);
113
+ }
114
+
115
+ // 或者直接上报字符串
116
+ monitor.reportError('Something went wrong');
117
+ ```
118
+
119
+ ## 配置说明
120
+
121
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
122
+ |------|------|------|--------|------|
123
+ | appId | string | ✅ | - | 项目唯一标识 |
124
+ | reportUrl | string | ✅ | - | 数据上报地址 |
125
+ | slowThreshold | number | ❌ | 1000 | 慢接口阈值(毫秒) |
126
+ | env | string | ❌ | 'prod' | 环境标识(dev/test/prod) |
127
+ | sampleRate | number | ❌ | 1 | 采样率(0-1) |
128
+ | reportEnabled | boolean | ❌ | true | 是否开启上报 |
129
+
130
+ ## 采集数据
131
+
132
+ ### 接口性能数据
133
+
134
+ - URL(已去除查询参数)
135
+ - HTTP 方法
136
+ - 耗时(毫秒)
137
+ - 状态码
138
+ - 是否成功
139
+ - 是否慢接口
140
+ - 当前路由
141
+ - 时间戳
142
+
143
+ ### 异常数据
144
+
145
+ - 异常类型(vue/js/promise/api)
146
+ - 错误信息
147
+ - 堆栈信息
148
+ - 页面 URL
149
+ - 错误指纹(自动计算)
150
+ - 时间戳
151
+
152
+ ## 注意事项
153
+
154
+ 1. **必须在 axios 之前初始化**:确保 `monitor.useAxios(axios)` 在业务代码使用 axios 之前调用
155
+ 2. **Vue 3 需要手动设置**:调用 `monitor.setupVue3Error(app)` 才能捕获 Vue 3 错误
156
+ 3. **采样率**:高流量项目建议设置 `sampleRate: 0.1`(10% 采样)
157
+ 4. **上报地址**:确保上报地址可访问,建议使用 HTTPS
158
+
159
+ ## 构建
160
+
161
+ ```bash
162
+ # 安装依赖
163
+ npm install
164
+
165
+ # 构建
166
+ npm run build
167
+
168
+ # 开发模式
169
+ npm run dev
170
+
171
+ # 测试
172
+ npm test
173
+ ```
174
+
175
+ ## 输出文件
176
+
177
+ ```
178
+ dist/
179
+ ├── index.js # UMD 格式(浏览器 + Node)
180
+ ├── index.esm.js # ES Module 格式
181
+ ├── index.d.ts # TypeScript 类型定义
182
+ └── ... (sourcemap)
183
+ ```
184
+
185
+ ## License
186
+
187
+ MIT
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "geca_monitor",
3
+ "version": "1.0.0-beta.20260416174938",
4
+ "description": "Vue 前端性能与异常监控 SDK",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "scripts": {
15
+ "build": "rollup -c",
16
+ "dev": "rollup -c -w",
17
+ "test": "vitest",
18
+ "clean": "rm -rf dist"
19
+ },
20
+ "keywords": [
21
+ "vue",
22
+ "monitor",
23
+ "performance",
24
+ "exception",
25
+ "tracking"
26
+ ],
27
+ "author": "",
28
+ "license": "MIT",
29
+ "devDependencies": {
30
+ "@rollup/plugin-typescript": "^11.1.6",
31
+ "@types/node": "^20.10.0",
32
+ "rollup": "^4.9.0",
33
+ "rollup-plugin-dts": "^6.1.0",
34
+ "tslib": "^2.6.2",
35
+ "typescript": "^5.3.0",
36
+ "vitest": "^1.1.0"
37
+ },
38
+ "peerDependencies": {
39
+ "axios": ">=0.21.0"
40
+ },
41
+ "packageManager": "pnpm@10.15.1+sha512.34e538c329b5553014ca8e8f4535997f96180a1d0f614339357449935350d924e22f8614682191264ec33d1462ac21561aff97f6bb18065351c162c7e8f6de67"
42
+ }