sobey-monitor-sdk 1.0.4 → 1.0.6
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.
- package/README.md +20 -7
- package/dist/index.cjs.js +76 -36
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +17 -0
- package/dist/index.esm.js +76 -36
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +76 -36
- package/dist/index.umd.js.map +1 -1
- package/dist/types/index.d.ts +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,6 +35,22 @@ monitor.init({
|
|
|
35
35
|
### `monitor.setUser(userInfo)`
|
|
36
36
|
设置当前用户信息,方便追踪。
|
|
37
37
|
|
|
38
|
+
### `monitor.updateConfig(partialConfig)`
|
|
39
|
+
动态更新 SDK 配置。可以在运行时修改采样率、调试模式等配置。
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
// 示例:动态开启调试模式
|
|
43
|
+
monitor.updateConfig({ debug: true });
|
|
44
|
+
|
|
45
|
+
// 示例:动态调整采样率
|
|
46
|
+
monitor.updateConfig({
|
|
47
|
+
sampling: { error: 0.5, performance: 0.1 }
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### `monitor.getConfig()`
|
|
52
|
+
获取当前 SDK 配置。
|
|
53
|
+
|
|
38
54
|
### `monitor.captureError(error, extra)`
|
|
39
55
|
手动上报异常或错误。
|
|
40
56
|
|
|
@@ -103,17 +119,14 @@ ReactDOM.render(
|
|
|
103
119
|
| `sampling.error` | number | 1 | 错误采样率 (0-1) |
|
|
104
120
|
| `sampling.performance` | number | 1 | 性能采样率 (0-1) |
|
|
105
121
|
| `sampling.behavior` | number | 1 | 行为采样率 (0-1) |
|
|
122
|
+
| `behavior.maxBreadcrumbs` | number | 20 | 行为回溯最大记录数 |
|
|
123
|
+
| `behavior.recordRequestBreadcrumb` | 'all' \| 'error' \| 'none' | 'error' | 网络请求记录模式:'all' 记录所有、'error' 记录非 2xx 响应(重定向、4xx、5xx、网络错误)、'none' 不记录 |
|
|
106
124
|
| `report.maxBufferSize` | number | 10 | 缓冲区最大数量 |
|
|
107
125
|
| `report.flushInterval` | number | 5000 | 上报间隔 (ms) |
|
|
108
126
|
|
|
109
|
-
##
|
|
110
|
-
|
|
111
|
-
```bash
|
|
112
|
-
npm install
|
|
113
|
-
npm run build
|
|
114
|
-
```
|
|
127
|
+
## 产物清单
|
|
115
128
|
|
|
116
|
-
|
|
129
|
+
构建产物生成在 `dist` 目录中:
|
|
117
130
|
- `index.cjs.js`: CommonJS 格式
|
|
118
131
|
- `index.esm.js`: ES Module 格式
|
|
119
132
|
- `index.umd.js`: UMD 格式 (支持 CDN 引入)
|
package/dist/index.cjs.js
CHANGED
|
@@ -36,6 +36,7 @@ const DEFAULT_CONFIG = {
|
|
|
36
36
|
click: true,
|
|
37
37
|
route: true,
|
|
38
38
|
maxBreadcrumbs: 20,
|
|
39
|
+
recordRequestBreadcrumb: 'error',
|
|
39
40
|
},
|
|
40
41
|
};
|
|
41
42
|
/**
|
|
@@ -662,18 +663,26 @@ function interceptXHR() {
|
|
|
662
663
|
return;
|
|
663
664
|
const duration = Date.now() - monitorData.startTime;
|
|
664
665
|
const status = this.status;
|
|
665
|
-
//
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
666
|
+
// 根据配置决定是否添加到面包屑
|
|
667
|
+
const cfg = config.get();
|
|
668
|
+
const recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
669
|
+
// 重定向(3xx)始终记录,不受配置控制
|
|
670
|
+
const isRedirect = status >= 300 && status < 400;
|
|
671
|
+
// 其他异常情况(0-网络错误、4xx-客户端错误、5xx-服务端错误)根据配置控制
|
|
672
|
+
const isError = status === 0 || status < 200 || status >= 400;
|
|
673
|
+
const shouldRecord = isRedirect || recordMode === 'all' || (recordMode === 'error' && isError);
|
|
674
|
+
if (shouldRecord) {
|
|
675
|
+
context.addBreadcrumb({
|
|
676
|
+
type: 'request',
|
|
677
|
+
category: 'xhr',
|
|
678
|
+
data: {
|
|
679
|
+
method: monitorData.method,
|
|
680
|
+
url: monitorData.url,
|
|
681
|
+
status,
|
|
682
|
+
duration,
|
|
683
|
+
},
|
|
684
|
+
});
|
|
685
|
+
}
|
|
677
686
|
if (status === 0 || status >= 400) {
|
|
678
687
|
reportHttpError({
|
|
679
688
|
method: monitorData.method,
|
|
@@ -705,18 +714,26 @@ function interceptFetch() {
|
|
|
705
714
|
const response = await originalFetch.call(window, input, init);
|
|
706
715
|
const duration = Date.now() - startTime;
|
|
707
716
|
const status = response.status;
|
|
708
|
-
//
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
717
|
+
// 根据配置决定是否添加到面包屑
|
|
718
|
+
const cfg = config.get();
|
|
719
|
+
const recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
720
|
+
// 重定向(3xx)始终记录,不受配置控制
|
|
721
|
+
const isRedirect = status >= 300 && status < 400;
|
|
722
|
+
// 其他异常情况根据配置控制
|
|
723
|
+
const isError = status < 200 || status >= 400;
|
|
724
|
+
const shouldRecord = isRedirect || recordMode === 'all' || (recordMode === 'error' && isError);
|
|
725
|
+
if (shouldRecord) {
|
|
726
|
+
context.addBreadcrumb({
|
|
727
|
+
type: 'request',
|
|
728
|
+
category: 'fetch',
|
|
729
|
+
data: {
|
|
730
|
+
method,
|
|
731
|
+
url,
|
|
732
|
+
status,
|
|
733
|
+
duration,
|
|
734
|
+
},
|
|
735
|
+
});
|
|
736
|
+
}
|
|
720
737
|
if (!response.ok) {
|
|
721
738
|
// 克隆响应以读取 body
|
|
722
739
|
const cloned = response.clone();
|
|
@@ -739,18 +756,23 @@ function interceptFetch() {
|
|
|
739
756
|
}
|
|
740
757
|
catch (error) {
|
|
741
758
|
const duration = Date.now() - startTime;
|
|
742
|
-
//
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
759
|
+
// 网络错误时根据配置决定是否添加到面包屑
|
|
760
|
+
const cfg = config.get();
|
|
761
|
+
const recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
762
|
+
// 网络错误属于 error,当模式为 'all' 或 'error' 时都记录
|
|
763
|
+
if (recordMode !== 'none') {
|
|
764
|
+
context.addBreadcrumb({
|
|
765
|
+
type: 'request',
|
|
766
|
+
category: 'fetch',
|
|
767
|
+
data: {
|
|
768
|
+
method,
|
|
769
|
+
url,
|
|
770
|
+
status: 0,
|
|
771
|
+
duration,
|
|
772
|
+
error: error.message,
|
|
773
|
+
},
|
|
774
|
+
});
|
|
775
|
+
}
|
|
754
776
|
reportHttpError({
|
|
755
777
|
method,
|
|
756
778
|
url,
|
|
@@ -1627,6 +1649,24 @@ class MonitorSDK {
|
|
|
1627
1649
|
this.checkInit();
|
|
1628
1650
|
config.setUser(user);
|
|
1629
1651
|
}
|
|
1652
|
+
/**
|
|
1653
|
+
* 动态更新配置
|
|
1654
|
+
* @description 可以在运行时更新 SDK 配置,如 debug、sampling、report 等
|
|
1655
|
+
*/
|
|
1656
|
+
updateConfig(partialConfig) {
|
|
1657
|
+
this.checkInit();
|
|
1658
|
+
config.update(partialConfig);
|
|
1659
|
+
if (config.get().debug) {
|
|
1660
|
+
console.log('[Monitor] Config updated', partialConfig);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* 获取当前配置
|
|
1665
|
+
*/
|
|
1666
|
+
getConfig() {
|
|
1667
|
+
this.checkInit();
|
|
1668
|
+
return config.get();
|
|
1669
|
+
}
|
|
1630
1670
|
/**
|
|
1631
1671
|
* 手动上报错误
|
|
1632
1672
|
*/
|