sentry-compatibility-test.html
27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sentry 兼容性测试</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
.success { color: green; }
.error { color: red; }
.info { color: blue; }
.warning { color: orange; }
button { padding: 10px 15px; margin: 5px; cursor: pointer; }
pre { background: #f5f5f5; padding: 10px; border-radius: 3px; overflow-x: auto; }
.result { margin: 10px 0; padding: 10px; background: #f9f9f9; border-radius: 3px; }
.sentry-event { background: #fff3cd; border: 1px solid #ffeaa7; padding: 10px; margin: 5px 0; border-radius: 3px; }
</style>
</head>
<body>
<h1>Sentry 错误上报兼容性测试</h1>
<div class="test-section">
<h2>1. Sentry 初始化测试</h2>
<button onclick="testSentryInit()">测试 Sentry 初始化</button>
<div id="sentry-init-results"></div>
</div>
<div class="test-section">
<h2>2. 小程序环境 Sentry 测试</h2>
<button onclick="testMiniappSentry()">测试小程序 Sentry</button>
<div id="miniapp-sentry-results"></div>
</div>
<div class="test-section">
<h2>3. UniApp 环境 Sentry 测试</h2>
<button onclick="testUniappSentry()">测试 UniApp Sentry</button>
<div id="uniapp-sentry-results"></div>
</div>
<div class="test-section">
<h2>4. 错误捕获和上报测试</h2>
<button onclick="testErrorCapture()">测试错误捕获</button>
<div id="error-capture-results"></div>
</div>
<div class="test-section">
<h2>5. 性能监控测试</h2>
<button onclick="testPerformanceMonitoring()">测试性能监控</button>
<div id="performance-monitoring-results"></div>
</div>
<div class="test-section">
<h2>6. 自定义事件测试</h2>
<button onclick="testCustomEvents()">测试自定义事件</button>
<div id="custom-events-results"></div>
</div>
<!-- 加载 UMD 格式的库 -->
<script src="../dist/index.umd.js"></script>
<script>
// 模拟 Sentry SDK
window.Sentry = {
init: function(options) {
console.log('Sentry.init called with:', options);
this._options = options;
this._isInitialized = true;
this._events = [];
return this;
},
captureException: function(error, context) {
console.log('Sentry.captureException:', error, context);
const event = {
type: 'exception',
error: error,
context: context,
timestamp: Date.now(),
environment: this._options?.environment || 'unknown'
};
this._events.push(event);
return event;
},
captureMessage: function(message, level, context) {
console.log('Sentry.captureMessage:', message, level, context);
const event = {
type: 'message',
message: message,
level: level || 'info',
context: context,
timestamp: Date.now(),
environment: this._options?.environment || 'unknown'
};
this._events.push(event);
return event;
},
addBreadcrumb: function(breadcrumb) {
console.log('Sentry.addBreadcrumb:', breadcrumb);
if (!this._breadcrumbs) this._breadcrumbs = [];
this._breadcrumbs.push({
...breadcrumb,
timestamp: Date.now()
});
},
setContext: function(key, context) {
console.log('Sentry.setContext:', key, context);
if (!this._contexts) this._contexts = {};
this._contexts[key] = context;
},
setTag: function(key, value) {
console.log('Sentry.setTag:', key, value);
if (!this._tags) this._tags = {};
this._tags[key] = value;
},
setUser: function(user) {
console.log('Sentry.setUser:', user);
this._user = user;
},
startTransaction: function(context) {
console.log('Sentry.startTransaction:', context);
return {
name: context.name,
op: context.op,
startTime: Date.now(),
finish: function() {
this.endTime = Date.now();
console.log('Transaction finished:', this);
},
setTag: function(key, value) {
if (!this.tags) this.tags = {};
this.tags[key] = value;
}
};
},
// 获取捕获的事件(测试用)
getEvents: function() {
return this._events || [];
},
getBreadcrumbs: function() {
return this._breadcrumbs || [];
},
getContexts: function() {
return this._contexts || {};
},
getTags: function() {
return this._tags || {};
},
isInitialized: function() {
return this._isInitialized || false;
}
};
function logResult(containerId, message, type = 'info') {
const container = document.getElementById(containerId);
const div = document.createElement('div');
div.className = `result ${type}`;
div.innerHTML = message;
container.appendChild(div);
console.log(`[${type.toUpperCase()}] ${message}`);
}
function clearResults(containerId) {
document.getElementById(containerId).innerHTML = '';
}
function showSentryEvent(containerId, event) {
const container = document.getElementById(containerId);
const div = document.createElement('div');
div.className = 'sentry-event';
div.innerHTML = `
<strong>Sentry Event:</strong><br>
Type: ${event.type}<br>
${event.error ? `Error: ${event.error.message}<br>` : ''}
${event.message ? `Message: ${event.message}<br>` : ''}
Environment: ${event.environment}<br>
Timestamp: ${new Date(event.timestamp).toLocaleString()}
`;
container.appendChild(div);
}
// 1. Sentry 初始化测试
function testSentryInit() {
clearResults('sentry-init-results');
try {
// 基础初始化
Sentry.init({
dsn: 'https://test@sentry.io/test',
environment: 'test',
debug: true
});
logResult('sentry-init-results', '✅ Sentry 基础初始化成功', 'success');
logResult('sentry-init-results', `初始化状态: ${Sentry.isInitialized()}`, 'info');
// 小程序环境初始化
Sentry.init({
dsn: 'https://miniapp@sentry.io/miniapp',
environment: 'miniapp',
integrations: [],
beforeSend: function(event) {
console.log('小程序 beforeSend:', event);
return event;
}
});
logResult('sentry-init-results', '✅ 小程序环境 Sentry 初始化成功', 'success');
// UniApp 环境初始化
Sentry.init({
dsn: 'https://uniapp@sentry.io/uniapp',
environment: 'uniapp',
tracesSampleRate: 1.0,
beforeSend: function(event) {
console.log('UniApp beforeSend:', event);
return event;
}
});
logResult('sentry-init-results', '✅ UniApp 环境 Sentry 初始化成功', 'success');
} catch (error) {
logResult('sentry-init-results', `❌ Sentry 初始化失败: ${error.message}`, 'error');
}
}
// 2. 小程序环境 Sentry 测试
function testMiniappSentry() {
clearResults('miniapp-sentry-results');
try {
// 模拟小程序环境
window.wx = {
request: function(options) {
console.log('wx.request for Sentry:', options);
// 模拟网络错误
setTimeout(() => {
if (options.fail) {
options.fail({
errMsg: '模拟网络错误',
statusCode: 500
});
}
}, 100);
},
getSystemInfo: function(options) {
if (options.success) {
options.success({
platform: 'miniapp',
system: 'iOS 14.0',
version: '8.0.0',
SDKVersion: '2.19.0'
});
}
}
};
const { Request } = window.GlskFetch;
// 初始化小程序 Sentry
Sentry.init({
dsn: 'https://miniapp@sentry.io/miniapp',
environment: 'miniapp'
});
// 设置小程序上下文
Sentry.setContext('miniapp', {
platform: 'miniapp',
version: '1.0.0'
});
Sentry.setTag('platform', 'miniapp');
Sentry.setUser({
id: 'miniapp-user-123',
platform: 'miniapp'
});
logResult('miniapp-sentry-results', '✅ 小程序 Sentry 上下文设置成功', 'success');
// 创建小程序请求实例
const miniappRequest = new Request({
baseURL: 'https://api.miniapp.com',
timeout: 5000
});
// 模拟小程序错误
try {
throw new Error('小程序测试错误');
} catch (error) {
const event = Sentry.captureException(error, {
tags: { source: 'miniapp-test' },
extra: { requestInstance: 'miniapp' }
});
showSentryEvent('miniapp-sentry-results', event);
}
// 添加面包屑
Sentry.addBreadcrumb({
message: '小程序页面加载',
category: 'navigation',
level: 'info'
});
Sentry.addBreadcrumb({
message: '小程序请求发送',
category: 'http',
level: 'info',
data: {
url: 'https://api.miniapp.com/test',
method: 'GET'
}
});
logResult('miniapp-sentry-results', '✅ 小程序面包屑添加成功', 'success');
logResult('miniapp-sentry-results', `面包屑数量: ${Sentry.getBreadcrumbs().length}`, 'info');
// 测试小程序特定错误
wx.request({
url: 'https://api.miniapp.com/error',
method: 'GET',
success: function(res) {
console.log('小程序请求成功:', res);
},
fail: function(err) {
const event = Sentry.captureException(new Error(`小程序请求失败: ${err.errMsg}`), {
tags: { source: 'wx.request' },
extra: { error: err }
});
showSentryEvent('miniapp-sentry-results', event);
}
});
logResult('miniapp-sentry-results', '✅ 小程序 Sentry 测试完成', 'success');
} catch (error) {
logResult('miniapp-sentry-results', `❌ 小程序 Sentry 测试失败: ${error.message}`, 'error');
}
}
// 3. UniApp 环境 Sentry 测试
function testUniappSentry() {
clearResults('uniapp-sentry-results');
try {
// 模拟 UniApp 环境
window.uni = {
request: function(options) {
console.log('uni.request for Sentry:', options);
return new Promise((resolve, reject) => {
setTimeout(() => {
// 模拟随机错误
if (Math.random() > 0.7) {
reject({
errMsg: '模拟 UniApp 网络错误',
statusCode: 500
});
} else {
resolve({
data: { message: 'UniApp 请求成功' },
statusCode: 200
});
}
}, 200);
});
},
getSystemInfo: function() {
return {
platform: 'uniapp',
system: 'Android 11',
uniPlatform: 'app-plus',
uniVersion: '3.0.0'
};
}
};
const { Request } = window.GlskFetch;
// 初始化 UniApp Sentry
Sentry.init({
dsn: 'https://uniapp@sentry.io/uniapp',
environment: 'uniapp',
tracesSampleRate: 1.0
});
// 设置 UniApp 上下文
const systemInfo = uni.getSystemInfo();
Sentry.setContext('uniapp', systemInfo);
Sentry.setTag('platform', 'uniapp');
Sentry.setTag('uniPlatform', systemInfo.uniPlatform);
Sentry.setUser({
id: 'uniapp-user-456',
platform: 'uniapp'
});
logResult('uniapp-sentry-results', '✅ UniApp Sentry 上下文设置成功', 'success');
// 创建 UniApp 请求实例
const uniappRequest = new Request({
baseURL: 'https://api.uniapp.com',
timeout: 8000
});
// 开始性能监控
const transaction = Sentry.startTransaction({
name: 'UniApp 页面加载',
op: 'navigation'
});
transaction.setTag('page', 'test-page');
// 模拟 UniApp 异步操作
async function testUniappAsync() {
try {
Sentry.addBreadcrumb({
message: 'UniApp 异步操作开始',
category: 'async',
level: 'info'
});
const response = await uni.request({
url: 'https://api.uniapp.com/test',
method: 'POST',
data: { test: 'data' }
});
logResult('uniapp-sentry-results', '✅ UniApp 异步请求成功', 'success');
Sentry.addBreadcrumb({
message: 'UniApp 异步操作成功',
category: 'async',
level: 'info',
data: response
});
} catch (error) {
const event = Sentry.captureException(new Error(`UniApp 异步操作失败: ${error.errMsg}`), {
tags: { source: 'uni.request', async: true },
extra: { error: error }
});
showSentryEvent('uniapp-sentry-results', event);
} finally {
transaction.finish();
logResult('uniapp-sentry-results', '✅ UniApp 性能监控完成', 'success');
}
}
// 运行异步测试
testUniappAsync();
// 测试 UniApp 特定错误
try {
throw new Error('UniApp 测试错误');
} catch (error) {
const event = Sentry.captureException(error, {
tags: { source: 'uniapp-test' },
extra: {
systemInfo: systemInfo,
requestInstance: 'uniapp'
}
});
showSentryEvent('uniapp-sentry-results', event);
}
logResult('uniapp-sentry-results', '✅ UniApp Sentry 测试完成', 'success');
} catch (error) {
logResult('uniapp-sentry-results', `❌ UniApp Sentry 测试失败: ${error.message}`, 'error');
}
}
// 4. 错误捕获和上报测试
function testErrorCapture() {
clearResults('error-capture-results');
try {
const { Request, generateSignature } = window.GlskFetch;
// 测试不同类型的错误
// 1. JavaScript 错误
try {
undefined.someMethod();
} catch (error) {
const event = Sentry.captureException(error, {
tags: { errorType: 'javascript' }
});
showSentryEvent('error-capture-results', event);
}
// 2. 请求配置错误
try {
new Request(null);
} catch (error) {
const event = Sentry.captureException(error, {
tags: { errorType: 'request-config' }
});
showSentryEvent('error-capture-results', event);
}
// 3. 签名错误
try {
generateSignature({});
} catch (error) {
const event = Sentry.captureException(error, {
tags: { errorType: 'signature' }
});
showSentryEvent('error-capture-results', event);
}
// 4. 自定义错误消息
Sentry.captureMessage('这是一个测试消息', 'info', {
tags: { messageType: 'test' }
});
Sentry.captureMessage('这是一个警告消息', 'warning', {
tags: { messageType: 'warning' }
});
logResult('error-capture-results', '✅ 错误捕获测试完成', 'success');
logResult('error-capture-results', `捕获事件数量: ${Sentry.getEvents().length}`, 'info');
} catch (error) {
logResult('error-capture-results', `❌ 错误捕获测试失败: ${error.message}`, 'error');
}
}
// 5. 性能监控测试
function testPerformanceMonitoring() {
clearResults('performance-monitoring-results');
try {
const { Request, generateSignature } = window.GlskFetch;
// 测试请求性能监控
const requestTransaction = Sentry.startTransaction({
name: 'Request 实例化性能',
op: 'request.create'
});
const startTime = performance.now();
for (let i = 0; i < 100; i++) {
new Request({
baseURL: `https://api${i}.example.com`,
timeout: 5000
});
}
const endTime = performance.now();
const duration = endTime - startTime;
requestTransaction.setTag('count', '100');
requestTransaction.setTag('duration', duration.toFixed(2));
requestTransaction.finish();
logResult('performance-monitoring-results', `✅ 请求实例化性能监控完成: ${duration.toFixed(2)}ms`, 'success');
// 测试签名性能监控
const signatureTransaction = Sentry.startTransaction({
name: '签名生成性能',
op: 'signature.generate'
});
const signatureStartTime = performance.now();
for (let i = 0; i < 100; i++) {
generateSignature({
appId: `test-${i}`,
appSecret: 'test-secret',
params: { data: `test-data-${i}` }
});
}
const signatureEndTime = performance.now();
const signatureDuration = signatureEndTime - signatureStartTime;
signatureTransaction.setTag('count', '100');
signatureTransaction.setTag('duration', signatureDuration.toFixed(2));
signatureTransaction.finish();
logResult('performance-monitoring-results', `✅ 签名生成性能监控完成: ${signatureDuration.toFixed(2)}ms`, 'success');
// 添加性能相关面包屑
Sentry.addBreadcrumb({
message: '性能监控测试完成',
category: 'performance',
level: 'info',
data: {
requestDuration: duration,
signatureDuration: signatureDuration
}
});
logResult('performance-monitoring-results', '✅ 性能监控测试完成', 'success');
} catch (error) {
logResult('performance-monitoring-results', `❌ 性能监控测试失败: ${error.message}`, 'error');
}
}
// 6. 自定义事件测试
function testCustomEvents() {
clearResults('custom-events-results');
try {
const { Request } = window.GlskFetch;
// 自定义业务事件
Sentry.captureMessage('用户登录', 'info', {
tags: {
event: 'user_login',
platform: 'web'
},
extra: {
userId: 'test-user-123',
timestamp: Date.now()
}
});
Sentry.captureMessage('API 调用', 'info', {
tags: {
event: 'api_call',
endpoint: '/api/test'
},
extra: {
method: 'POST',
duration: 150
}
});
// 业务逻辑错误
Sentry.captureMessage('业务逻辑错误', 'error', {
tags: {
event: 'business_error',
errorCode: 'BIZ_001'
},
extra: {
description: '用户权限不足',
action: 'access_denied'
}
});
// 添加业务相关面包屑
Sentry.addBreadcrumb({
message: '用户点击按钮',
category: 'ui',
level: 'info',
data: {
buttonId: 'test-button',
page: 'sentry-test'
}
});
Sentry.addBreadcrumb({
message: '数据加载完成',
category: 'data',
level: 'info',
data: {
dataType: 'test-data',
count: 100
}
});
logResult('custom-events-results', '✅ 自定义事件测试完成', 'success');
logResult('custom-events-results', `总事件数量: ${Sentry.getEvents().length}`, 'info');
logResult('custom-events-results', `面包屑数量: ${Sentry.getBreadcrumbs().length}`, 'info');
logResult('custom-events-results', `上下文数量: ${Object.keys(Sentry.getContexts()).length}`, 'info');
logResult('custom-events-results', `标签数量: ${Object.keys(Sentry.getTags()).length}`, 'info');
} catch (error) {
logResult('custom-events-results', `❌ 自定义事件测试失败: ${error.message}`, 'error');
}
}
// 页面加载完成后自动初始化
window.addEventListener('load', function() {
console.log('=== Sentry 兼容性测试页面加载完成 ===');
testSentryInit();
});
</script>
</body>
</html>