java.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  4. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  5. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  6. import {t, tct} from 'sentry/locale';
  7. interface StepProps {
  8. dsn: string;
  9. organizationSlug?: string;
  10. projectSlug?: string;
  11. sourcePackageRegistries?: ModuleProps['sourcePackageRegistries'];
  12. }
  13. // Configuration Start
  14. const introduction = (
  15. <p>
  16. {tct(
  17. '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.',
  18. {
  19. strong: <strong />,
  20. link: <ExternalLink href="https://docs.sentry.io/platforms/java/" />,
  21. }
  22. )}
  23. </p>
  24. );
  25. export const steps = ({
  26. dsn,
  27. sourcePackageRegistries,
  28. projectSlug,
  29. organizationSlug,
  30. }: StepProps): LayoutProps['steps'] => [
  31. {
  32. type: StepType.INSTALL,
  33. description: t('Install the SDK via Gradle, Maven, or SBT:'),
  34. configurations: [
  35. {
  36. description: <h5>{t('Gradle')}</h5>,
  37. configurations: [
  38. {
  39. language: 'groovy',
  40. partialLoading: sourcePackageRegistries?.isLoading,
  41. description: (
  42. <p>
  43. {tct('For Gradle, add to your [code:build.gradle] file:', {
  44. code: <code />,
  45. })}
  46. </p>
  47. ),
  48. code: `
  49. // Make sure mavenCentral is there.
  50. repositories {
  51. mavenCentral()
  52. }
  53. // Add Sentry's SDK as a dependency.
  54. dependencies {
  55. implementation 'io.sentry:sentry:${
  56. sourcePackageRegistries?.isLoading
  57. ? t('\u2026loading')
  58. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  59. }'
  60. }
  61. `,
  62. },
  63. {
  64. language: 'groovy',
  65. partialLoading: sourcePackageRegistries?.isLoading,
  66. description: t(
  67. 'To upload your source code to Sentry so it can be shown in stack traces, use our Gradle plugin.'
  68. ),
  69. code: `
  70. buildscript {
  71. repositories {
  72. mavenCentral()
  73. }
  74. }
  75. plugins {
  76. id "io.sentry.jvm.gradle" version "${
  77. sourcePackageRegistries?.isLoading
  78. ? t('\u2026loading')
  79. : sourcePackageRegistries?.data?.['sentry.java.android.gradle-plugin']?.version ??
  80. '3.11.1'
  81. }"
  82. }
  83. sentry {
  84. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  85. // This enables source context, allowing you to see your source
  86. // code as part of your stack traces in Sentry.
  87. includeSourceContext = true
  88. org = "${organizationSlug}"
  89. projectName = "${projectSlug}"
  90. authToken = "your-sentry-auth-token"
  91. }
  92. `,
  93. },
  94. ],
  95. },
  96. {
  97. description: <h5>{t('Maven')}</h5>,
  98. configurations: [
  99. {
  100. language: 'xml',
  101. partialLoading: sourcePackageRegistries?.isLoading,
  102. description: (
  103. <p>
  104. {tct('For Maven, add to your [code:pom.xml] file:', {code: <code />})}
  105. </p>
  106. ),
  107. code: `
  108. <dependency>
  109. <groupId>io.sentry</groupId>
  110. <artifactId>sentry</artifactId>
  111. <version>${
  112. sourcePackageRegistries?.isLoading
  113. ? t('\u2026loading')
  114. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  115. }</version>
  116. </dependency>
  117. `,
  118. },
  119. {
  120. language: 'xml',
  121. partialLoading: sourcePackageRegistries?.isLoading,
  122. description: t(
  123. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  124. ),
  125. code: `
  126. <build>
  127. <plugins>
  128. <plugin>
  129. <groupId>io.sentry</groupId>
  130. <artifactId>sentry-maven-plugin</artifactId>
  131. <version>${
  132. sourcePackageRegistries?.isLoading
  133. ? t('\u2026loading')
  134. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ?? '0.0.3'
  135. }</version>
  136. <configuration>
  137. <!-- for showing output of sentry-cli -->
  138. <debugSentryCli>true</debugSentryCli>
  139. <!-- download the latest sentry-cli and provide path to it here -->
  140. <!-- download it here: https://github.com/getsentry/sentry-cli/releases -->
  141. <!-- minimum required version is 2.17.3 -->
  142. <sentryCliExecutablePath>/path/to/sentry-cli</sentryCliExecutablePath>
  143. <org>${organizationSlug}</org>
  144. <project>${projectSlug}</project>
  145. <!-- in case you're self hosting, provide the URL here -->
  146. <!--<url>http://localhost:8000/</url>-->
  147. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  148. <!-- you can find it in Sentry UI: Settings > Account > API > Auth Tokens -->
  149. <authToken>env.SENTRY_AUTH_TOKEN</authToken>
  150. </configuration>
  151. <executions>
  152. <execution>
  153. <phase>generate-resources</phase>
  154. <goals>
  155. <goal>uploadSourceBundle</goal>
  156. </goals>
  157. </execution>
  158. </executions>
  159. </plugin>
  160. </plugins>
  161. ...
  162. </build>
  163. `,
  164. },
  165. ],
  166. },
  167. {
  168. description: <h5>{t('SBT')}</h5>,
  169. configurations: [
  170. {
  171. description: <p>{tct('For [strong:SBT]:', {strong: <strong />})}</p>,
  172. language: 'scala',
  173. partialLoading: sourcePackageRegistries?.isLoading,
  174. code: `libraryDependencies += "io.sentry" % "sentry" % "${
  175. sourcePackageRegistries?.isLoading
  176. ? t('\u2026loading')
  177. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  178. }"`,
  179. },
  180. ],
  181. },
  182. ],
  183. additionalInfo: (
  184. <p>
  185. {tct(
  186. 'To upload your source code to Sentry so it can be shown in stack traces, please refer to [link:Manually Uploading Source Context].',
  187. {
  188. link: (
  189. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/" />
  190. ),
  191. }
  192. )}
  193. </p>
  194. ),
  195. },
  196. {
  197. type: StepType.CONFIGURE,
  198. description: t(
  199. "Configure Sentry as soon as possible in your application's lifecycle:"
  200. ),
  201. configurations: [
  202. {
  203. language: 'java',
  204. code: `
  205. import io.sentry.Sentry;
  206. Sentry.init(options -> {
  207. options.setDsn("${dsn}");
  208. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  209. // We recommend adjusting this value in production.
  210. options.setTracesSampleRate(1.0);
  211. // When first trying Sentry it's good to see what the SDK is doing:
  212. options.setDebug(true);
  213. });
  214. `,
  215. },
  216. ],
  217. },
  218. {
  219. type: StepType.VERIFY,
  220. description: (
  221. <p>
  222. {tct(
  223. '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:',
  224. {code: <code />}
  225. )}
  226. </p>
  227. ),
  228. configurations: [
  229. {
  230. language: 'java',
  231. code: `
  232. import java.lang.Exception;
  233. import io.sentry.Sentry;
  234. try {
  235. throw new Exception("This is a test.");
  236. } catch (Exception e) {
  237. Sentry.captureException(e);
  238. }
  239. `,
  240. },
  241. ],
  242. additionalInfo: (
  243. <Fragment>
  244. <p>
  245. {t(
  246. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  247. )}
  248. </p>
  249. <p>
  250. {t(
  251. "If you're an existing user and have disabled alerts, you won't receive this email."
  252. )}
  253. </p>
  254. </Fragment>
  255. ),
  256. },
  257. {
  258. title: t('Measure Performance'),
  259. description: t('You can capture transactions using the SDK. For example:'),
  260. configurations: [
  261. {
  262. language: 'java',
  263. code: `
  264. import io.sentry.ITransaction;
  265. import io.sentry.Sentry;
  266. import io.sentry.SpanStatus;
  267. // A good name for the transaction is key, to help identify what this is about
  268. ITransaction transaction = Sentry.startTransaction("processOrderBatch()", "task");
  269. try {
  270. processOrderBatch();
  271. } catch (Exception e) {
  272. transaction.setThrowable(e);
  273. transaction.setStatus(SpanStatus.INTERNAL_ERROR);
  274. throw e;
  275. } finally {
  276. transaction.finish();
  277. }
  278. `,
  279. },
  280. ],
  281. additionalInfo: (
  282. <p>
  283. {tct(
  284. 'For more information about the API and automatic instrumentations included in the SDK, [link:visit the docs].',
  285. {
  286. link: (
  287. <ExternalLink href="https://docs.sentry.io/platforms/java/performance/" />
  288. ),
  289. }
  290. )}
  291. </p>
  292. ),
  293. },
  294. ];
  295. // Configuration End
  296. export function GettingStartedWithJava({
  297. dsn,
  298. sourcePackageRegistries,
  299. projectSlug,
  300. organization,
  301. ...props
  302. }: ModuleProps) {
  303. return (
  304. <Layout
  305. steps={steps({
  306. dsn,
  307. sourcePackageRegistries,
  308. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  309. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  310. })}
  311. introduction={introduction}
  312. {...props}
  313. />
  314. );
  315. }
  316. export default GettingStartedWithJava;