kotlin.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 PlatformOptionKey = '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<PlatformOptionKey, 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. language: 'xml',
  136. partialLoading: sourcePackageRegistries?.isLoading,
  137. description: (
  138. <p>
  139. {tct(
  140. '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:',
  141. {
  142. code: <code />,
  143. link: (
  144. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  145. ),
  146. }
  147. )}
  148. </p>
  149. ),
  150. code: `<build>
  151. <plugins>
  152. <plugin>
  153. <groupId>io.sentry</groupId>
  154. <artifactId>sentry-maven-plugin</artifactId>
  155. <version>${
  156. sourcePackageRegistries?.isLoading
  157. ? t('\u2026loading')
  158. : sourcePackageRegistries?.data?.['sentry.java.maven-plugin']?.version ??
  159. '0.0.4'
  160. }</version>
  161. <extensions>true</extensions>
  162. <configuration>
  163. <!-- for showing output of sentry-cli -->
  164. <debugSentryCli>true</debugSentryCli>
  165. <org>${organizationSlug}</org>
  166. <project>${projectSlug}</project>
  167. <!-- in case you're self hosting, provide the URL here -->
  168. <!--<url>http://localhost:8000/</url>-->
  169. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  170. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  171. </configuration>
  172. <executions>
  173. <execution>
  174. <goals>
  175. <!--
  176. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  177. This enables source context, allowing you to see your source
  178. code as part of your stack traces in Sentry.
  179. -->
  180. <goal>uploadSourceBundle</goal>
  181. </goals>
  182. </execution>
  183. </executions>
  184. </plugin>
  185. </plugins>
  186. ...
  187. </build>`,
  188. },
  189. ]
  190. : []),
  191. ],
  192. additionalInfo: (
  193. <p>
  194. {tct(
  195. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  196. {
  197. link: (
  198. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  199. ),
  200. }
  201. )}
  202. </p>
  203. ),
  204. },
  205. {
  206. type: StepType.CONFIGURE,
  207. description: t(
  208. "Configure Sentry as soon as possible in your application's lifecycle:"
  209. ),
  210. configurations: [
  211. {
  212. language: 'kotlin',
  213. code: `
  214. import io.sentry.Sentry
  215. Sentry.init { options ->
  216. options.dsn = "${dsn}"${
  217. hasPerformance
  218. ? `
  219. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  220. // We recommend adjusting this value in production.
  221. options.tracesSampleRate = 1.0`
  222. : ''
  223. }
  224. // When first trying Sentry it's good to see what the SDK is doing:
  225. options.isDebug = true
  226. }
  227. `,
  228. },
  229. ],
  230. },
  231. {
  232. type: StepType.VERIFY,
  233. description: (
  234. <p>
  235. {tct(
  236. '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:',
  237. {code: <code />}
  238. )}
  239. </p>
  240. ),
  241. configurations: [
  242. {
  243. language: 'kotlin',
  244. code: `
  245. import java.lang.Exception
  246. import io.sentry.Sentry
  247. try {
  248. throw Exception("This is a test.")
  249. } catch (e: Exception) {
  250. Sentry.captureException(e)
  251. }`,
  252. additionalInfo: (
  253. <Fragment>
  254. {t(
  255. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  256. )}
  257. <p>
  258. {t(
  259. "If you're an existing user and have disabled alerts, you won't receive this email."
  260. )}
  261. </p>
  262. </Fragment>
  263. ),
  264. },
  265. ],
  266. },
  267. ];
  268. export const nextSteps = [
  269. {
  270. id: 'examples',
  271. name: t('Examples'),
  272. description: t('Check out our sample applications.'),
  273. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  274. },
  275. {
  276. id: 'performance-monitoring',
  277. name: t('Performance Monitoring'),
  278. description: t(
  279. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  280. ),
  281. link: 'https://docs.sentry.io/platforms/java/performance/',
  282. },
  283. ];
  284. // Configuration End
  285. export function GettingStartedWithKotlin({
  286. dsn,
  287. sourcePackageRegistries,
  288. projectSlug,
  289. organization,
  290. activeProductSelection = [],
  291. ...props
  292. }: ModuleProps) {
  293. const optionValues = useUrlPlatformOptions(platformOptions);
  294. const hasPerformance = activeProductSelection.includes(
  295. ProductSolution.PERFORMANCE_MONITORING
  296. );
  297. const nextStepDocs = [...nextSteps];
  298. return (
  299. <Layout
  300. steps={steps({
  301. dsn,
  302. sourcePackageRegistries,
  303. projectSlug: projectSlug ?? '___PROJECT_SLUG___',
  304. organizationSlug: organization?.slug ?? '___ORG_SLUG___',
  305. packageManager: optionValues.packageManager as PackageManager,
  306. hasPerformance,
  307. })}
  308. introduction={introduction}
  309. platformOptions={platformOptions}
  310. nextSteps={nextStepDocs}
  311. projectSlug={projectSlug}
  312. {...props}
  313. />
  314. );
  315. }
  316. export default GettingStartedWithKotlin;