dart.tsx 9.1 KB

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