spring-boot.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 {ProductSolution} from 'sentry/components/onboarding/productSelection';
  12. import {t, tct} from 'sentry/locale';
  13. export enum SpringBootVersion {
  14. V2 = 'v2',
  15. V3 = 'v3',
  16. }
  17. export enum PackageManager {
  18. GRADLE = 'gradle',
  19. MAVEN = 'maven',
  20. }
  21. type PlaformOptionKey = 'springBootVersion' | 'packageManager';
  22. interface StepsParams {
  23. dsn: string;
  24. hasPerformance: boolean;
  25. organizationSlug?: string;
  26. packageManager?: PackageManager;
  27. projectSlug?: string;
  28. sourcePackageRegistries?: ModuleProps['sourcePackageRegistries'];
  29. springBootVersion?: SpringBootVersion;
  30. }
  31. // Configuration Start
  32. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  33. springBootVersion: {
  34. label: t('Spring Boot Version'),
  35. items: [
  36. {
  37. label: t('Spring Boot 3'),
  38. value: SpringBootVersion.V3,
  39. },
  40. {
  41. label: t('Spring Boot 2'),
  42. value: SpringBootVersion.V2,
  43. },
  44. ],
  45. },
  46. packageManager: {
  47. label: t('Package Manager'),
  48. items: [
  49. {
  50. label: t('Gradle'),
  51. value: PackageManager.GRADLE,
  52. },
  53. {
  54. label: t('Maven'),
  55. value: PackageManager.MAVEN,
  56. },
  57. ],
  58. },
  59. };
  60. const introduction = (
  61. <p>
  62. {tct(
  63. "Sentry's integration with [springBootLink:Spring Boot] supports Spring Boot 2.1.0 and above. If you're on an older version, use [legacyIntegrationLink:our legacy integration].",
  64. {
  65. springBootLink: <ExternalLink href="https://spring.io/projects/spring-boot" />,
  66. legacyIntegrationLink: (
  67. <ExternalLink href="https://docs.sentry.io/platforms/java/legacy/spring/" />
  68. ),
  69. }
  70. )}
  71. </p>
  72. );
  73. export const steps = ({
  74. dsn,
  75. sourcePackageRegistries,
  76. projectSlug,
  77. organizationSlug,
  78. springBootVersion,
  79. packageManager,
  80. hasPerformance,
  81. }: StepsParams): LayoutProps['steps'] => [
  82. {
  83. type: StepType.INSTALL,
  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: `
  98. SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
  99. `,
  100. },
  101. packageManager === PackageManager.GRADLE
  102. ? {
  103. description: (
  104. <p>
  105. {tct(
  106. '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:',
  107. {
  108. code: <code />,
  109. link: (
  110. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  111. ),
  112. }
  113. )}
  114. </p>
  115. ),
  116. language: 'groovy',
  117. code: `
  118. buildscript {
  119. repositories {
  120. mavenCentral()
  121. }
  122. }
  123. plugins {
  124. id "io.sentry.jvm.gradle" version "${
  125. sourcePackageRegistries?.isLoading
  126. ? t('\u2026loading')
  127. : sourcePackageRegistries?.data?.['sentry.java.android.gradle-plugin']?.version ??
  128. '3.12.0'
  129. }"
  130. }
  131. sentry {
  132. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  133. // This enables source context, allowing you to see your source
  134. // code as part of your stack traces in Sentry.
  135. includeSourceContext = true
  136. org = "${organizationSlug}"
  137. projectName = "${projectSlug}"
  138. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  139. }
  140. `,
  141. }
  142. : {
  143. description: t('Install using Maven:'),
  144. configurations: [
  145. {
  146. language: 'xml',
  147. partialLoading: sourcePackageRegistries?.isLoading,
  148. code:
  149. springBootVersion === SpringBootVersion.V3
  150. ? `
  151. <dependency>
  152. <groupId>io.sentry</groupId>
  153. <artifactId>sentry-spring-boot-starter-jakarta</artifactId>
  154. <version>${
  155. sourcePackageRegistries?.isLoading
  156. ? t('\u2026loading')
  157. : sourcePackageRegistries?.data?.['sentry.java.spring-boot.jakarta']?.version ??
  158. '6.28.0'
  159. }</version>
  160. </dependency>`
  161. : `
  162. <dependency>
  163. <groupId>io.sentry</groupId>
  164. <artifactId>sentry-spring-boot-starter</artifactId>
  165. <version>${
  166. sourcePackageRegistries?.isLoading
  167. ? t('\u2026loading')
  168. : sourcePackageRegistries?.data?.['sentry.java.spring-boot']?.version ?? '6.28.0'
  169. }</version>
  170. </dependency>`,
  171. additionalInfo: (
  172. <p>
  173. {tct(
  174. 'If you use Logback for logging you may also want to send error logs to Sentry. Add a dependency to the [sentryLogbackCode:sentry-logback] module. Sentry Spring Boot Starter will auto-configure [sentryAppenderCode:SentryAppender].',
  175. {sentryAppenderCode: <code />, sentryLogbackCode: <code />}
  176. )}
  177. </p>
  178. ),
  179. },
  180. {
  181. language: 'xml',
  182. code: `
  183. <dependency>
  184. <groupId>io.sentry</groupId>
  185. <artifactId>sentry-logback</artifactId>
  186. <version>${
  187. sourcePackageRegistries?.isLoading
  188. ? t('\u2026loading')
  189. : sourcePackageRegistries?.data?.['sentry.java.logback']?.version ?? '6.28.0'
  190. }</version>
  191. </dependency>
  192. `,
  193. },
  194. {
  195. language: 'xml',
  196. description: t(
  197. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  198. ),
  199. code: `
  200. <build>
  201. <plugins>
  202. <plugin>
  203. <groupId>io.sentry</groupId>
  204. <artifactId>sentry-maven-plugin</artifactId>
  205. <version>${
  206. sourcePackageRegistries?.isLoading
  207. ? t('\u2026loading')
  208. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ?? '0.0.4'
  209. }</version>
  210. <configuration>
  211. <!-- for showing output of sentry-cli -->
  212. <debugSentryCli>true</debugSentryCli>
  213. <org>${organizationSlug}</org>
  214. <project>${projectSlug}</project>
  215. <!-- in case you're self hosting, provide the URL here -->
  216. <!--<url>http://localhost:8000/</url>-->
  217. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  218. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  219. </configuration>
  220. <executions>
  221. <execution>
  222. <phase>generate-resources</phase>
  223. <goals>
  224. <goal>uploadSourceBundle</goal>
  225. </goals>
  226. </execution>
  227. </executions>
  228. </plugin>
  229. </plugins>
  230. ...
  231. </build>
  232. `,
  233. },
  234. ],
  235. },
  236. ],
  237. },
  238. {
  239. type: StepType.CONFIGURE,
  240. description: (
  241. <p>
  242. {tct(
  243. 'Open up [applicationPropertiesCode:src/main/application.properties] (or [applicationYmlCode:src/main/application.yml]) and configure the DSN, and any other settings you need:',
  244. {
  245. applicationPropertiesCode: <code />,
  246. applicationYmlCode: <code />,
  247. }
  248. )}
  249. </p>
  250. ),
  251. configurations: [
  252. {
  253. code: [
  254. {
  255. label: 'Properties',
  256. value: 'properties',
  257. language: 'properties',
  258. code: `
  259. sentry.dsn=${dsn}${
  260. hasPerformance
  261. ? `
  262. # Set traces-sample-rate to 1.0 to capture 100% of transactions for performance monitoring.
  263. # We recommend adjusting this value in production.
  264. sentry.traces-sample-rate=1.0`
  265. : ''
  266. }`,
  267. },
  268. {
  269. label: 'YAML',
  270. value: 'yaml',
  271. language: 'properties',
  272. code: `
  273. sentry:
  274. dsn: ${dsn}${
  275. hasPerformance
  276. ? `
  277. # Set traces-sample-rate to 1.0 to capture 100% of transactions for performance monitoring.
  278. # We recommend adjusting this value in production.
  279. sentry.traces-sample-rate: 1.0`
  280. : ''
  281. }`,
  282. },
  283. ],
  284. },
  285. ],
  286. },
  287. {
  288. type: StepType.VERIFY,
  289. description: t(
  290. 'Then create an intentional error, so you can test that everything is working using either Java or Kotlin:'
  291. ),
  292. configurations: [
  293. {
  294. code: [
  295. {
  296. label: 'Java',
  297. value: 'java',
  298. language: 'javascript', // TODO: This shouldn't be javascript but because of better formatting we use it for now
  299. code: `
  300. import java.lang.Exception;
  301. import io.sentry.Sentry;
  302. try {
  303. throw new Exception("This is a test.");
  304. } catch (Exception e) {
  305. Sentry.captureException(e);
  306. }`,
  307. },
  308. {
  309. label: 'Kotlin',
  310. value: 'kotlin',
  311. language: 'javascript', // TODO: This shouldn't be javascript but because of better formatting we use it for now
  312. code: `
  313. import java.lang.Exception
  314. import io.sentry.Sentry
  315. try {
  316. throw Exception("This is a test.")
  317. } catch (e: Exception) {
  318. Sentry.captureException(e)
  319. }`,
  320. },
  321. ],
  322. },
  323. ],
  324. additionalInfo: (
  325. <Fragment>
  326. <p>
  327. {t(
  328. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  329. )}
  330. </p>
  331. <p>
  332. {t(
  333. "If you're an existing user and have disabled alerts, you won't receive this email."
  334. )}
  335. </p>
  336. </Fragment>
  337. ),
  338. },
  339. ];
  340. export const nextSteps = [
  341. {
  342. id: 'examples',
  343. name: t('Examples'),
  344. description: t('Check out our sample applications.'),
  345. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  346. },
  347. {
  348. id: 'performance-monitoring',
  349. name: t('Performance Monitoring'),
  350. description: t(
  351. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  352. ),
  353. link: 'https://docs.sentry.io/platforms/java/guides/spring-boot/performance/',
  354. },
  355. ];
  356. // Configuration End
  357. export function GettingStartedWithSpringBoot({
  358. dsn,
  359. sourcePackageRegistries,
  360. projectSlug,
  361. organization,
  362. activeProductSelection = [],
  363. ...props
  364. }: ModuleProps) {
  365. const optionValues = useUrlPlatformOptions(platformOptions);
  366. const hasPerformance = activeProductSelection.includes(
  367. ProductSolution.PERFORMANCE_MONITORING
  368. );
  369. const nextStepDocs = [...nextSteps];
  370. return (
  371. <Layout
  372. steps={steps({
  373. dsn,
  374. sourcePackageRegistries,
  375. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  376. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  377. hasPerformance,
  378. springBootVersion: optionValues.springBootVersion as SpringBootVersion,
  379. packageManager: optionValues.packageManager as PackageManager,
  380. })}
  381. nextSteps={nextStepDocs}
  382. introduction={introduction}
  383. platformOptions={platformOptions}
  384. projectSlug={projectSlug}
  385. {...props}
  386. />
  387. );
  388. }
  389. export default GettingStartedWithSpringBoot;