spring.tsx 12 KB

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