spring.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 {t, tct} from 'sentry/locale';
  12. export enum SpringVersion {
  13. V5 = 'v5',
  14. V6 = 'v6',
  15. }
  16. export enum PackageManager {
  17. GRADLE = 'gradle',
  18. MAVEN = 'maven',
  19. }
  20. type PlaformOptionKey = 'springVersion' | 'packageManager';
  21. interface StepsParams {
  22. dsn: string;
  23. packageManager: PackageManager;
  24. springVersion: SpringVersion;
  25. organizationSlug?: string;
  26. projectSlug?: string;
  27. sourcePackageRegistries?: ModuleProps['sourcePackageRegistries'];
  28. }
  29. // Configuration Start
  30. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  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. };
  58. const introduction = (
  59. <p>
  60. {tct(
  61. "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].",
  62. {
  63. legacyIntegrationLink: (
  64. <ExternalLink href="https://docs.sentry.io/platforms/java/guides/spring/legacy/" />
  65. ),
  66. }
  67. )}
  68. </p>
  69. );
  70. export const steps = ({
  71. dsn,
  72. sourcePackageRegistries,
  73. projectSlug,
  74. organizationSlug,
  75. packageManager,
  76. springVersion,
  77. }: StepsParams): LayoutProps['steps'] => [
  78. {
  79. type: StepType.INSTALL,
  80. description: t(
  81. "Install Sentry's integration with Spring using %s:",
  82. packageManager === PackageManager.GRADLE ? 'Gradle' : 'Maven'
  83. ),
  84. configurations: [
  85. {
  86. description: (
  87. <p>
  88. {tct(
  89. '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.',
  90. {
  91. link: <Link to="/settings/auth-tokens/" />,
  92. }
  93. )}
  94. </p>
  95. ),
  96. language: 'bash',
  97. code: 'SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___',
  98. },
  99. ...(packageManager === PackageManager.GRADLE
  100. ? [
  101. {
  102. description: (
  103. <p>
  104. {tct(
  105. '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:',
  106. {
  107. code: <code />,
  108. link: (
  109. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  110. ),
  111. }
  112. )}
  113. </p>
  114. ),
  115. language: 'groovy',
  116. code: `
  117. buildscript {
  118. repositories {
  119. mavenCentral()
  120. }
  121. }
  122. plugins {
  123. id "io.sentry.jvm.gradle" version "${
  124. sourcePackageRegistries?.isLoading
  125. ? t('\u2026loading')
  126. : sourcePackageRegistries?.data?.['sentry.java.android.gradle-plugin']?.version ??
  127. '3.12.0'
  128. }"
  129. }
  130. sentry {
  131. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  132. // This enables source context, allowing you to see your source
  133. // code as part of your stack traces in Sentry.
  134. includeSourceContext = true
  135. org = "${organizationSlug}"
  136. projectName = "${projectSlug}"
  137. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  138. }
  139. `,
  140. },
  141. ]
  142. : []),
  143. ...(packageManager === PackageManager.MAVEN
  144. ? [
  145. {
  146. description: t("Add the Sentry SDK to your project's dependencies."),
  147. language: 'xml',
  148. partialLoading: sourcePackageRegistries?.isLoading,
  149. code:
  150. springVersion === SpringVersion.V5
  151. ? `
  152. <dependency>
  153. <groupId>io.sentry</groupId>
  154. <artifactId>sentry-spring</artifactId>
  155. <version>${
  156. sourcePackageRegistries?.isLoading
  157. ? t('\u2026loading')
  158. : sourcePackageRegistries?.data?.['sentry.java.spring']?.version ?? '6.28.0'
  159. }</version>
  160. </dependency>`
  161. : `
  162. <dependency>
  163. <groupId>io.sentry</groupId>
  164. <artifactId>sentry-spring-jakarta</artifactId>
  165. <version>${
  166. sourcePackageRegistries?.isLoading
  167. ? t('\u2026loading')
  168. : sourcePackageRegistries?.data?.['sentry.java.spring.jakarta']?.version ??
  169. '6.28.0'
  170. }</version>
  171. </dependency>`,
  172. },
  173. {
  174. language: 'xml',
  175. partialLoading: sourcePackageRegistries?.isLoading,
  176. description: t(
  177. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  178. ),
  179. code: `
  180. <build>
  181. <plugins>
  182. <plugin>
  183. <groupId>io.sentry</groupId>
  184. <artifactId>sentry-maven-plugin</artifactId>
  185. <version>${
  186. sourcePackageRegistries?.isLoading
  187. ? t('\u2026loading')
  188. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ??
  189. '0.0.4'
  190. }</version>
  191. <configuration>
  192. <!-- for showing output of sentry-cli -->
  193. <debugSentryCli>true</debugSentryCli>
  194. <org>${organizationSlug}</org>
  195. <project>${projectSlug}</project>
  196. <!-- in case you're self hosting, provide the URL here -->
  197. <!--<url>http://localhost:8000/</url>-->
  198. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  199. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  200. </configuration>
  201. <executions>
  202. <execution>
  203. <phase>generate-resources</phase>
  204. <goals>
  205. <goal>uploadSourceBundle</goal>
  206. </goals>
  207. </execution>
  208. </executions>
  209. </plugin>
  210. </plugins>
  211. ...`,
  212. },
  213. ]
  214. : []),
  215. ],
  216. },
  217. {
  218. type: StepType.CONFIGURE,
  219. description: (
  220. <Fragment>
  221. {t("Configure Sentry as soon as possible in your application's lifecycle:")}
  222. <p>
  223. {tct(
  224. '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].',
  225. {
  226. libraryName: (
  227. <code>
  228. {springVersion === SpringVersion.V5
  229. ? 'sentry-spring'
  230. : 'sentry-spring-jakarta'}
  231. </code>
  232. ),
  233. codeEnableSentry: <code />,
  234. configurationLink: (
  235. <ExternalLink href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html" />
  236. ),
  237. springBootApplicationLink: (
  238. <ExternalLink href="https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html" />
  239. ),
  240. }
  241. )}
  242. </p>
  243. </Fragment>
  244. ),
  245. configurations: [
  246. {
  247. description: <h5>{t('Java')}</h5>,
  248. configurations: [
  249. {
  250. language: 'java',
  251. code: [
  252. {
  253. language: 'java',
  254. label: 'Java',
  255. value: 'java',
  256. code: `
  257. import io.sentry.spring${
  258. springVersion === SpringVersion.V6 ? '.jakarta' : ''
  259. }.EnableSentry;
  260. @EnableSentry(dsn = "${dsn}")
  261. @Configuration
  262. class SentryConfiguration {
  263. }`,
  264. },
  265. {
  266. language: 'java',
  267. label: 'Kotlin',
  268. value: 'kotlin',
  269. code: `
  270. import io.sentry.spring${
  271. springVersion === SpringVersion.V6 ? '.jakarta' : ''
  272. }.EnableSentry
  273. import org.springframework.core.Ordered
  274. @EnableSentry(
  275. dsn = "${dsn}",
  276. exceptionResolverOrder = Ordered.LOWEST_PRECEDENCE
  277. )`,
  278. },
  279. ],
  280. },
  281. ],
  282. },
  283. ],
  284. },
  285. {
  286. type: StepType.VERIFY,
  287. description: t(
  288. 'Last, create an intentional error, so you can test that everything is working:'
  289. ),
  290. configurations: [
  291. {
  292. code: [
  293. {
  294. language: 'java',
  295. label: 'Java',
  296. value: 'java',
  297. code: `
  298. import java.lang.Exception;
  299. import io.sentry.Sentry;
  300. try {
  301. throw new Exception("This is a test.");
  302. } catch (Exception e) {
  303. Sentry.captureException(e);
  304. }`,
  305. },
  306. {
  307. language: 'java',
  308. label: 'Kotlin',
  309. value: 'kotlin',
  310. code: `
  311. import java.lang.Exception
  312. import io.sentry.Sentry
  313. try {
  314. throw Exception("This is a test.")
  315. } catch (e: Exception) {
  316. Sentry.captureException(e)
  317. }`,
  318. },
  319. ],
  320. },
  321. ],
  322. additionalInfo: (
  323. <Fragment>
  324. <p>
  325. {t(
  326. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  327. )}
  328. </p>
  329. <p>
  330. {t(
  331. "If you're an existing user and have disabled alerts, you won't receive this email."
  332. )}
  333. </p>
  334. </Fragment>
  335. ),
  336. },
  337. {
  338. title: t('Other build tools'),
  339. additionalInfo: (
  340. <p>
  341. {tct(
  342. 'For other dependency managers see the [mavenRepositorySpringLink:central Maven repository].',
  343. {
  344. mavenRepositorySpringLink:
  345. springVersion === SpringVersion.V5 ? (
  346. <ExternalLink
  347. href={`https://central.sonatype.com/artifact/io.sentry/sentry-spring/${
  348. sourcePackageRegistries?.data?.['sentry.java.spring']?.version ??
  349. '6.28.0'
  350. }`}
  351. />
  352. ) : (
  353. <ExternalLink
  354. href={`https://central.sonatype.com/artifact/io.sentry/sentry-spring-jakarta/${
  355. sourcePackageRegistries?.data?.['sentry.java.spring.jakarta']
  356. ?.version ?? '6.28.0'
  357. }`}
  358. />
  359. ),
  360. }
  361. )}
  362. </p>
  363. ),
  364. },
  365. ];
  366. export const nextSteps = [
  367. {
  368. id: 'examples',
  369. name: t('Examples'),
  370. description: t('Check out our sample applications.'),
  371. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  372. },
  373. {
  374. id: 'performance-monitoring',
  375. name: t('Performance Monitoring'),
  376. description: t(
  377. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  378. ),
  379. link: 'https://docs.sentry.io/platforms/java/guides/spring/performance/',
  380. },
  381. ];
  382. // Configuration End
  383. export function GettingStartedWithSpring({
  384. dsn,
  385. sourcePackageRegistries,
  386. projectSlug,
  387. organization,
  388. ...props
  389. }: ModuleProps) {
  390. const optionValues = useUrlPlatformOptions(platformOptions);
  391. const nextStepDocs = [...nextSteps];
  392. return (
  393. <Layout
  394. steps={steps({
  395. dsn,
  396. sourcePackageRegistries,
  397. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  398. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  399. springVersion: optionValues.springVersion as SpringVersion,
  400. packageManager: optionValues.packageManager as PackageManager,
  401. })}
  402. nextSteps={nextStepDocs}
  403. introduction={introduction}
  404. platformOptions={platformOptions}
  405. projectSlug={projectSlug}
  406. {...props}
  407. />
  408. );
  409. }
  410. export default GettingStartedWithSpring;