java.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.public}");${
  109. params.isPerformanceSelected
  110. ? `
  111. // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
  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. };
  272. export const feedbackOnboardingCrashApiJava: OnboardingConfig = {
  273. introduction: () => getCrashReportApiIntroduction(),
  274. install: () => [
  275. {
  276. type: StepType.INSTALL,
  277. description: getCrashReportInstallDescription(),
  278. configurations: [
  279. {
  280. code: [
  281. {
  282. label: 'Java',
  283. value: 'java',
  284. language: 'java',
  285. code: `import io.sentry.Sentry;
  286. import io.sentry.UserFeedback;
  287. SentryId sentryId = Sentry.captureMessage("My message");
  288. UserFeedback userFeedback = new UserFeedback(sentryId);
  289. userFeedback.setComments("It broke.");
  290. userFeedback.setEmail("john.doe@example.com");
  291. userFeedback.setName("John Doe");
  292. Sentry.captureUserFeedback(userFeedback);`,
  293. },
  294. {
  295. label: 'Kotlin',
  296. value: 'kotlin',
  297. language: 'kotlin',
  298. code: `import io.sentry.Sentry
  299. import io.sentry.UserFeedback
  300. val sentryId = Sentry.captureMessage("My message")
  301. val userFeedback = UserFeedback(sentryId).apply {
  302. comments = "It broke."
  303. email = "john.doe@example.com"
  304. name = "John Doe"
  305. }
  306. Sentry.captureUserFeedback(userFeedback)`,
  307. },
  308. ],
  309. },
  310. ],
  311. },
  312. ],
  313. configure: () => [],
  314. verify: () => [],
  315. nextSteps: () => [],
  316. };
  317. const docs: Docs<PlatformOptions> = {
  318. platformOptions,
  319. feedbackOnboardingCrashApi: feedbackOnboardingCrashApiJava,
  320. crashReportOnboarding: feedbackOnboardingCrashApiJava,
  321. customMetricsOnboarding: getJavaMetricsOnboarding(),
  322. onboarding,
  323. };
  324. export default docs;