flutter.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import Link from 'sentry/components/links/link';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import type {
  5. Docs,
  6. DocsParams,
  7. OnboardingConfig,
  8. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  9. import {feedbackOnboardingCrashApiDart} from 'sentry/gettingStartedDocs/dart/dart';
  10. import {t, tct} from 'sentry/locale';
  11. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  12. type Params = DocsParams;
  13. const getInstallSnippet = (params: Params) => `
  14. dependencies:
  15. sentry_flutter: ^${getPackageVersion(params, 'sentry.dart.flutter', '7.8.0')}`;
  16. const getConfigureSnippet = (params: Params) => `
  17. import 'package:flutter/widgets.dart';
  18. import 'package:sentry_flutter/sentry_flutter.dart';
  19. Future<void> main() async {
  20. await SentryFlutter.init(
  21. (options) {
  22. options.dsn = '${params.dsn}';${
  23. params.isPerformanceSelected
  24. ? `
  25. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  26. // We recommend adjusting this value in production.
  27. options.tracesSampleRate = 1.0;`
  28. : ''
  29. }${
  30. params.isProfilingSelected
  31. ? `
  32. // The sampling rate for profiling is relative to tracesSampleRate
  33. // Setting to 1.0 will profile 100% of sampled transactions:
  34. options.profilesSampleRate = 1.0;`
  35. : ''
  36. }
  37. },
  38. appRunner: () => runApp(MyApp()),
  39. );
  40. // or define SENTRY_DSN via Dart environment variable (--dart-define)
  41. }`;
  42. const getVerifySnippet = () => `
  43. import 'package:sentry/sentry.dart';
  44. try {
  45. aMethodThatMightFail();
  46. } catch (exception, stackTrace) {
  47. await Sentry.captureException(
  48. exception,
  49. stackTrace: stackTrace,
  50. );
  51. }`;
  52. const getPerformanceSnippet = () => `
  53. import 'package:sentry/sentry.dart';
  54. import { getPackageVersion } from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  55. final transaction = Sentry.startTransaction('processOrderBatch()', 'task');
  56. try {
  57. await processOrderBatch(transaction);
  58. } catch (exception) {
  59. transaction.throwable = exception;
  60. transaction.status = SpanStatus.internalError();
  61. } finally {
  62. await transaction.finish();
  63. }
  64. Future<void> processOrderBatch(ISentrySpan span) async {
  65. // span operation: task, span description: operation
  66. final innerSpan = span.startChild('task', description: 'operation');
  67. try {
  68. // omitted code
  69. } catch (exception) {
  70. innerSpan.throwable = exception;
  71. innerSpan.status = SpanStatus.notFound();
  72. } finally {
  73. await innerSpan.finish();
  74. }
  75. }`;
  76. const getConfigureMetricsSnippet = (params: Params) => `
  77. import 'package:sentry_flutter/sentry_flutter.dart';
  78. Future<void> main() async {
  79. await SentryFlutter.init(
  80. (options) {
  81. options.dsn = '${params.dsn}';
  82. options.enableMetrics = true;
  83. },
  84. appRunner: initApp, // Init your App.
  85. );
  86. };`;
  87. const metricsOnboarding: OnboardingConfig = {
  88. install: (params: DocsParams) => [
  89. {
  90. type: StepType.INSTALL,
  91. description: tct(
  92. 'You need Sentry Flutter SDK version [codeVersion:7.19.0] or higher. Learn more about installation methods in our [docsLink:full documentation].',
  93. {
  94. package: <code />,
  95. codeVersion: <code />,
  96. docsLink: <Link to={`/projects/${params.projectSlug}/getting-started/`} />,
  97. }
  98. ),
  99. configurations: [
  100. {
  101. language: 'yml',
  102. partialLoading: params.sourcePackageRegistries?.isLoading,
  103. code: getInstallSnippet(params),
  104. },
  105. ],
  106. },
  107. ],
  108. configure: (params: DocsParams) => [
  109. {
  110. type: StepType.CONFIGURE,
  111. description: t(
  112. 'To enable capturing metrics, you need to enable the metrics feature.'
  113. ),
  114. configurations: [
  115. {
  116. code: [
  117. {
  118. label: 'Dart',
  119. value: 'dart',
  120. language: 'dart',
  121. code: getConfigureMetricsSnippet(params),
  122. },
  123. ],
  124. },
  125. ],
  126. },
  127. ],
  128. verify: () => [
  129. {
  130. type: StepType.VERIFY,
  131. description: tct(
  132. "Then you'll be able to add metrics as [codeCounters:counters], [codeSets:sets], [codeDistribution:distributions], and [codeGauge:gauges]. These are available under the [codeNamespace:Sentry.metrics()] namespace. Try out this example:",
  133. {
  134. codeCounters: <code />,
  135. codeSets: <code />,
  136. codeDistribution: <code />,
  137. codeGauge: <code />,
  138. codeNamespace: <code />,
  139. }
  140. ),
  141. configurations: [
  142. {
  143. configurations: [
  144. {
  145. code: [
  146. {
  147. label: 'Dart',
  148. value: 'dart',
  149. language: 'dart',
  150. code: `
  151. // Add 4 to a counter named "hits"
  152. Sentry.metrics().increment("hits", value: 4);`,
  153. },
  154. ],
  155. },
  156. ],
  157. },
  158. {
  159. description: t(
  160. 'With a bit of delay you can see the data appear in the Sentry UI.'
  161. ),
  162. },
  163. {
  164. description: tct(
  165. 'Learn more about metrics and how to configure them, by reading the [docsLink:docs].',
  166. {
  167. docsLink: (
  168. <ExternalLink href="https://docs.sentry.io/platforms/flutter/metrics/" />
  169. ),
  170. }
  171. ),
  172. },
  173. ],
  174. },
  175. ],
  176. };
  177. const onboarding: OnboardingConfig = {
  178. install: params => [
  179. {
  180. type: StepType.INSTALL,
  181. description: tct(
  182. 'Sentry captures data by using an SDK within your application’s runtime. Add the following to your [pubspec: pubspec.yaml]',
  183. {
  184. pubspec: <code />,
  185. }
  186. ),
  187. configurations: [
  188. {
  189. language: 'yml',
  190. partialLoading: params.sourcePackageRegistries?.isLoading,
  191. code: getInstallSnippet(params),
  192. },
  193. ],
  194. },
  195. ],
  196. configure: params => [
  197. {
  198. type: StepType.CONFIGURE,
  199. description: tct('Import [sentryFlutter: sentry_flutter] and initialize it', {
  200. sentryFlutter: <code />,
  201. }),
  202. configurations: [
  203. ...(params.isProfilingSelected
  204. ? [
  205. {
  206. description: t(
  207. 'Flutter Profiling alpha is available for iOS and macOS since SDK version 7.12.0.'
  208. ),
  209. },
  210. ]
  211. : []),
  212. {
  213. language: 'dart',
  214. code: getConfigureSnippet(params),
  215. additionalInfo: tct(
  216. '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.',
  217. {
  218. sentryDsn: <code />,
  219. sentryRelease: <code />,
  220. sentryDist: <code />,
  221. sentryEnv: <code />,
  222. dartDefine: <code />,
  223. }
  224. ),
  225. },
  226. ],
  227. },
  228. ],
  229. verify: (params: Params) => [
  230. {
  231. type: StepType.VERIFY,
  232. description: t(
  233. 'Create an intentional error, so you can test that everything is working:'
  234. ),
  235. configurations: [
  236. {
  237. language: 'dart',
  238. code: getVerifySnippet(),
  239. additionalInfo: tct(
  240. "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.",
  241. {
  242. break: <br />,
  243. }
  244. ),
  245. },
  246. ],
  247. },
  248. ...(params.isPerformanceSelected
  249. ? [
  250. {
  251. title: t('Performance'),
  252. description: t(
  253. "You'll be able to monitor the performance of your app using the SDK. For example:"
  254. ),
  255. configurations: [
  256. {
  257. language: 'dart',
  258. code: getPerformanceSnippet(),
  259. additionalInfo: tct(
  260. 'To learn more about the API and automatic instrumentations, check out the [perfDocs: performance documentation].',
  261. {
  262. perfDocs: (
  263. <ExternalLink href="https://docs.sentry.io/platforms/flutter/performance/instrumentation/" />
  264. ),
  265. }
  266. ),
  267. },
  268. ],
  269. },
  270. ]
  271. : []),
  272. ],
  273. nextSteps: () => [
  274. {
  275. name: t('Debug Symbols'),
  276. description: t(
  277. 'We offer a range of methods to provide Sentry with debug symbols so that you can see symbolicated stack traces and triage issues faster.'
  278. ),
  279. link: 'https://docs.sentry.io/platforms/flutter/upload-debug/',
  280. },
  281. {
  282. name: t('Source Context'),
  283. description: t(
  284. "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."
  285. ),
  286. link: 'https://docs.sentry.io/platforms/flutter/upload-debug/#uploading-source-context-for-flutter-android-ios-and-macos',
  287. },
  288. ],
  289. };
  290. const docs: Docs = {
  291. onboarding,
  292. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiDart,
  293. crashReportOnboarding: feedbackOnboardingCrashApiDart,
  294. customMetricsOnboarding: metricsOnboarding,
  295. };
  296. export default docs;