spring.tsx 12 KB

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