flutter.tsx 10.0 KB

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