spring.tsx 11 KB

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