java.tsx 11 KB

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