dart.tsx 8.5 KB

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