java.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Link from 'sentry/components/links/link';
  4. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  5. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  6. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  7. import {PlatformOption} from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {useUrlPlatformOptions} from 'sentry/components/onboarding/platformOptionsControl';
  9. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  10. import {t, tct} from 'sentry/locale';
  11. export enum PackageManager {
  12. GRADLE = 'gradle',
  13. MAVEN = 'maven',
  14. SBT = 'sbt',
  15. }
  16. type PlaformOptionKey = 'packageManager';
  17. interface StepsParams {
  18. dsn: string;
  19. hasPerformance: boolean;
  20. packageManager: PackageManager;
  21. organizationSlug?: string;
  22. projectSlug?: string;
  23. sourcePackageRegistries?: ModuleProps['sourcePackageRegistries'];
  24. }
  25. // Configuration Start
  26. const packageManagerName: Record<PackageManager, string> = {
  27. [PackageManager.GRADLE]: 'Gradle',
  28. [PackageManager.MAVEN]: 'Maven',
  29. [PackageManager.SBT]: 'SBT',
  30. };
  31. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  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. };
  50. const introduction = (
  51. <p>
  52. {tct(
  53. '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.',
  54. {
  55. strong: <strong />,
  56. link: <ExternalLink href="https://docs.sentry.io/platforms/java/" />,
  57. }
  58. )}
  59. </p>
  60. );
  61. export const steps = ({
  62. dsn,
  63. sourcePackageRegistries,
  64. packageManager,
  65. projectSlug,
  66. hasPerformance,
  67. organizationSlug,
  68. }: StepsParams): LayoutProps['steps'] => [
  69. {
  70. type: StepType.INSTALL,
  71. description: t(`Install the SDK via %s:`, packageManagerName[packageManager]),
  72. configurations: [
  73. {
  74. description: (
  75. <p>
  76. {tct(
  77. '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.',
  78. {
  79. link: <Link to="/settings/auth-tokens/" />,
  80. }
  81. )}
  82. </p>
  83. ),
  84. language: 'bash',
  85. code: `SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___`,
  86. },
  87. ...(packageManager === PackageManager.GRADLE
  88. ? [
  89. {
  90. language: 'groovy',
  91. partialLoading: sourcePackageRegistries?.isLoading,
  92. description: (
  93. <p>
  94. {tct(
  95. '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:',
  96. {
  97. code: <code />,
  98. link: (
  99. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  100. ),
  101. }
  102. )}
  103. </p>
  104. ),
  105. code: `
  106. buildscript {
  107. repositories {
  108. mavenCentral()
  109. }
  110. }
  111. plugins {
  112. id "io.sentry.jvm.gradle" version "${
  113. sourcePackageRegistries?.isLoading
  114. ? t('\u2026loading')
  115. : sourcePackageRegistries?.data?.['sentry.java.android.gradle-plugin']?.version ??
  116. '3.12.0'
  117. }"
  118. }
  119. sentry {
  120. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  121. // This enables source context, allowing you to see your source
  122. // code as part of your stack traces in Sentry.
  123. includeSourceContext = true
  124. org = "${organizationSlug}"
  125. projectName = "${projectSlug}"
  126. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  127. }
  128. `,
  129. },
  130. ]
  131. : []),
  132. ...(packageManager === PackageManager.MAVEN
  133. ? [
  134. {
  135. description: (
  136. <p>
  137. {tct('Add the Sentry SDK to your [code:pom.xml] file:', {
  138. code: <code />,
  139. })}
  140. </p>
  141. ),
  142. configurations: [
  143. {
  144. language: 'xml',
  145. partialLoading: sourcePackageRegistries?.isLoading,
  146. code: `
  147. <dependency>
  148. <groupId>io.sentry</groupId>
  149. <artifactId>sentry</artifactId>
  150. <version>${
  151. sourcePackageRegistries?.isLoading
  152. ? t('\u2026loading')
  153. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  154. }</version>
  155. </dependency>
  156. `,
  157. },
  158. {
  159. language: 'xml',
  160. partialLoading: sourcePackageRegistries?.isLoading,
  161. description: t(
  162. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  163. ),
  164. code: `
  165. <build>
  166. <plugins>
  167. <plugin>
  168. <groupId>io.sentry</groupId>
  169. <artifactId>sentry-maven-plugin</artifactId>
  170. <version>${
  171. sourcePackageRegistries?.isLoading
  172. ? t('\u2026loading')
  173. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ?? '0.0.4'
  174. }</version>
  175. <configuration>
  176. <!-- for showing output of sentry-cli -->
  177. <debugSentryCli>true</debugSentryCli>
  178. <org>${organizationSlug}</org>
  179. <project>${projectSlug}</project>
  180. <!-- in case you're self hosting, provide the URL here -->
  181. <!--<url>http://localhost:8000/</url>-->
  182. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  183. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  184. </configuration>
  185. <executions>
  186. <execution>
  187. <phase>generate-resources</phase>
  188. <goals>
  189. <goal>uploadSourceBundle</goal>
  190. </goals>
  191. </execution>
  192. </executions>
  193. </plugin>
  194. </plugins>
  195. ...
  196. </build>
  197. `,
  198. },
  199. ],
  200. },
  201. ]
  202. : []),
  203. ...(packageManager === PackageManager.SBT
  204. ? [
  205. {
  206. description: (
  207. <p>
  208. {tct('Add the sentry SDK to your [code:libraryDependencies]:', {
  209. code: <code />,
  210. })}
  211. </p>
  212. ),
  213. language: 'scala',
  214. partialLoading: sourcePackageRegistries?.isLoading,
  215. code: `libraryDependencies += "io.sentry" % "sentry" % "${
  216. sourcePackageRegistries?.isLoading
  217. ? t('\u2026loading')
  218. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  219. }"`,
  220. },
  221. ]
  222. : []),
  223. ],
  224. additionalInfo: (
  225. <p>
  226. {tct(
  227. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  228. {
  229. link: (
  230. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  231. ),
  232. }
  233. )}
  234. </p>
  235. ),
  236. },
  237. {
  238. type: StepType.CONFIGURE,
  239. description: t(
  240. "Configure Sentry as soon as possible in your application's lifecycle:"
  241. ),
  242. configurations: [
  243. {
  244. language: 'java',
  245. code: `
  246. import io.sentry.Sentry;
  247. Sentry.init(options -> {
  248. options.setDsn("${dsn}");${
  249. hasPerformance
  250. ? `
  251. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  252. // We recommend adjusting this value in production.
  253. options.setTracesSampleRate(1.0);`
  254. : ''
  255. }
  256. // When first trying Sentry it's good to see what the SDK is doing:
  257. options.setDebug(true);
  258. });
  259. `,
  260. },
  261. ],
  262. },
  263. {
  264. type: StepType.VERIFY,
  265. description: (
  266. <p>
  267. {tct(
  268. '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:',
  269. {code: <code />}
  270. )}
  271. </p>
  272. ),
  273. configurations: [
  274. {
  275. language: 'java',
  276. code: `
  277. import java.lang.Exception;
  278. import io.sentry.Sentry;
  279. try {
  280. throw new Exception("This is a test.");
  281. } catch (Exception e) {
  282. Sentry.captureException(e);
  283. }
  284. `,
  285. },
  286. ],
  287. additionalInfo: (
  288. <Fragment>
  289. <p>
  290. {t(
  291. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  292. )}
  293. </p>
  294. <p>
  295. {t(
  296. "If you're an existing user and have disabled alerts, you won't receive this email."
  297. )}
  298. </p>
  299. </Fragment>
  300. ),
  301. },
  302. ];
  303. export const nextSteps = [
  304. {
  305. id: 'examples',
  306. name: t('Examples'),
  307. description: t('Check out our sample applications.'),
  308. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  309. },
  310. {
  311. id: 'performance-monitoring',
  312. name: t('Performance Monitoring'),
  313. description: t(
  314. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  315. ),
  316. link: 'https://docs.sentry.io/platforms/java/performance/',
  317. },
  318. ];
  319. // Configuration End
  320. export function GettingStartedWithJava({
  321. dsn,
  322. sourcePackageRegistries,
  323. projectSlug,
  324. organization,
  325. activeProductSelection = [],
  326. ...props
  327. }: ModuleProps) {
  328. const optionValues = useUrlPlatformOptions(platformOptions);
  329. const hasPerformance = activeProductSelection.includes(
  330. ProductSolution.PERFORMANCE_MONITORING
  331. );
  332. const nextStepDocs = [...nextSteps];
  333. return (
  334. <Layout
  335. steps={steps({
  336. dsn,
  337. sourcePackageRegistries,
  338. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  339. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  340. packageManager: optionValues.packageManager as PackageManager,
  341. hasPerformance,
  342. })}
  343. platformOptions={platformOptions}
  344. nextSteps={nextStepDocs}
  345. introduction={introduction}
  346. projectSlug={projectSlug}
  347. {...props}
  348. />
  349. );
  350. }
  351. export default GettingStartedWithJava;