dart.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 {
  10. getCrashReportApiIntroduction,
  11. getCrashReportInstallDescription,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  13. import exampleSnippets from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsExampleSnippets';
  14. import {metricTagsExplanation} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  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: ^${getPackageVersion(params, 'sentry.dart', '7.8.0')}`;
  21. const getConfigureSnippet = (params: Params) => `
  22. import 'package:sentry/sentry.dart';
  23. Future<void> main() async {
  24. await Sentry.init((options) {
  25. options.dsn = '${params.dsn.public}';
  26. // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
  27. // We recommend adjusting this value in production.
  28. options.tracesSampleRate = 1.0;
  29. });
  30. // or define SENTRY_DSN via Dart environment variable (--dart-define)
  31. }`;
  32. const getVerifySnippet = () => `
  33. import 'package:sentry/sentry.dart';
  34. try {
  35. aMethodThatMightFail();
  36. } catch (exception, stackTrace) {
  37. await Sentry.captureException(
  38. exception,
  39. stackTrace: stackTrace,
  40. );
  41. }`;
  42. const getPerfomanceSnippet = () => `
  43. import 'package:sentry/sentry.dart';
  44. import { getPackageVersion } from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  45. final transaction = Sentry.startTransaction('processOrderBatch()', 'task');
  46. try {
  47. await processOrderBatch(transaction);
  48. } catch (exception) {
  49. transaction.throwable = exception;
  50. transaction.status = SpanStatus.internalError();
  51. } finally {
  52. await transaction.finish();
  53. }
  54. Future<void> processOrderBatch(ISentrySpan span) async {
  55. // span operation: task, span description: operation
  56. final innerSpan = span.startChild('task', description: 'operation');
  57. try {
  58. // omitted code
  59. } catch (exception) {
  60. innerSpan.throwable = exception;
  61. innerSpan.status = SpanStatus.notFound();
  62. } finally {
  63. await innerSpan.finish();
  64. }
  65. }`;
  66. const getConfigureMetricsSnippet = (params: Params) => `
  67. import 'package:sentry/sentry.dart';
  68. Future<void> main() async {
  69. await Sentry.init((options) {
  70. options.dsn = '${params.dsn.public}';
  71. options.enableMetrics = true;
  72. },
  73. );`;
  74. const metricsOnboarding: OnboardingConfig = {
  75. install: (params: DocsParams) => [
  76. {
  77. type: StepType.INSTALL,
  78. description: tct(
  79. 'You need Sentry Dart SDK version [code:7.19.0] or higher. Learn more about installation methods in our [docsLink:full documentation].',
  80. {
  81. code: <code />,
  82. docsLink: <Link to={`/projects/${params.projectSlug}/getting-started/`} />,
  83. }
  84. ),
  85. configurations: [
  86. {
  87. language: 'yml',
  88. partialLoading: params.sourcePackageRegistries?.isLoading,
  89. code: getInstallSnippet(params),
  90. },
  91. ],
  92. },
  93. ],
  94. configure: (params: DocsParams) => [
  95. {
  96. type: StepType.CONFIGURE,
  97. description: t(
  98. 'To enable capturing metrics, you need to enable the metrics feature.'
  99. ),
  100. configurations: [
  101. {
  102. code: [
  103. {
  104. label: 'Dart',
  105. value: 'dart',
  106. language: 'dart',
  107. code: getConfigureMetricsSnippet(params),
  108. },
  109. ],
  110. },
  111. ],
  112. },
  113. ],
  114. verify: () => [
  115. {
  116. type: StepType.VERIFY,
  117. description: tct(
  118. "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.",
  119. {
  120. code: <code />,
  121. }
  122. ),
  123. configurations: [
  124. {
  125. description: metricTagsExplanation,
  126. },
  127. {
  128. description: t('Try out these examples:'),
  129. code: [
  130. {
  131. label: 'Counter',
  132. value: 'counter',
  133. language: 'dart',
  134. code: exampleSnippets.dart.counter,
  135. },
  136. {
  137. label: 'Distribution',
  138. value: 'distribution',
  139. language: 'dart',
  140. code: exampleSnippets.dart.distribution,
  141. },
  142. {
  143. label: 'Set',
  144. value: 'set',
  145. language: 'dart',
  146. code: exampleSnippets.dart.set,
  147. },
  148. {
  149. label: 'Gauge',
  150. value: 'gauge',
  151. language: 'dart',
  152. code: exampleSnippets.dart.gauge,
  153. },
  154. ],
  155. },
  156. {
  157. description: t(
  158. 'It can take up to 3 minutes for the data to appear in the Sentry UI.'
  159. ),
  160. },
  161. {
  162. description: tct(
  163. 'Learn more about metrics and how to configure them, by reading the [docsLink:docs].',
  164. {
  165. docsLink: (
  166. <ExternalLink href="https://docs.sentry.io/platforms/dart/metrics/" />
  167. ),
  168. }
  169. ),
  170. },
  171. ],
  172. },
  173. ],
  174. };
  175. const onboarding: OnboardingConfig = {
  176. install: params => [
  177. {
  178. type: StepType.INSTALL,
  179. description: tct(
  180. 'Sentry captures data by using an SDK within your application’s runtime. Add the following to your [pubspec: pubspec.yaml]',
  181. {
  182. pubspec: <code />,
  183. }
  184. ),
  185. configurations: [
  186. {
  187. language: 'yml',
  188. partialLoading: params.sourcePackageRegistries.isLoading,
  189. code: getInstallSnippet(params),
  190. },
  191. ],
  192. },
  193. ],
  194. configure: params => [
  195. {
  196. type: StepType.CONFIGURE,
  197. description: tct('Import [sentry: sentry] and initialize it', {
  198. sentry: <code />,
  199. }),
  200. configurations: [
  201. {
  202. language: 'dart',
  203. code: getConfigureSnippet(params),
  204. additionalInfo: tct(
  205. '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.',
  206. {
  207. code: <code />,
  208. }
  209. ),
  210. },
  211. ],
  212. },
  213. ],
  214. verify: () => [
  215. {
  216. type: StepType.VERIFY,
  217. description: t(
  218. 'Create an intentional error, so you can test that everything is working:'
  219. ),
  220. configurations: [
  221. {
  222. language: 'dart',
  223. code: getVerifySnippet(),
  224. additionalInfo: tct(
  225. "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.",
  226. {
  227. break: <br />,
  228. }
  229. ),
  230. },
  231. ],
  232. },
  233. {
  234. title: t('Tracing'),
  235. description: t(
  236. "You'll be able to monitor the performance of your app using the SDK. For example:"
  237. ),
  238. configurations: [
  239. {
  240. language: 'dart',
  241. code: getPerfomanceSnippet(),
  242. additionalInfo: tct(
  243. 'To learn more about the API and automatic instrumentations, check out the [perfDocs: performance documentation].',
  244. {
  245. perfDocs: (
  246. <ExternalLink href="https://docs.sentry.io/platforms/dart/tracing/instrumentation/" />
  247. ),
  248. }
  249. ),
  250. },
  251. ],
  252. },
  253. ],
  254. };
  255. export const feedbackOnboardingCrashApiDart: OnboardingConfig = {
  256. introduction: () => getCrashReportApiIntroduction(),
  257. install: () => [
  258. {
  259. type: StepType.INSTALL,
  260. description: getCrashReportInstallDescription(),
  261. configurations: [
  262. {
  263. code: [
  264. {
  265. label: 'Dart',
  266. value: 'dart',
  267. language: 'dart',
  268. code: `import 'package:sentry/sentry.dart';
  269. SentryId sentryId = Sentry.captureMessage("My message");
  270. final userFeedback = SentryUserFeedback(
  271. eventId: sentryId,
  272. comments: 'Hello World!',
  273. email: 'foo@bar.org',
  274. name: 'John Doe',
  275. );
  276. Sentry.captureUserFeedback(userFeedback);`,
  277. },
  278. ],
  279. },
  280. ],
  281. },
  282. ],
  283. configure: () => [],
  284. verify: () => [],
  285. nextSteps: () => [],
  286. };
  287. const docs: Docs = {
  288. onboarding,
  289. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiDart,
  290. crashReportOnboarding: feedbackOnboardingCrashApiDart,
  291. customMetricsOnboarding: metricsOnboarding,
  292. };
  293. export default docs;