java.tsx 11 KB

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