spring.tsx 11 KB

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