spring-boot.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. language: 'properties',
  254. description: (
  255. <p>{tct('Modify [code:src/main/application.properties]:', {code: <code />})}</p>
  256. ),
  257. code: `
  258. sentry.dsn=${dsn}${
  259. hasPerformance
  260. ? `
  261. # Set traces-sample-rate to 1.0 to capture 100% of transactions for performance monitoring.
  262. # We recommend adjusting this value in production.
  263. sentry.traces-sample-rate=1.0`
  264. : ''
  265. }`,
  266. },
  267. {
  268. language: 'properties',
  269. description: (
  270. <p>{tct('Or, modify [code:src/main/application.yml]:', {code: <code />})}</p>
  271. ),
  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. type: StepType.VERIFY,
  287. description: t(
  288. 'Then create an intentional error, so you can test that everything is working using either Java or Kotlin:'
  289. ),
  290. configurations: [
  291. {
  292. description: <h5>Java</h5>,
  293. language: 'javascript', // TODO: This shouldn't be javascript but because of better formatting we use it for now
  294. code: `
  295. import java.lang.Exception;
  296. import io.sentry.Sentry;
  297. try {
  298. throw new Exception("This is a test.");
  299. } catch (Exception e) {
  300. Sentry.captureException(e);
  301. }
  302. `,
  303. },
  304. {
  305. description: <h5>Kotlin</h5>,
  306. language: 'javascript', // TODO: This shouldn't be javascript but because of better formatting we use it for now
  307. code: `
  308. import java.lang.Exception
  309. import io.sentry.Sentry
  310. try {
  311. throw Exception("This is a test.")
  312. } catch (e: Exception) {
  313. Sentry.captureException(e)
  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. export const 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-boot/performance/',
  348. },
  349. ];
  350. // Configuration End
  351. export function GettingStartedWithSpringBoot({
  352. dsn,
  353. sourcePackageRegistries,
  354. projectSlug,
  355. organization,
  356. activeProductSelection = [],
  357. ...props
  358. }: ModuleProps) {
  359. const optionValues = useUrlPlatformOptions(platformOptions);
  360. const hasPerformance = activeProductSelection.includes(
  361. ProductSolution.PERFORMANCE_MONITORING
  362. );
  363. const nextStepDocs = [...nextSteps];
  364. return (
  365. <Layout
  366. steps={steps({
  367. dsn,
  368. sourcePackageRegistries,
  369. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  370. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  371. hasPerformance,
  372. springBootVersion: optionValues.springBootVersion as SpringBootVersion,
  373. packageManager: optionValues.packageManager as PackageManager,
  374. })}
  375. nextSteps={nextStepDocs}
  376. introduction={introduction}
  377. platformOptions={platformOptions}
  378. {...props}
  379. />
  380. );
  381. }
  382. export default GettingStartedWithSpringBoot;