dart.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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}';
  26. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  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}';
  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 [codeVersion:7.19.0] or higher. Learn more about installation methods in our [docsLink:full documentation].',
  80. {
  81. package: <code />,
  82. codeVersion: <code />,
  83. docsLink: <Link to={`/projects/${params.projectSlug}/getting-started/`} />,
  84. }
  85. ),
  86. configurations: [
  87. {
  88. language: 'yml',
  89. partialLoading: params.sourcePackageRegistries?.isLoading,
  90. code: getInstallSnippet(params),
  91. },
  92. ],
  93. },
  94. ],
  95. configure: (params: DocsParams) => [
  96. {
  97. type: StepType.CONFIGURE,
  98. description: t(
  99. 'To enable capturing metrics, you need to enable the metrics feature.'
  100. ),
  101. configurations: [
  102. {
  103. code: [
  104. {
  105. label: 'Dart',
  106. value: 'dart',
  107. language: 'dart',
  108. code: getConfigureMetricsSnippet(params),
  109. },
  110. ],
  111. },
  112. ],
  113. },
  114. ],
  115. verify: () => [
  116. {
  117. type: StepType.VERIFY,
  118. description: tct(
  119. "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.",
  120. {
  121. codeCounters: <code />,
  122. codeSets: <code />,
  123. codeDistribution: <code />,
  124. codeGauge: <code />,
  125. codeNamespace: <code />,
  126. }
  127. ),
  128. configurations: [
  129. {
  130. description: metricTagsExplanation,
  131. },
  132. {
  133. description: t('Try out these examples:'),
  134. code: [
  135. {
  136. label: 'Counter',
  137. value: 'counter',
  138. language: 'dart',
  139. code: exampleSnippets.dart.counter,
  140. },
  141. {
  142. label: 'Distribution',
  143. value: 'distribution',
  144. language: 'dart',
  145. code: exampleSnippets.dart.distribution,
  146. },
  147. {
  148. label: 'Set',
  149. value: 'set',
  150. language: 'dart',
  151. code: exampleSnippets.dart.set,
  152. },
  153. {
  154. label: 'Gauge',
  155. value: 'gauge',
  156. language: 'dart',
  157. code: exampleSnippets.dart.gauge,
  158. },
  159. ],
  160. },
  161. {
  162. description: t(
  163. 'It can take up to 3 minutes for the data to appear in the Sentry UI.'
  164. ),
  165. },
  166. {
  167. description: tct(
  168. 'Learn more about metrics and how to configure them, by reading the [docsLink:docs].',
  169. {
  170. docsLink: (
  171. <ExternalLink href="https://docs.sentry.io/platforms/dart/metrics/" />
  172. ),
  173. }
  174. ),
  175. },
  176. ],
  177. },
  178. ],
  179. };
  180. const onboarding: OnboardingConfig = {
  181. install: params => [
  182. {
  183. type: StepType.INSTALL,
  184. description: tct(
  185. 'Sentry captures data by using an SDK within your application’s runtime. Add the following to your [pubspec: pubspec.yaml]',
  186. {
  187. pubspec: <code />,
  188. }
  189. ),
  190. configurations: [
  191. {
  192. language: 'yml',
  193. partialLoading: params.sourcePackageRegistries.isLoading,
  194. code: getInstallSnippet(params),
  195. },
  196. ],
  197. },
  198. ],
  199. configure: params => [
  200. {
  201. type: StepType.CONFIGURE,
  202. description: tct('Import [sentry: sentry] and initialize it', {
  203. sentry: <code />,
  204. }),
  205. configurations: [
  206. {
  207. language: 'dart',
  208. code: getConfigureSnippet(params),
  209. additionalInfo: tct(
  210. '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.',
  211. {
  212. sentryDsn: <code />,
  213. sentryRelease: <code />,
  214. sentryDist: <code />,
  215. sentryEnv: <code />,
  216. dartDefine: <code />,
  217. }
  218. ),
  219. },
  220. ],
  221. },
  222. ],
  223. verify: () => [
  224. {
  225. type: StepType.VERIFY,
  226. description: t(
  227. 'Create an intentional error, so you can test that everything is working:'
  228. ),
  229. configurations: [
  230. {
  231. language: 'dart',
  232. code: getVerifySnippet(),
  233. additionalInfo: tct(
  234. "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.",
  235. {
  236. break: <br />,
  237. }
  238. ),
  239. },
  240. ],
  241. },
  242. {
  243. title: t('Performance'),
  244. description: t(
  245. "You'll be able to monitor the performance of your app using the SDK. For example:"
  246. ),
  247. configurations: [
  248. {
  249. language: 'dart',
  250. code: getPerfomanceSnippet(),
  251. additionalInfo: tct(
  252. 'To learn more about the API and automatic instrumentations, check out the [perfDocs: performance documentation].',
  253. {
  254. perfDocs: (
  255. <ExternalLink href="https://docs.sentry.io/platforms/dart/performance/instrumentation/" />
  256. ),
  257. }
  258. ),
  259. },
  260. ],
  261. },
  262. ],
  263. };
  264. export const feedbackOnboardingCrashApiDart: OnboardingConfig = {
  265. introduction: () => getCrashReportApiIntroduction(),
  266. install: () => [
  267. {
  268. type: StepType.INSTALL,
  269. description: getCrashReportInstallDescription(),
  270. configurations: [
  271. {
  272. code: [
  273. {
  274. label: 'Dart',
  275. value: 'dart',
  276. language: 'dart',
  277. code: `import 'package:sentry/sentry.dart';
  278. SentryId sentryId = Sentry.captureMessage("My message");
  279. final userFeedback = SentryUserFeedback(
  280. eventId: sentryId,
  281. comments: 'Hello World!',
  282. email: 'foo@bar.org',
  283. name: 'John Doe',
  284. );
  285. Sentry.captureUserFeedback(userFeedback);`,
  286. },
  287. ],
  288. },
  289. ],
  290. },
  291. ],
  292. configure: () => [],
  293. verify: () => [],
  294. nextSteps: () => [],
  295. };
  296. const docs: Docs = {
  297. onboarding,
  298. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiDart,
  299. crashReportOnboarding: feedbackOnboardingCrashApiDart,
  300. customMetricsOnboarding: metricsOnboarding,
  301. };
  302. export default docs;