kotlin.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 {PlatformOption} from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {useUrlPlatformOptions} from 'sentry/components/onboarding/platformOptionsControl';
  9. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  10. import {t, tct} from 'sentry/locale';
  11. export enum PackageManager {
  12. GRADLE = 'gradle',
  13. MAVEN = 'maven',
  14. }
  15. type PlaformOptionKey = 'packageManager';
  16. interface StepsParams {
  17. dsn: string;
  18. hasPerformance: boolean;
  19. packageManager: PackageManager;
  20. organizationSlug?: string;
  21. projectSlug?: string;
  22. sourcePackageRegistries?: ModuleProps['sourcePackageRegistries'];
  23. }
  24. // Configuration Start
  25. const packageManagerName: Record<PackageManager, string> = {
  26. [PackageManager.GRADLE]: 'Gradle',
  27. [PackageManager.MAVEN]: 'Maven',
  28. };
  29. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  30. packageManager: {
  31. label: t('Package Manager'),
  32. items: [
  33. {
  34. label: t('Gradle'),
  35. value: PackageManager.GRADLE,
  36. },
  37. {
  38. label: t('Maven'),
  39. value: PackageManager.MAVEN,
  40. },
  41. ],
  42. },
  43. };
  44. const introduction = (
  45. <p>
  46. {tct(
  47. "Sentry supports Kotlin for both JVM and Android. This wizard guides you through set up in the JVM scenario. If you're interested in [strong:Android], head over to the [gettingStartedWithAndroidLink:Getting Started] for that SDK instead. At its core, Sentry for Java provides a raw client for sending events to Sentry. If you use [strong2:Spring Boot, Spring, Logback, JUL, or Log4j2], head over to our [gettingStartedWithJavaLink:Getting Started for Sentry Java].",
  48. {
  49. gettingStartedWithAndroidLink: (
  50. <ExternalLink href="https://docs.sentry.io/platforms/android/" />
  51. ),
  52. gettingStartedWithJavaLink: (
  53. <ExternalLink href="https://docs.sentry.io/platforms/java/" />
  54. ),
  55. strong: <strong />,
  56. strong2: <strong />,
  57. }
  58. )}
  59. </p>
  60. );
  61. export const steps = ({
  62. dsn,
  63. sourcePackageRegistries,
  64. hasPerformance,
  65. packageManager,
  66. projectSlug,
  67. organizationSlug,
  68. }: StepsParams): LayoutProps['steps'] => [
  69. {
  70. type: StepType.INSTALL,
  71. description: t(`Install the SDK via %s:`, packageManagerName[packageManager]),
  72. configurations: [
  73. {
  74. description: (
  75. <p>
  76. {tct(
  77. '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.',
  78. {
  79. link: <Link to="/settings/auth-tokens/" />,
  80. }
  81. )}
  82. </p>
  83. ),
  84. language: 'bash',
  85. code: `SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___`,
  86. },
  87. ...(packageManager === PackageManager.GRADLE
  88. ? [
  89. {
  90. language: 'groovy',
  91. partialLoading: sourcePackageRegistries?.isLoading,
  92. description: (
  93. <p>
  94. {tct(
  95. '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:',
  96. {
  97. code: <code />,
  98. link: (
  99. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  100. ),
  101. }
  102. )}
  103. </p>
  104. ),
  105. code: `
  106. buildscript {
  107. repositories {
  108. mavenCentral()
  109. }
  110. }
  111. plugins {
  112. id "io.sentry.jvm.gradle" version "${
  113. sourcePackageRegistries?.isLoading
  114. ? t('\u2026loading')
  115. : sourcePackageRegistries?.data?.['sentry.java.android.gradle-plugin']?.version ??
  116. '3.12.0'
  117. }"
  118. }
  119. sentry {
  120. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  121. // This enables source context, allowing you to see your source
  122. // code as part of your stack traces in Sentry.
  123. includeSourceContext = true
  124. org = "${organizationSlug}"
  125. projectName = "${projectSlug}"
  126. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  127. }
  128. `,
  129. },
  130. ]
  131. : []),
  132. ...(packageManager === PackageManager.MAVEN
  133. ? [
  134. {
  135. description: (
  136. <p>
  137. {tct('Add the Sentry SDK to your [code:pom.xml] file:', {
  138. code: <code />,
  139. })}
  140. </p>
  141. ),
  142. configurations: [
  143. {
  144. language: 'xml',
  145. partialLoading: sourcePackageRegistries?.isLoading,
  146. code: `
  147. <dependency>
  148. <groupId>io.sentry</groupId>
  149. <artifactId>sentry</artifactId>
  150. <version>${
  151. sourcePackageRegistries?.isLoading
  152. ? t('\u2026loading')
  153. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.27.0'
  154. }</version>
  155. </dependency>
  156. `,
  157. },
  158. {
  159. language: 'xml',
  160. partialLoading: sourcePackageRegistries?.isLoading,
  161. description: t(
  162. 'To upload your source code to Sentry so it can be shown in stack traces, use our Maven plugin.'
  163. ),
  164. code: `
  165. <build>
  166. <plugins>
  167. <plugin>
  168. <groupId>io.sentry</groupId>
  169. <artifactId>sentry-maven-plugin</artifactId>
  170. <version>${
  171. sourcePackageRegistries?.isLoading
  172. ? t('\u2026loading')
  173. : sourcePackageRegistries?.data?.['sentry.java.mavenplugin']?.version ?? '0.0.4'
  174. }</version>
  175. <configuration>
  176. <!-- for showing output of sentry-cli -->
  177. <debugSentryCli>true</debugSentryCli>
  178. <org>${organizationSlug}</org>
  179. <project>${projectSlug}</project>
  180. <!-- in case you're self hosting, provide the URL here -->
  181. <!--<url>http://localhost:8000/</url>-->
  182. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  183. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  184. </configuration>
  185. <executions>
  186. <execution>
  187. <phase>generate-resources</phase>
  188. <goals>
  189. <goal>uploadSourceBundle</goal>
  190. </goals>
  191. </execution>
  192. </executions>
  193. </plugin>
  194. </plugins>
  195. ...
  196. </build>
  197. `,
  198. },
  199. ],
  200. },
  201. ]
  202. : []),
  203. ],
  204. additionalInfo: (
  205. <p>
  206. {tct(
  207. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  208. {
  209. link: (
  210. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  211. ),
  212. }
  213. )}
  214. </p>
  215. ),
  216. },
  217. {
  218. type: StepType.CONFIGURE,
  219. description: t(
  220. "Configure Sentry as soon as possible in your application's lifecycle:"
  221. ),
  222. configurations: [
  223. {
  224. language: 'kotlin',
  225. code: `
  226. import io.sentry.Sentry
  227. Sentry.init { options ->
  228. options.dsn = "${dsn}"${
  229. hasPerformance
  230. ? `
  231. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  232. // We recommend adjusting this value in production.
  233. options.tracesSampleRate = 1.0`
  234. : ''
  235. }
  236. // When first trying Sentry it's good to see what the SDK is doing:
  237. options.isDebug = true
  238. }
  239. `,
  240. },
  241. ],
  242. },
  243. {
  244. type: StepType.VERIFY,
  245. description: (
  246. <p>
  247. {tct(
  248. 'Trigger your first event from your development environment by intentionally creating an error with the [code:Sentry#captureException] method, to test that everything is working:',
  249. {code: <code />}
  250. )}
  251. </p>
  252. ),
  253. configurations: [
  254. {
  255. language: 'kotlin',
  256. code: `
  257. import java.lang.Exception
  258. import io.sentry.Sentry
  259. try {
  260. throw Exception("This is a test.")
  261. } catch (e: Exception) {
  262. Sentry.captureException(e)
  263. }`,
  264. additionalInfo: (
  265. <Fragment>
  266. {t(
  267. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  268. )}
  269. <p>
  270. {t(
  271. "If you're an existing user and have disabled alerts, you won't receive this email."
  272. )}
  273. </p>
  274. </Fragment>
  275. ),
  276. },
  277. ],
  278. },
  279. ];
  280. export const nextSteps = [
  281. {
  282. id: 'examples',
  283. name: t('Examples'),
  284. description: t('Check out our sample applications.'),
  285. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  286. },
  287. {
  288. id: 'performance-monitoring',
  289. name: t('Performance Monitoring'),
  290. description: t(
  291. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  292. ),
  293. link: 'https://docs.sentry.io/platforms/java/performance/',
  294. },
  295. ];
  296. // Configuration End
  297. export function GettingStartedWithKotlin({
  298. dsn,
  299. sourcePackageRegistries,
  300. projectSlug,
  301. organization,
  302. activeProductSelection = [],
  303. ...props
  304. }: ModuleProps) {
  305. const optionValues = useUrlPlatformOptions(platformOptions);
  306. const hasPerformance = activeProductSelection.includes(
  307. ProductSolution.PERFORMANCE_MONITORING
  308. );
  309. const nextStepDocs = [...nextSteps];
  310. return (
  311. <Layout
  312. steps={steps({
  313. dsn,
  314. sourcePackageRegistries,
  315. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  316. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  317. packageManager: optionValues.packageManager as PackageManager,
  318. hasPerformance,
  319. })}
  320. introduction={introduction}
  321. platformOptions={platformOptions}
  322. nextSteps={nextStepDocs}
  323. projectSlug={projectSlug}
  324. {...props}
  325. />
  326. );
  327. }
  328. export default GettingStartedWithKotlin;