flutter.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {feedbackOnboardingCrashApiDart} from 'sentry/gettingStartedDocs/dart/dart';
  9. import {t, tct} from 'sentry/locale';
  10. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  11. type Params = DocsParams;
  12. const getInstallSnippet = (params: Params) => `
  13. dependencies:
  14. sentry_flutter: ^${getPackageVersion(params, 'sentry.dart.flutter', '7.8.0')}`;
  15. const getConfigureSnippet = (params: Params) => `
  16. import 'package:flutter/widgets.dart';
  17. import 'package:sentry_flutter/sentry_flutter.dart';
  18. Future<void> main() async {
  19. await SentryFlutter.init(
  20. (options) {
  21. options.dsn = '${params.dsn}';${
  22. params.isPerformanceSelected
  23. ? `
  24. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  25. // We recommend adjusting this value in production.
  26. options.tracesSampleRate = 1.0;`
  27. : ''
  28. }${
  29. params.isProfilingSelected
  30. ? `
  31. // The sampling rate for profiling is relative to tracesSampleRate
  32. // Setting to 1.0 will profile 100% of sampled transactions:
  33. options.profilesSampleRate = 1.0;`
  34. : ''
  35. }
  36. },
  37. appRunner: () => runApp(MyApp()),
  38. );
  39. // or define SENTRY_DSN via Dart environment variable (--dart-define)
  40. }`;
  41. const getVerifySnippet = () => `
  42. import 'package:sentry/sentry.dart';
  43. try {
  44. aMethodThatMightFail();
  45. } catch (exception, stackTrace) {
  46. await Sentry.captureException(
  47. exception,
  48. stackTrace: stackTrace,
  49. );
  50. }`;
  51. const getPerformanceSnippet = () => `
  52. import 'package:sentry/sentry.dart';
  53. import { getPackageVersion } from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  54. final transaction = Sentry.startTransaction('processOrderBatch()', 'task');
  55. try {
  56. await processOrderBatch(transaction);
  57. } catch (exception) {
  58. transaction.throwable = exception;
  59. transaction.status = SpanStatus.internalError();
  60. } finally {
  61. await transaction.finish();
  62. }
  63. Future<void> processOrderBatch(ISentrySpan span) async {
  64. // span operation: task, span description: operation
  65. final innerSpan = span.startChild('task', description: 'operation');
  66. try {
  67. // omitted code
  68. } catch (exception) {
  69. innerSpan.throwable = exception;
  70. innerSpan.status = SpanStatus.notFound();
  71. } finally {
  72. await innerSpan.finish();
  73. }
  74. }`;
  75. const onboarding: OnboardingConfig = {
  76. install: params => [
  77. {
  78. type: StepType.INSTALL,
  79. description: tct(
  80. 'Sentry captures data by using an SDK within your application’s runtime. Add the following to your [pubspec: pubspec.yaml]',
  81. {
  82. pubspec: <code />,
  83. }
  84. ),
  85. configurations: [
  86. {
  87. language: 'yml',
  88. partialLoading: params.sourcePackageRegistries?.isLoading,
  89. code: getInstallSnippet(params),
  90. },
  91. ],
  92. },
  93. ],
  94. configure: params => [
  95. {
  96. type: StepType.CONFIGURE,
  97. description: tct('Import [sentryFlutter: sentry_flutter] and initialize it', {
  98. sentryFlutter: <code />,
  99. }),
  100. configurations: [
  101. ...(params.isProfilingSelected
  102. ? [
  103. {
  104. description: t(
  105. 'Flutter Profiling alpha is available for iOS and macOS since SDK version 7.12.0.'
  106. ),
  107. },
  108. ]
  109. : []),
  110. {
  111. language: 'dart',
  112. code: getConfigureSnippet(params),
  113. additionalInfo: tct(
  114. 'You can configure the [sentryDsn: SENTRY_DSN], [sentryRelease: SENTRY_RELEASE], [sentryDist: SENTRY_DIST], and [sentryEnv: SENTRY_ENVIRONMENT] via the Dart environment variables passing the [dartDefine: --dart-define] flag to the compiler, as noted in the code sample.',
  115. {
  116. sentryDsn: <code />,
  117. sentryRelease: <code />,
  118. sentryDist: <code />,
  119. sentryEnv: <code />,
  120. dartDefine: <code />,
  121. }
  122. ),
  123. },
  124. ],
  125. },
  126. ],
  127. verify: (params: Params) => [
  128. {
  129. type: StepType.VERIFY,
  130. description: t(
  131. 'Create an intentional error, so you can test that everything is working:'
  132. ),
  133. configurations: [
  134. {
  135. language: 'dart',
  136. code: getVerifySnippet(),
  137. additionalInfo: tct(
  138. "If you're new to Sentry, use the email alert to access your account and complete a product tour.[break] If you're an existing user and have disabled alerts, you won't receive this email.",
  139. {
  140. break: <br />,
  141. }
  142. ),
  143. },
  144. ],
  145. },
  146. ...(params.isPerformanceSelected
  147. ? [
  148. {
  149. title: t('Performance'),
  150. description: t(
  151. "You'll be able to monitor the performance of your app using the SDK. For example:"
  152. ),
  153. configurations: [
  154. {
  155. language: 'dart',
  156. code: getPerformanceSnippet(),
  157. additionalInfo: tct(
  158. 'To learn more about the API and automatic instrumentations, check out the [perfDocs: performance documentation].',
  159. {
  160. perfDocs: (
  161. <ExternalLink href="https://docs.sentry.io/platforms/flutter/performance/instrumentation/" />
  162. ),
  163. }
  164. ),
  165. },
  166. ],
  167. },
  168. ]
  169. : []),
  170. ],
  171. nextSteps: () => [
  172. {
  173. name: t('Debug Symbols'),
  174. description: t(
  175. 'We offer a range of methods to provide Sentry with debug symbols so that you can see symbolicated stack traces and triage issues faster.'
  176. ),
  177. link: 'https://docs.sentry.io/platforms/flutter/upload-debug/',
  178. },
  179. {
  180. name: t('Source Context'),
  181. description: t(
  182. "If Sentry has access to your application's source code, it can show snippets of code source context around the location of stack frames, which helps to quickly pinpoint problematic code."
  183. ),
  184. link: 'https://docs.sentry.io/platforms/flutter/upload-debug/#uploading-source-context-for-flutter-android-ios-and-macos',
  185. },
  186. ],
  187. };
  188. const docs: Docs = {
  189. onboarding,
  190. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiDart,
  191. crashReportOnboarding: feedbackOnboardingCrashApiDart,
  192. };
  193. export default docs;