sobey-monitor-sdk 1.0.9 → 1.1.1

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 CHANGED
@@ -17,6 +17,27 @@ npm install sobey-monitor-sdk --save
17
17
 
18
18
  ## 快速开始
19
19
 
20
+ ### 方式一:使用远程配置(推荐)
21
+
22
+ 通过 `configUrl` 从服务端获取配置,无需手动维护 `appId` 和 `dsn`:
23
+
24
+ ```javascript
25
+ import { monitor } from 'sobey-monitor-sdk';
26
+
27
+ // 只需提供 configUrl,其他配置从远程获取
28
+ monitor.init({
29
+ configUrl: '/monitor/api/config/your-app-id'
30
+ });
31
+
32
+ // 也可以覆盖部分远程配置
33
+ monitor.init({
34
+ configUrl: '/monitor/api/config/your-app-id',
35
+ debug: true // 本地配置优先级高于远程配置
36
+ });
37
+ ```
38
+
39
+ ### 方式二:传统配置
40
+
20
41
  ```javascript
21
42
  import { monitor } from 'sobey-monitor-sdk';
22
43
 
@@ -113,8 +134,9 @@ ReactDOM.render(
113
134
 
114
135
  | 配置项 | 类型 | 默认值 | 说明 |
115
136
  |--------|------|--------|------|
116
- | `appId` | string | 必填 | 应用唯一标识 |
117
- | `dsn` | string | 必填 | 数据上报地址 |
137
+ | `configUrl` | string | - | 远程配置接口地址,填写后可从远程获取 `appId`、`dsn` 等配置(仅初始化时获取一次) |
138
+ | `appId` | string | 必填* | 应用唯一标识(使用 `configUrl` 时可不填) |
139
+ | `dsn` | string | 必填* | 数据上报地址(使用 `configUrl` 时可不填) |
118
140
  | `enabled` | boolean | true | **全局开关**,设为 false 则完全禁用所有监控上报 |
119
141
  | `debug` | boolean | false | 调试模式,开启后会在控制台输出日志 |
120
142
  | `sampling.error` | number | 1 | 错误采样率 (0-1) |
package/dist/index.cjs.js CHANGED
@@ -1647,41 +1647,56 @@ const SDK_VERSION = '1.0.0';
1647
1647
  class MonitorSDK {
1648
1648
  constructor() {
1649
1649
  this.initialized = false;
1650
+ this.initializing = false;
1650
1651
  }
1651
1652
  /**
1652
1653
  * 初始化 SDK
1653
1654
  * 如果配置了 configUrl,将先从远程获取配置(仅获取一次)
1655
+ * 无需 await,SDK 内部会自动处理异步逻辑
1654
1656
  */
1655
- async init(userConfig) {
1656
- if (this.initialized) {
1657
- console.warn('[Monitor] SDK already initialized');
1657
+ init(userConfig) {
1658
+ if (this.initialized || this.initializing) {
1659
+ if (this.initialized) {
1660
+ console.warn('[Monitor] SDK already initialized');
1661
+ }
1658
1662
  return;
1659
1663
  }
1660
- let finalConfig = { ...userConfig };
1661
- // 如果配置了 configUrl,从远程获取配置
1664
+ // 如果配置了 configUrl,异步获取远程配置后再初始化
1662
1665
  if (userConfig.configUrl) {
1663
- try {
1664
- const remoteConfig = await this.fetchRemoteConfig(userConfig.configUrl);
1666
+ this.initializing = true;
1667
+ this.fetchRemoteConfig(userConfig.configUrl)
1668
+ .then((remoteConfig) => {
1665
1669
  // 远程配置作为基础,用户本地配置覆盖远程配置
1666
- finalConfig = this.mergeUserConfig(remoteConfig, userConfig);
1670
+ const finalConfig = this.mergeUserConfig(remoteConfig, userConfig);
1667
1671
  if (userConfig.debug) {
1668
1672
  console.log('[Monitor] Remote config loaded:', remoteConfig);
1669
1673
  }
1670
- }
1671
- catch (error) {
1674
+ this.doInit(finalConfig, true);
1675
+ })
1676
+ .catch((error) => {
1677
+ this.initializing = false;
1672
1678
  console.error('[Monitor] Failed to fetch remote config:', error);
1673
- throw new Error('[Monitor] Failed to fetch remote config. Please check configUrl.');
1674
- }
1679
+ });
1675
1680
  }
1676
- // 初始化配置(远程配置模式下跳过初始校验,因为已经合并了远程配置)
1681
+ else {
1682
+ // 没有 configUrl,直接同步初始化
1683
+ this.doInit(userConfig, false);
1684
+ }
1685
+ }
1686
+ /**
1687
+ * 执行实际的初始化逻辑
1688
+ */
1689
+ doInit(finalConfig, isRemoteConfig) {
1690
+ // 初始化配置
1677
1691
  config.init({
1678
1692
  ...finalConfig,
1679
1693
  version: finalConfig.version || SDK_VERSION,
1680
- }, !!userConfig.configUrl);
1694
+ }, isRemoteConfig);
1681
1695
  // 初始化上下文
1682
1696
  const maxBreadcrumbs = config.get().behavior?.maxBreadcrumbs || 20;
1683
1697
  context.init(maxBreadcrumbs);
1684
1698
  this.initialized = true;
1699
+ this.initializing = false;
1685
1700
  if (config.get().debug) {
1686
1701
  console.log('[Monitor] SDK initialized', config.get());
1687
1702
  }
@@ -1744,12 +1759,32 @@ class MonitorSDK {
1744
1759
  /**
1745
1760
  * 动态更新配置
1746
1761
  * @description 可以在运行时更新 SDK 配置,如 debug、sampling、report 等
1762
+ * 如果传入 configUrl,会从远程获取配置并合并
1747
1763
  */
1748
1764
  updateConfig(partialConfig) {
1749
1765
  this.checkInit();
1750
- config.update(partialConfig);
1751
- if (config.get().debug) {
1752
- console.log('[Monitor] Config updated', partialConfig);
1766
+ // 如果传入了 configUrl,从远程获取配置并更新
1767
+ if (partialConfig.configUrl) {
1768
+ this.fetchRemoteConfig(partialConfig.configUrl)
1769
+ .then((remoteConfig) => {
1770
+ // 远程配置作为基础,传入的其他本地配置覆盖远程配置
1771
+ const mergedConfig = this.mergeUserConfig(remoteConfig, partialConfig);
1772
+ // 移除 configUrl,不需要存到配置里
1773
+ delete mergedConfig.configUrl;
1774
+ config.update(mergedConfig);
1775
+ if (config.get().debug) {
1776
+ console.log('[Monitor] Config updated from remote:', mergedConfig);
1777
+ }
1778
+ })
1779
+ .catch((error) => {
1780
+ console.error('[Monitor] Failed to fetch remote config:', error);
1781
+ });
1782
+ }
1783
+ else {
1784
+ config.update(partialConfig);
1785
+ if (config.get().debug) {
1786
+ console.log('[Monitor] Config updated', partialConfig);
1787
+ }
1753
1788
  }
1754
1789
  }
1755
1790
  /**