java.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. language: 'xml',
  136. partialLoading: sourcePackageRegistries?.isLoading,
  137. description: (
  138. <p>
  139. {tct(
  140. '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:',
  141. {
  142. code: <code />,
  143. link: (
  144. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  145. ),
  146. }
  147. )}
  148. </p>
  149. ),
  150. code: `<build>
  151. <plugins>
  152. <plugin>
  153. <groupId>io.sentry</groupId>
  154. <artifactId>sentry-maven-plugin</artifactId>
  155. <version>${
  156. sourcePackageRegistries?.isLoading
  157. ? t('\u2026loading')
  158. : sourcePackageRegistries?.data?.['sentry.java.maven-plugin']?.version ??
  159. '0.0.4'
  160. }</version>
  161. <extensions>true</extensions>
  162. <configuration>
  163. <!-- for showing output of sentry-cli -->
  164. <debugSentryCli>true</debugSentryCli>
  165. <org>${organizationSlug}</org>
  166. <project>${projectSlug}</project>
  167. <!-- in case you're self hosting, provide the URL here -->
  168. <!--<url>http://localhost:8000/</url>-->
  169. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  170. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  171. </configuration>
  172. <executions>
  173. <execution>
  174. <goals>
  175. <!--
  176. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  177. This enables source context, allowing you to see your source
  178. code as part of your stack traces in Sentry.
  179. -->
  180. <goal>uploadSourceBundle</goal>
  181. </goals>
  182. </execution>
  183. </executions>
  184. </plugin>
  185. </plugins>
  186. ...
  187. </build>`,
  188. },
  189. ]
  190. : []),
  191. ...(packageManager === PackageManager.SBT
  192. ? [
  193. {
  194. description: (
  195. <p>
  196. {tct('Add the sentry SDK to your [code:libraryDependencies]:', {
  197. code: <code />,
  198. })}
  199. </p>
  200. ),
  201. language: 'scala',
  202. partialLoading: sourcePackageRegistries?.isLoading,
  203. code: `libraryDependencies += "io.sentry" % "sentry" % "${
  204. sourcePackageRegistries?.isLoading
  205. ? t('\u2026loading')
  206. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  207. }"`,
  208. },
  209. ]
  210. : []),
  211. ],
  212. additionalInfo: (
  213. <p>
  214. {tct(
  215. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  216. {
  217. link: (
  218. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  219. ),
  220. }
  221. )}
  222. </p>
  223. ),
  224. },
  225. {
  226. type: StepType.CONFIGURE,
  227. description: t(
  228. "Configure Sentry as soon as possible in your application's lifecycle:"
  229. ),
  230. configurations: [
  231. {
  232. language: 'java',
  233. code: `
  234. import io.sentry.Sentry;
  235. Sentry.init(options -> {
  236. options.setDsn("${dsn}");${
  237. hasPerformance
  238. ? `
  239. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  240. // We recommend adjusting this value in production.
  241. options.setTracesSampleRate(1.0);`
  242. : ''
  243. }
  244. // When first trying Sentry it's good to see what the SDK is doing:
  245. options.setDebug(true);
  246. });
  247. `,
  248. },
  249. ],
  250. },
  251. {
  252. type: StepType.VERIFY,
  253. description: (
  254. <p>
  255. {tct(
  256. '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:',
  257. {code: <code />}
  258. )}
  259. </p>
  260. ),
  261. configurations: [
  262. {
  263. language: 'java',
  264. code: `
  265. import java.lang.Exception;
  266. import io.sentry.Sentry;
  267. try {
  268. throw new Exception("This is a test.");
  269. } catch (Exception e) {
  270. Sentry.captureException(e);
  271. }
  272. `,
  273. },
  274. ],
  275. additionalInfo: (
  276. <Fragment>
  277. <p>
  278. {t(
  279. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  280. )}
  281. </p>
  282. <p>
  283. {t(
  284. "If you're an existing user and have disabled alerts, you won't receive this email."
  285. )}
  286. </p>
  287. </Fragment>
  288. ),
  289. },
  290. ];
  291. export const nextSteps = [
  292. {
  293. id: 'examples',
  294. name: t('Examples'),
  295. description: t('Check out our sample applications.'),
  296. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  297. },
  298. {
  299. id: 'performance-monitoring',
  300. name: t('Performance Monitoring'),
  301. description: t(
  302. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  303. ),
  304. link: 'https://docs.sentry.io/platforms/java/performance/',
  305. },
  306. ];
  307. // Configuration End
  308. export function GettingStartedWithJava({
  309. dsn,
  310. sourcePackageRegistries,
  311. projectSlug,
  312. organization,
  313. activeProductSelection = [],
  314. ...props
  315. }: ModuleProps) {
  316. const optionValues = useUrlPlatformOptions(platformOptions);
  317. const hasPerformance = activeProductSelection.includes(
  318. ProductSolution.PERFORMANCE_MONITORING
  319. );
  320. const nextStepDocs = [...nextSteps];
  321. return (
  322. <Layout
  323. steps={steps({
  324. dsn,
  325. sourcePackageRegistries,
  326. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  327. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  328. packageManager: optionValues.packageManager as PackageManager,
  329. hasPerformance,
  330. })}
  331. platformOptions={platformOptions}
  332. nextSteps={nextStepDocs}
  333. introduction={introduction}
  334. projectSlug={projectSlug}
  335. {...props}
  336. />
  337. );
  338. }
  339. export default GettingStartedWithJava;