java.tsx 13 KB

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