flutter.tsx 10 KB

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