spring.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Link from 'sentry/components/links/link';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. BasePlatformOptions,
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {feedbackOnboardingCrashApiJava} from 'sentry/gettingStartedDocs/java/java';
  12. import {
  13. feedbackOnboardingJsLoader,
  14. replayOnboardingJsLoader,
  15. } from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  16. import {t, tct} from 'sentry/locale';
  17. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  18. export enum SpringVersion {
  19. V5 = 'v5',
  20. V6 = 'v6',
  21. }
  22. export enum PackageManager {
  23. GRADLE = 'gradle',
  24. MAVEN = 'maven',
  25. }
  26. export enum YesNo {
  27. YES = 'yes',
  28. NO = 'no',
  29. }
  30. const platformOptions = {
  31. springVersion: {
  32. label: t('Spring Version'),
  33. items: [
  34. {
  35. label: t('Spring 6'),
  36. value: SpringVersion.V6,
  37. },
  38. {
  39. label: t('Spring 5'),
  40. value: SpringVersion.V5,
  41. },
  42. ],
  43. },
  44. packageManager: {
  45. label: t('Package Manager'),
  46. items: [
  47. {
  48. label: t('Gradle'),
  49. value: PackageManager.GRADLE,
  50. },
  51. {
  52. label: t('Maven'),
  53. value: PackageManager.MAVEN,
  54. },
  55. ],
  56. },
  57. opentelemetry: {
  58. label: t('OpenTelemetry'),
  59. items: [
  60. {
  61. label: t('With OpenTelemetry'),
  62. value: YesNo.YES,
  63. },
  64. {
  65. label: t('Without OpenTelemetry'),
  66. value: YesNo.NO,
  67. },
  68. ],
  69. },
  70. } satisfies BasePlatformOptions;
  71. type PlatformOptions = typeof platformOptions;
  72. type Params = DocsParams<PlatformOptions>;
  73. const getGradleInstallSnippet = (params: Params) => `
  74. buildscript {
  75. repositories {
  76. mavenCentral()
  77. }
  78. }
  79. plugins {
  80. id "io.sentry.jvm.gradle" version "${getPackageVersion(
  81. params,
  82. 'sentry.java.android.gradle-plugin',
  83. '3.12.0'
  84. )}"
  85. }
  86. sentry {
  87. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  88. // This enables source context, allowing you to see your source
  89. // code as part of your stack traces in Sentry.
  90. includeSourceContext = true
  91. org = "${params.organization.slug}"
  92. projectName = "${params.projectSlug}"
  93. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  94. }`;
  95. const getMavenInstallSnippet = (params: Params) => `
  96. <build>
  97. <plugins>
  98. <plugin>
  99. <groupId>io.sentry</groupId>
  100. <artifactId>sentry-maven-plugin</artifactId>
  101. <version>${
  102. params.sourcePackageRegistries?.isLoading
  103. ? t('\u2026loading')
  104. : params.sourcePackageRegistries?.data?.['sentry.java.maven-plugin']?.version ??
  105. '0.0.4'
  106. }</version>
  107. <extensions>true</extensions>
  108. <configuration>
  109. <!-- for showing output of sentry-cli -->
  110. <debugSentryCli>true</debugSentryCli>
  111. <org>${params.organization.slug}</org>
  112. <project>${params.projectSlug}</project>
  113. <!-- in case you're self hosting, provide the URL here -->
  114. <!--<url>http://localhost:8000/</url>-->
  115. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  116. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  117. </configuration>
  118. <executions>
  119. <execution>
  120. <goals>
  121. <!--
  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. -->
  126. <goal>uploadSourceBundle</goal>
  127. </goals>
  128. </execution>
  129. </executions>
  130. </plugin>
  131. </plugins>
  132. ...
  133. </build>`;
  134. const getOpenTelemetryRunSnippet = (params: Params) => `
  135. SENTRY_AUTO_INIT=false java -javaagent:sentry-opentelemetry-agent-${getPackageVersion(params, 'sentry.java.opentelemetry-agent', '8.0.0')}.jar -jar your-application.jar
  136. `;
  137. const getOpenTelemetryApplicationServerSnippet = (params: Params) => `
  138. JAVA_OPTS="$\{JAVA_OPTS} -javaagent:/somewhere/sentry-opentelemetry-agent-${getPackageVersion(params, 'sentry.java.opentelemetry-agent', '8.0.0')}.jar"
  139. `;
  140. const getJavaConfigSnippet = (params: Params) => `
  141. import io.sentry.spring${
  142. params.platformOptions.springVersion === SpringVersion.V6 ? '.jakarta' : ''
  143. }.EnableSentry;
  144. @EnableSentry(dsn = "${params.dsn.public}")
  145. @Configuration
  146. class SentryConfiguration {
  147. }`;
  148. const getKotlinConfigSnippet = (params: Params) => `
  149. import io.sentry.spring${
  150. params.platformOptions.springVersion === SpringVersion.V6 ? '.jakarta' : ''
  151. }.EnableSentry
  152. import org.springframework.core.Ordered
  153. @EnableSentry(
  154. dsn = "${params.dsn.public}",
  155. exceptionResolverOrder = Ordered.LOWEST_PRECEDENCE
  156. )`;
  157. const getJavaVerifySnippet = () => `
  158. import java.lang.Exception;
  159. import io.sentry.Sentry;
  160. try {
  161. throw new Exception("This is a test.");
  162. } catch (Exception e) {
  163. Sentry.captureException(e);
  164. }`;
  165. const getKotlinVerifySnippet = () => `
  166. import java.lang.Exception
  167. import io.sentry.Sentry
  168. try {
  169. throw Exception("This is a test.")
  170. } catch (e: Exception) {
  171. Sentry.captureException(e)
  172. }`;
  173. const getSentryPropertiesSnippet = (params: Params) =>
  174. `${
  175. params.isPerformanceSelected
  176. ? `
  177. traces-sample-rate=1.0`
  178. : ''
  179. }`;
  180. const onboarding: OnboardingConfig<PlatformOptions> = {
  181. introduction: () =>
  182. tct(
  183. "Sentry's integration with Spring supports Spring Framework 5.1.2 and above. If you're on an older version, use [legacyIntegrationLink:our legacy integration].",
  184. {
  185. legacyIntegrationLink: (
  186. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/spring/legacy/" />
  187. ),
  188. }
  189. ),
  190. install: (params: Params) => [
  191. {
  192. type: StepType.INSTALL,
  193. description: t(
  194. "Install Sentry's integration with Spring using %s:",
  195. params.platformOptions.packageManager === PackageManager.GRADLE
  196. ? 'Gradle'
  197. : 'Maven'
  198. ),
  199. configurations: [
  200. {
  201. description: (
  202. <p>
  203. {tct(
  204. '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.',
  205. {
  206. link: <Link to="/settings/auth-tokens/" />,
  207. }
  208. )}
  209. </p>
  210. ),
  211. language: 'bash',
  212. code: 'SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___',
  213. },
  214. ...(params.platformOptions.packageManager === PackageManager.GRADLE
  215. ? [
  216. {
  217. description: (
  218. <p>
  219. {tct(
  220. '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:',
  221. {
  222. code: <code />,
  223. link: (
  224. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  225. ),
  226. }
  227. )}
  228. </p>
  229. ),
  230. language: 'groovy',
  231. code: getGradleInstallSnippet(params),
  232. },
  233. ]
  234. : []),
  235. ...(params.platformOptions.packageManager === PackageManager.MAVEN
  236. ? [
  237. {
  238. language: 'xml',
  239. description: (
  240. <p>
  241. {tct(
  242. '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:',
  243. {
  244. code: <code />,
  245. link: (
  246. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  247. ),
  248. }
  249. )}
  250. </p>
  251. ),
  252. code: getMavenInstallSnippet(params),
  253. },
  254. ]
  255. : []),
  256. ...(params.platformOptions.opentelemetry === YesNo.YES
  257. ? [
  258. {
  259. description: tct(
  260. "When running your application, please add our [code:sentry-opentelemetry-agent] to the [code:java] command. In case you are using an application server to run your [code:.WAR] file, please add it to the [code:JAVA_OPTS] of your application server. You can download the latest version of the [code:sentry-opentelemetry-agent.jar] from [linkMC:MavenCentral]. It's also available as a [code:ZIP] containing the [code:JAR] used on this page on [linkGH:GitHub].",
  261. {
  262. code: <code />,
  263. linkMC: (
  264. <ExternalLink href="https://search.maven.org/artifact/io.sentry/sentry-opentelemetry-agent" />
  265. ),
  266. linkGH: (
  267. <ExternalLink href="https://github.com/getsentry/sentry-java/releases/" />
  268. ),
  269. }
  270. ),
  271. language: 'bash',
  272. code: getOpenTelemetryRunSnippet(params),
  273. },
  274. ]
  275. : []),
  276. ...(params.platformOptions.opentelemetry === YesNo.YES
  277. ? [
  278. {
  279. description: t(
  280. 'In case of an application server, adding the Agent might look more like the following:'
  281. ),
  282. language: 'bash',
  283. code: getOpenTelemetryApplicationServerSnippet(params),
  284. },
  285. ]
  286. : []),
  287. ],
  288. additionalInfo: (
  289. <p>
  290. {tct(
  291. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  292. {
  293. link: (
  294. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  295. ),
  296. }
  297. )}
  298. </p>
  299. ),
  300. },
  301. ],
  302. configure: (params: Params) => [
  303. {
  304. type: StepType.CONFIGURE,
  305. description: (
  306. <Fragment>
  307. {t("Configure Sentry as soon as possible in your application's lifecycle:")}
  308. <p>
  309. {tct(
  310. 'The [libraryName] library provides an [codeEnableSentry:@EnableSentry] annotation that registers all required Spring beans. [codeEnableSentry:@EnableSentry] can be placed on any class annotated with [configurationLink:@Configuration] including the main entry class in Spring Boot applications annotated with [springBootApplicationLink:@SpringBootApplication].',
  311. {
  312. libraryName: (
  313. <code>
  314. {params.platformOptions.springVersion === SpringVersion.V5
  315. ? 'sentry-spring'
  316. : 'sentry-spring-jakarta'}
  317. </code>
  318. ),
  319. codeEnableSentry: <code />,
  320. configurationLink: (
  321. <ExternalLink href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html" />
  322. ),
  323. springBootApplicationLink: (
  324. <ExternalLink href="https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html" />
  325. ),
  326. }
  327. )}
  328. </p>
  329. </Fragment>
  330. ),
  331. configurations: [
  332. {
  333. description: <h5>{t('Java')}</h5>,
  334. configurations: [
  335. {
  336. language: 'java',
  337. code: [
  338. {
  339. language: 'java',
  340. label: 'Java',
  341. value: 'java',
  342. code: getJavaConfigSnippet(params),
  343. },
  344. {
  345. language: 'kotlin',
  346. label: 'Kotlin',
  347. value: 'kotlin',
  348. code: getKotlinConfigSnippet(params),
  349. },
  350. ],
  351. },
  352. ],
  353. },
  354. ...(params.isPerformanceSelected
  355. ? [
  356. {
  357. type: StepType.CONFIGURE,
  358. description: tct(
  359. 'Add a [code:sentry.properties] file to enable Performance:',
  360. {
  361. code: <code />,
  362. }
  363. ),
  364. configurations: [
  365. {
  366. language: 'java',
  367. code: getSentryPropertiesSnippet(params),
  368. },
  369. ],
  370. },
  371. ]
  372. : []),
  373. ],
  374. },
  375. ],
  376. verify: () => [
  377. {
  378. type: StepType.VERIFY,
  379. description: t(
  380. 'Last, create an intentional error, so you can test that everything is working:'
  381. ),
  382. configurations: [
  383. {
  384. code: [
  385. {
  386. language: 'java',
  387. label: 'Java',
  388. value: 'java',
  389. code: getJavaVerifySnippet(),
  390. },
  391. {
  392. language: 'kotlin',
  393. label: 'Kotlin',
  394. value: 'kotlin',
  395. code: getKotlinVerifySnippet(),
  396. },
  397. ],
  398. },
  399. ],
  400. additionalInfo: (
  401. <Fragment>
  402. <p>
  403. {t(
  404. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  405. )}
  406. </p>
  407. <p>
  408. {t(
  409. "If you're an existing user and have disabled alerts, you won't receive this email."
  410. )}
  411. </p>
  412. </Fragment>
  413. ),
  414. },
  415. ],
  416. nextSteps: () => [
  417. {
  418. id: 'examples',
  419. name: t('Examples'),
  420. description: t('Check out our sample applications.'),
  421. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  422. },
  423. ],
  424. };
  425. const docs: Docs<PlatformOptions> = {
  426. onboarding,
  427. platformOptions,
  428. crashReportOnboarding: feedbackOnboardingCrashApiJava,
  429. replayOnboardingJsLoader,
  430. feedbackOnboardingJsLoader,
  431. };
  432. export default docs;