java.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Link from 'sentry/components/links/link';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. BasePlatformOptions,
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {
  12. getCrashReportApiIntroduction,
  13. getCrashReportInstallDescription,
  14. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  15. import {t, tct} from 'sentry/locale';
  16. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  17. export enum PackageManager {
  18. GRADLE = 'gradle',
  19. MAVEN = 'maven',
  20. SBT = 'sbt',
  21. }
  22. const packageManagerName: Record<PackageManager, string> = {
  23. [PackageManager.GRADLE]: 'Gradle',
  24. [PackageManager.MAVEN]: 'Maven',
  25. [PackageManager.SBT]: 'SBT',
  26. };
  27. const platformOptions = {
  28. packageManager: {
  29. label: t('Package Manager'),
  30. items: [
  31. {
  32. label: packageManagerName[PackageManager.GRADLE],
  33. value: PackageManager.GRADLE,
  34. },
  35. {
  36. label: packageManagerName[PackageManager.MAVEN],
  37. value: PackageManager.MAVEN,
  38. },
  39. {
  40. label: packageManagerName[PackageManager.SBT],
  41. value: PackageManager.SBT,
  42. },
  43. ],
  44. },
  45. } satisfies BasePlatformOptions;
  46. type PlatformOptions = typeof platformOptions;
  47. type Params = DocsParams<PlatformOptions>;
  48. const getGradleInstallSnippet = (params: Params) => `
  49. buildscript {
  50. repositories {
  51. mavenCentral()
  52. }
  53. }
  54. plugins {
  55. id "io.sentry.jvm.gradle" version "${getPackageVersion(
  56. params,
  57. 'sentry.java.android.gradle-plugin',
  58. '3.12.0'
  59. )}"
  60. }
  61. sentry {
  62. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  63. // This enables source context, allowing you to see your source
  64. // code as part of your stack traces in Sentry.
  65. includeSourceContext = true
  66. org = "${params.organization.slug}"
  67. projectName = "${params.projectSlug}"
  68. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  69. }`;
  70. const getMavenInstallSnippet = (params: Params) => `
  71. <build>
  72. <plugins>
  73. <plugin>
  74. <groupId>io.sentry</groupId>
  75. <artifactId>sentry-maven-plugin</artifactId>
  76. <version>${getPackageVersion(params, 'sentry.java.maven-plugin', '0.0.4')}</version>
  77. <extensions>true</extensions>
  78. <configuration>
  79. <!-- for showing output of sentry-cli -->
  80. <debugSentryCli>true</debugSentryCli>
  81. <org>${params.organization.slug}</org>
  82. <project>${params.projectSlug}</project>
  83. <!-- in case you're self hosting, provide the URL here -->
  84. <!--<url>http://localhost:8000/</url>-->
  85. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  86. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  87. </configuration>
  88. <executions>
  89. <execution>
  90. <goals>
  91. <!--
  92. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  93. This enables source context, allowing you to see your source
  94. code as part of your stack traces in Sentry.
  95. -->
  96. <goal>uploadSourceBundle</goal>
  97. </goals>
  98. </execution>
  99. </executions>
  100. </plugin>
  101. </plugins>
  102. ...
  103. </build>`;
  104. const getConfigureSnippet = (params: Params) => `
  105. import io.sentry.Sentry;
  106. Sentry.init(options -> {
  107. options.setDsn("${params.dsn}");${
  108. params.isPerformanceSelected
  109. ? `
  110. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  111. // We recommend adjusting this value in production.
  112. options.setTracesSampleRate(1.0);`
  113. : ''
  114. }
  115. // When first trying Sentry it's good to see what the SDK is doing:
  116. options.setDebug(true);
  117. });`;
  118. const getVerifySnippet = () => `
  119. import java.lang.Exception;
  120. import io.sentry.Sentry;
  121. try {
  122. throw new Exception("This is a test.");
  123. } catch (Exception e) {
  124. Sentry.captureException(e);
  125. }`;
  126. const onboarding: OnboardingConfig<PlatformOptions> = {
  127. introduction: () =>
  128. tct(
  129. 'Sentry for Java is a collection of modules provided by Sentry; it supports Java 1.8 and above. At its core, Sentry for Java provides a raw client for sending events to Sentry. If you use [strong:Spring Boot, Spring, Logback, or Log4j2], we recommend visiting our Sentry Java documentation for installation instructions.',
  130. {
  131. strong: <strong />,
  132. link: <ExternalLink href="https://docs.sentry.io/platforms/java/" />,
  133. }
  134. ),
  135. install: params => [
  136. {
  137. type: StepType.INSTALL,
  138. description: t(
  139. `Install the SDK via %s:`,
  140. packageManagerName[params.platformOptions.packageManager]
  141. ),
  142. configurations: [
  143. {
  144. description: tct(
  145. 'To see source context in Sentry, you have to generate an auth token by visiting the [link:Organization Auth Tokens] settings. You can then set the token as an environment variable that is used by the build plugins.',
  146. {
  147. link: <Link to="/settings/auth-tokens/" />,
  148. }
  149. ),
  150. language: 'bash',
  151. code: `SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___`,
  152. },
  153. ...(params.platformOptions.packageManager === PackageManager.GRADLE
  154. ? [
  155. {
  156. language: 'groovy',
  157. partialLoading: params.sourcePackageRegistries?.isLoading,
  158. description: tct(
  159. 'The [link:Sentry Gradle Plugin] automatically installs the Sentry SDK as well as available integrations for your dependencies. Add the following to your [code:build.gradle] file:',
  160. {
  161. code: <code />,
  162. link: (
  163. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  164. ),
  165. }
  166. ),
  167. code: getGradleInstallSnippet(params),
  168. },
  169. ]
  170. : []),
  171. ...(params.platformOptions.packageManager === PackageManager.MAVEN
  172. ? [
  173. {
  174. language: 'xml',
  175. partialLoading: params.sourcePackageRegistries?.isLoading,
  176. description: tct(
  177. 'The [link:Sentry Maven Plugin] automatically installs the Sentry SDK as well as available integrations for your dependencies. Add the following to your [code:pom.xml] file:',
  178. {
  179. code: <code />,
  180. link: (
  181. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  182. ),
  183. }
  184. ),
  185. code: getMavenInstallSnippet(params),
  186. },
  187. ]
  188. : []),
  189. ...(params.platformOptions.packageManager === PackageManager.SBT
  190. ? [
  191. {
  192. description: tct(
  193. 'Add the sentry SDK to your [code:libraryDependencies]:',
  194. {
  195. code: <code />,
  196. }
  197. ),
  198. language: 'scala',
  199. partialLoading: params.sourcePackageRegistries?.isLoading,
  200. code: `libraryDependencies += "io.sentry" % "sentry" % "${getPackageVersion(
  201. params,
  202. 'sentry.java',
  203. '6.27.0'
  204. )}"`,
  205. },
  206. ]
  207. : []),
  208. ],
  209. additionalInfo: tct(
  210. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  211. {
  212. link: (
  213. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  214. ),
  215. }
  216. ),
  217. },
  218. ],
  219. configure: params => [
  220. {
  221. type: StepType.CONFIGURE,
  222. description: t(
  223. "Configure Sentry as soon as possible in your application's lifecycle:"
  224. ),
  225. configurations: [
  226. {
  227. language: 'java',
  228. code: getConfigureSnippet(params),
  229. },
  230. ],
  231. },
  232. ],
  233. verify: () => [
  234. {
  235. type: StepType.VERIFY,
  236. description: tct(
  237. 'Trigger your first event from your development environment by intentionally creating an error with the [code:Sentry#captureException] method, to test that everything is working:',
  238. {code: <code />}
  239. ),
  240. configurations: [
  241. {
  242. language: 'java',
  243. code: getVerifySnippet(),
  244. },
  245. ],
  246. additionalInfo: (
  247. <Fragment>
  248. <p>
  249. {t(
  250. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  251. )}
  252. </p>
  253. <p>
  254. {t(
  255. "If you're an existing user and have disabled alerts, you won't receive this email."
  256. )}
  257. </p>
  258. </Fragment>
  259. ),
  260. },
  261. ],
  262. nextSteps: () => [
  263. {
  264. id: 'examples',
  265. name: t('Examples'),
  266. description: t('Check out our sample applications.'),
  267. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  268. },
  269. {
  270. id: 'performance-monitoring',
  271. name: t('Performance Monitoring'),
  272. description: t(
  273. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  274. ),
  275. link: 'https://docs.sentry.io/platforms/java/performance/',
  276. },
  277. ],
  278. };
  279. export const feedbackOnboardingCrashApiJava: OnboardingConfig = {
  280. introduction: () => getCrashReportApiIntroduction(),
  281. install: () => [
  282. {
  283. type: StepType.INSTALL,
  284. description: getCrashReportInstallDescription(),
  285. configurations: [
  286. {
  287. code: [
  288. {
  289. label: 'Java',
  290. value: 'java',
  291. language: 'java',
  292. code: `import io.sentry.Sentry;
  293. import io.sentry.UserFeedback;
  294. SentryId sentryId = Sentry.captureMessage("My message");
  295. UserFeedback userFeedback = new UserFeedback(sentryId);
  296. userFeedback.setComments("It broke.");
  297. userFeedback.setEmail("john.doe@example.com");
  298. userFeedback.setName("John Doe");
  299. Sentry.captureUserFeedback(userFeedback);`,
  300. },
  301. {
  302. label: 'Kotlin',
  303. value: 'kotlin',
  304. language: 'kotlin',
  305. code: `import io.sentry.Sentry
  306. import io.sentry.UserFeedback
  307. val sentryId = Sentry.captureMessage("My message")
  308. val userFeedback = UserFeedback(sentryId).apply {
  309. comments = "It broke."
  310. email = "john.doe@example.com"
  311. name = "John Doe"
  312. }
  313. Sentry.captureUserFeedback(userFeedback)`,
  314. },
  315. ],
  316. },
  317. ],
  318. },
  319. ],
  320. configure: () => [],
  321. verify: () => [],
  322. nextSteps: () => [],
  323. };
  324. const docs: Docs<PlatformOptions> = {
  325. platformOptions,
  326. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  327. onboarding,
  328. };
  329. export default docs;