Flutter集成Sentry Dio无法自动采集HTTP请求追踪数据问题
问题:Flutter集成Sentry Dio后无法自动采集HTTP请求追踪数据
按照Sentry官方文档在Flutter应用中集成Sentry与Dio,能正常接收错误报告和自定义性能追踪数据,但无法通过Dio自动采集HTTP请求追踪数据。操作步骤如下:
- 创建Flutter应用
- 在
pubspec.yaml中添加依赖:
sentry_flutter: ^7.10.1 sentry_dio: ^7.10.1 dio: ^5.3.3
- 在
main.dart中初始化Sentry:
import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:sentry_dio/sentry_dio.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; Future<void> main() async { await SentryFlutter.init( (options) { options.dsn = '<my dsn>'; options.tracesSampleRate = 1.0; }, appRunner: () => runApp(MyApp()), ); }
- 在MyApp中添加按钮发送HTTP GET请求:
FilledButton( onPressed: () async { final dio = Dio(); dio.addSentry(); final response = await dio.get('https://dart.dev'); print(response); }, child: const Text('TEST PERFORMANCE'), ),
预期上述代码可自动采集HTTP请求的追踪数据,但未达到预期效果。
问题解决(2023/10/02 更新)
此问题源于对文档的误解:自动采集对应的是Span而非Transaction。Span不会单独在性能仪表板中列出,需在问题或Transaction的面包屑中查看。
正确的示例代码如下:
FilledButton( onPressed: () async { final transaction = Sentry.startTransaction('POST /post_ok', 'http'); try { final dio = Dio(); dio.addSentry(); final response = await dio.post('http://localhost:3000/post_ok'); print(response); } catch (e) { transaction.throwable = e; transaction.status = SpanStatus.internalError(); } transaction.finish(); }, child: const Text('TEST PERFORMANCE'), ),
执行结果:
POST http://localhost/post_ok [200] { duration: 0:00:00.041514, reason: OK, response_body_size: 63 }
目前功能已正常运行。
内容的提问来源于stack exchange,提问作者Ethan Guo
相关产品推荐
相关产品推荐

