java.tsx 8.9 KB

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