@mtn-ui-z/monitor 0.0.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.
@@ -0,0 +1,40 @@
1
+ import { ApiData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * API 监控类
5
+ * 负责拦截并上报 API 请求数据
6
+ */
7
+ export declare class ApiMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /** 原始 fetch 方法 */
13
+ private originalFetch;
14
+ /** 原始 XMLHttpRequest */
15
+ private originalXhr;
16
+ /** 是否已启动 */
17
+ private isStarted;
18
+ /**
19
+ * 构造函数
20
+ * @param options - 监控配置
21
+ * @param reportFn - 上报函数
22
+ */
23
+ constructor(options: MonitorOptions, reportFn: (data: ApiData) => void);
24
+ /**
25
+ * 启动 API 监控
26
+ */
27
+ start(): void;
28
+ /**
29
+ * 停止 API 监控
30
+ */
31
+ stop(): void;
32
+ /**
33
+ * 拦截 fetch 请求
34
+ */
35
+ private interceptFetch;
36
+ /**
37
+ * 拦截 XMLHttpRequest
38
+ */
39
+ private interceptXhr;
40
+ }
@@ -0,0 +1,46 @@
1
+ import { BehaviorData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 用户行为监控类
5
+ * 负责采集用户交互行为数据
6
+ */
7
+ export declare class BehaviorMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /** 是否已启动 */
13
+ private isStarted;
14
+ /** 点击事件处理器(用于 stop 时移除) */
15
+ private boundClickHandler;
16
+ /** 滚动事件处理器(用于 stop 时移除) */
17
+ private boundScrollHandler;
18
+ /** 可见性变化处理器(用于 stop 时移除) */
19
+ private boundVisibilityHandler;
20
+ /**
21
+ * 构造函数
22
+ * @param options - 监控配置
23
+ * @param reportFn - 上报函数
24
+ */
25
+ constructor(options: MonitorOptions, reportFn: (data: BehaviorData) => void);
26
+ /**
27
+ * 启动用户行为监控
28
+ */
29
+ start(): void;
30
+ /**
31
+ * 停止用户行为监控
32
+ */
33
+ stop(): void;
34
+ /**
35
+ * 监听点击事件
36
+ */
37
+ private observeClick;
38
+ /**
39
+ * 监听滚动事件
40
+ */
41
+ private observeScroll;
42
+ /**
43
+ * 监听页面可见性变化
44
+ */
45
+ private observeVisibility;
46
+ }
@@ -0,0 +1,29 @@
1
+ import { CustomData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 自定义事件监控类
5
+ * 负责处理用户手动上报的自定义事件
6
+ */
7
+ export declare class CustomMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /**
13
+ * 构造函数
14
+ * @param options - 监控配置
15
+ * @param reportFn - 上报函数
16
+ */
17
+ constructor(options: MonitorOptions, reportFn: (data: CustomData) => void);
18
+ /**
19
+ * 上报自定义事件
20
+ * @param eventName - 事件名称
21
+ * @param eventData - 事件数据
22
+ */
23
+ report(eventName: string, eventData?: Record<string, unknown>): void;
24
+ /**
25
+ * 上报页面停留事件
26
+ * @param duration - 停留时长(毫秒)
27
+ */
28
+ reportStay(duration: number): void;
29
+ }
@@ -0,0 +1,72 @@
1
+ import { ErrorData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 错误监控类
5
+ * 负责捕获并上报各类前端错误
6
+ */
7
+ export declare class ErrorMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /** 绑定的事件处理器 - JS 错误 */
13
+ private boundErrorHandler;
14
+ /** 绑定的事件处理器 - Promise 未捕获异常 */
15
+ private boundPromiseHandler;
16
+ /** 绑定的事件处理器 - 资源加载错误 */
17
+ private boundResourceHandler;
18
+ /** Vue 错误处理器 */
19
+ private vueErrorHandler;
20
+ /**
21
+ * 构造函数
22
+ * @param options - 监控配置
23
+ * @param reportFn - 上报函数
24
+ */
25
+ constructor(options: MonitorOptions, reportFn: (data: ErrorData) => void);
26
+ /**
27
+ * 启动错误监控
28
+ * 添加全局事件监听器
29
+ */
30
+ start(): void;
31
+ /**
32
+ * 停止错误监控
33
+ * 移除所有事件监听器
34
+ */
35
+ stop(): void;
36
+ /**
37
+ * 创建 Vue 错误处理器
38
+ * 用于设置到 app.config.errorHandler
39
+ * @param customHandler - 用户自定义的处理函数(可选),会在监控上报后调用
40
+ * @returns Vue 错误处理函数
41
+ */
42
+ createVueErrorHandler(customHandler?: (err: Error, instance: unknown, info: string) => void): (err: Error, instance: unknown, info: string) => void;
43
+ /**
44
+ * 注册 Vue 错误处理器(兼容旧版)
45
+ * @param handler - 用户自定义的 Vue 错误处理函数
46
+ * @returns Vue 错误处理器函数
47
+ * @deprecated 请使用 createVueErrorHandler 代替
48
+ */
49
+ registerVueHandler(handler: (err: Error, instance: unknown, info: string) => void): (err: Error, instance: unknown, info: string) => void;
50
+ /**
51
+ * 处理 JS 运行时错误
52
+ * @param event - ErrorEvent 事件对象
53
+ */
54
+ private handleError;
55
+ /**
56
+ * 处理 Promise 未捕获异常
57
+ * @param event - PromiseRejectionEvent 事件对象
58
+ */
59
+ private handlePromiseReject;
60
+ /**
61
+ * 处理资源加载错误
62
+ * @param event - 事件对象
63
+ */
64
+ private handleResourceError;
65
+ /**
66
+ * 处理 Vue 组件错误
67
+ * @param err - Vue 错误对象
68
+ * @param instance - 触发错误的组件实例
69
+ * @param info - 错误信息字符串
70
+ */
71
+ private handleVueError;
72
+ }
@@ -0,0 +1,122 @@
1
+ import { App } from 'vue';
2
+ import { MonitorInstance, MonitorOptions } from './types';
3
+
4
+ /**
5
+ * 监控 SDK 主类
6
+ * 提供统一的初始化和 API 接口
7
+ */
8
+ declare class Monitor implements MonitorInstance {
9
+ /** 监控配置选项 */
10
+ private options;
11
+ /** 错误监控模块 */
12
+ private errorMonitor?;
13
+ /** 性能监控模块 */
14
+ private performanceMonitor?;
15
+ /** PV 监控模块 */
16
+ private pvMonitor?;
17
+ /** API 监控模块 */
18
+ private apiMonitor?;
19
+ /** 用户行为监控模块 */
20
+ private behaviorMonitor?;
21
+ /** 自定义事件监控模块 */
22
+ private customMonitor?;
23
+ /** 长任务监控模块 */
24
+ private longTaskMonitor?;
25
+ /** 上报管理模块 */
26
+ private reportManager?;
27
+ /** 当前用户 ID */
28
+ private userId?;
29
+ /** 是否已初始化 */
30
+ private isInited;
31
+ /**
32
+ * 初始化监控 SDK
33
+ * @param options - 监控配置选项
34
+ */
35
+ init(options: MonitorOptions): void;
36
+ /**
37
+ * 手动上报错误
38
+ * @param error - Error 对象
39
+ * @param errorType - 错误类型
40
+ */
41
+ reportError(error: Error, errorType?: 'js' | 'resource' | 'promise' | 'vue'): void;
42
+ /**
43
+ * 手动上报性能数据
44
+ * @param metric - 性能指标类型
45
+ * @param value - 指标值(毫秒)
46
+ */
47
+ reportPerformance(metric: 'page' | 'resource' | 'fcp' | 'fp' | 'lcp' | 'fid' | 'cls' | 'tti' | 'ttfb' | 'dns' | 'tcp' | 'download', value: number): void;
48
+ /**
49
+ * 手动上报页面访问(PV)
50
+ */
51
+ reportPv(): void;
52
+ /**
53
+ * 手动上报自定义事件
54
+ * @param eventName - 事件名称
55
+ * @param eventData - 事件数据
56
+ */
57
+ reportCustomEvent(eventName: string, eventData?: Record<string, unknown>): void;
58
+ /**
59
+ * 设置用户 ID
60
+ * 用于关联用户行为
61
+ * @param userId - 用户唯一标识
62
+ */
63
+ setUserId(userId: string): void;
64
+ /**
65
+ * 销毁监控实例
66
+ * 停止所有监控,释放资源
67
+ */
68
+ destroy(): void;
69
+ }
70
+ export declare const monitor: Monitor;
71
+ /**
72
+ * Vue 插件形式
73
+ * 可通过 app.use(MtnMonitor, options) 快速集成
74
+ */
75
+ export declare const MtnMonitor: {
76
+ /**
77
+ * 安装 Vue 插件
78
+ * @param app - Vue 应用实例
79
+ * @param options - 监控配置选项
80
+ */
81
+ install(app: App, options: MonitorOptions): void;
82
+ };
83
+ declare module '@vue/runtime-core' {
84
+ interface ComponentCustomProperties {
85
+ $monitor: MonitorInstance;
86
+ }
87
+ }
88
+ declare module 'vue' {
89
+ interface App {
90
+ $monitor: MonitorInstance;
91
+ }
92
+ }
93
+ export type {
94
+ /** 监控配置选项 */
95
+ MonitorOptions,
96
+ /** 监控实例接口 */
97
+ MonitorInstance,
98
+ /** 错误数据类型 */
99
+ ErrorData,
100
+ /** 性能数据类型 */
101
+ PerformanceData,
102
+ /** PV 数据类型 */
103
+ PvData,
104
+ /** API 数据类型 */
105
+ ApiData,
106
+ /** 用户行为数据类型 */
107
+ BehaviorData,
108
+ /** 自定义事件数据类型 */
109
+ CustomData,
110
+ /** 长任务数据类型 */
111
+ LongTaskData,
112
+ /** 监控数据联合类型 */
113
+ MonitorData, } from './types';
114
+ export { ErrorMonitor } from './error';
115
+ export { PerformanceMonitor } from './performance';
116
+ export { PvMonitor } from './pv';
117
+ export { ApiMonitor } from './api';
118
+ export { BehaviorMonitor } from './behavior';
119
+ export { CustomMonitor } from './custom';
120
+ export { LongTaskMonitor } from './longtask';
121
+ export { ReportManager } from './report';
122
+ export default monitor;
@@ -0,0 +1,42 @@
1
+ import { LongTaskData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 长任务监控类
5
+ * 使用 PerformanceObserver 监控长任务和内存使用
6
+ */
7
+ export declare class LongTaskMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /** PerformanceObserver 实例 */
13
+ private observer;
14
+ /** 是否已启动 */
15
+ private isStarted;
16
+ /** 内存信息轮询定时器 */
17
+ private memoryInterval;
18
+ /**
19
+ * 构造函数
20
+ * @param options - 监控配置
21
+ * @param reportFn - 上报函数
22
+ */
23
+ constructor(options: MonitorOptions, reportFn: (data: LongTaskData) => void);
24
+ /**
25
+ * 启动长任务监控
26
+ */
27
+ start(): void;
28
+ /**
29
+ * 停止长任务监控
30
+ */
31
+ stop(): void;
32
+ /**
33
+ * 监控 Long Task
34
+ * Long Task 是指执行时间超过 50ms 的任务
35
+ */
36
+ private observeLongTask;
37
+ /**
38
+ * 监控内存使用情况
39
+ * 兼容新版 Performance.memory 和旧版 performance.msMemoryUsageInfo
40
+ */
41
+ private observeMemory;
42
+ }
@@ -0,0 +1,64 @@
1
+ import { PerformanceData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 性能监控类
5
+ * 使用 Performance Observer API 采集关键性能指标
6
+ */
7
+ export declare class PerformanceMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /** PerformanceObserver 实例数组,用于停止时清理 */
13
+ private observers;
14
+ /** load 事件监听器引用 */
15
+ private loadHandler;
16
+ /** CLS 上报定时器引用 */
17
+ private clsTimeoutId;
18
+ /**
19
+ * 构造函数
20
+ * @param options - 监控配置
21
+ * @param reportFn - 上报函数
22
+ */
23
+ constructor(options: MonitorOptions, reportFn: (data: PerformanceData) => void);
24
+ /**
25
+ * 启动性能监控
26
+ * 采集各类性能指标
27
+ */
28
+ start(): void;
29
+ /**
30
+ * 停止性能监控
31
+ * 清理所有 observer 和定时器
32
+ */
33
+ stop(): void;
34
+ /**
35
+ * 上报页面基础性能指标
36
+ * 使用 performance.getEntriesByType('navigation') 获取页面加载各阶段耗时
37
+ */
38
+ private reportPagePerformance;
39
+ /**
40
+ * 监听资源加载性能
41
+ * 使用 PerformanceObserver 监听所有资源请求
42
+ */
43
+ private observeResources;
44
+ /**
45
+ * 监听 First Contentful Paint (FCP)
46
+ * 首次内容绘制时间
47
+ */
48
+ private observeFCP;
49
+ /**
50
+ * 监听 Largest Contentful Paint (LCP)
51
+ * 最大内容绘制时间,关键用户体验指标
52
+ */
53
+ private observeLCP;
54
+ /**
55
+ * 监听 Cumulative Layout Shift (CLS)
56
+ * 累计布局偏移,衡量页面稳定性
57
+ */
58
+ private observeCLS;
59
+ /**
60
+ * 监听 First Input Delay (FID)
61
+ * 首次输入延迟,衡量页面响应性
62
+ */
63
+ private observeFID;
64
+ }
@@ -0,0 +1,55 @@
1
+ import { PvData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 页面访问监控类
5
+ * 负责采集和上报页面访问数据
6
+ */
7
+ export declare class PvMonitor {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /** 数据上报函数 */
11
+ private reportFn;
12
+ /** 当前用户 ID */
13
+ private userId?;
14
+ /** 原始的 pushState 方法 */
15
+ private originalPushState;
16
+ /** 原始的 replaceState 方法 */
17
+ private originalReplaceState;
18
+ /** popstate 事件回调 */
19
+ private popstateHandler;
20
+ /** 是否已启动 */
21
+ private isStarted;
22
+ /**
23
+ * 构造函数
24
+ * @param options - 监控配置
25
+ * @param reportFn - 上报函数
26
+ */
27
+ constructor(options: MonitorOptions, reportFn: (data: PvData) => void);
28
+ /**
29
+ * 启动页面访问监控
30
+ * 首次访问立即上报,并监听路由变化
31
+ */
32
+ start(): void;
33
+ /**
34
+ * 停止页面访问监控
35
+ * 恢复原始方法,移除事件监听
36
+ */
37
+ stop(): void;
38
+ /**
39
+ * 手动触发一次 PV 上报
40
+ * 可用于路由切换后手动上报
41
+ */
42
+ report(): void;
43
+ /**
44
+ * 设置用户 ID
45
+ * @param userId - 用户唯一标识
46
+ */
47
+ setUserId(userId: string): void;
48
+ /**
49
+ * 监听路由变化
50
+ * 支持浏览器前进后退、pushState
51
+ * 注意:仅在 pushState 和 popstate 时上报,不在 replaceState 时上报。
52
+ * Vue Router 4 在一次 router.push() 中会先 replaceState 再 pushState,若 replaceState 也上报会导致同一次跳转产生两次 PV。
53
+ */
54
+ private observeRouter;
55
+ }
@@ -0,0 +1,31 @@
1
+ import { MonitorData, MonitorOptions } from './types';
2
+
3
+ /**
4
+ * 上报管理类
5
+ * 处理数据上报的核心逻辑
6
+ */
7
+ export declare class ReportManager {
8
+ /** 监控配置选项 */
9
+ private options;
10
+ /**
11
+ * 构造函数
12
+ * @param options - 监控配置
13
+ */
14
+ constructor(options: MonitorOptions);
15
+ /**
16
+ * 上报监控数据
17
+ * @param data - 监控数据
18
+ */
19
+ report(data: MonitorData): void;
20
+ /**
21
+ * 判断是否在采样范围内
22
+ * @returns 是否应该上报
23
+ */
24
+ private isSampled;
25
+ /**
26
+ * 默认上报实现
27
+ * 优先使用 sendBeacon,回退使用 fetch
28
+ * @param data - 监控数据
29
+ */
30
+ private defaultReport;
31
+ }