kotlin.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 type {
  6. BasePlatformOptions,
  7. Docs,
  8. DocsParams,
  9. OnboardingConfig,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {
  12. getCrashReportApiIntroduction,
  13. getCrashReportInstallDescription,
  14. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  15. import {t, tct} from 'sentry/locale';
  16. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  17. export enum PackageManager {
  18. GRADLE = 'gradle',
  19. MAVEN = 'maven',
  20. }
  21. const packageManagerName: Record<PackageManager, string> = {
  22. [PackageManager.GRADLE]: 'Gradle',
  23. [PackageManager.MAVEN]: 'Maven',
  24. };
  25. const platformOptions = {
  26. packageManager: {
  27. label: t('Package Manager'),
  28. items: [
  29. {
  30. label: t('Gradle'),
  31. value: PackageManager.GRADLE,
  32. },
  33. {
  34. label: t('Maven'),
  35. value: PackageManager.MAVEN,
  36. },
  37. ],
  38. },
  39. } satisfies BasePlatformOptions;
  40. type PlatformOptions = typeof platformOptions;
  41. type Params = DocsParams<PlatformOptions>;
  42. const getGradleInstallSnippet = (params: Params) => `
  43. buildscript {
  44. repositories {
  45. mavenCentral()
  46. }
  47. }
  48. plugins {
  49. id "io.sentry.jvm.gradle" version "${getPackageVersion(
  50. params,
  51. 'sentry.java.android.gradle-plugin',
  52. '3.12.0'
  53. )}"
  54. }
  55. sentry {
  56. // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  57. // This enables source context, allowing you to see your source
  58. // code as part of your stack traces in Sentry.
  59. includeSourceContext = true
  60. org = "${params.organization.slug}"
  61. projectName = "${params.projectSlug}"
  62. authToken = System.getenv("SENTRY_AUTH_TOKEN")
  63. }`;
  64. const getMavenInstallSnippet = (params: Params) => `
  65. <build>
  66. <plugins>
  67. <plugin>
  68. <groupId>io.sentry</groupId>
  69. <artifactId>sentry-maven-plugin</artifactId>
  70. <version>${getPackageVersion(params, 'sentry.java.maven-plugin', '0.0.4')}</version>
  71. <extensions>true</extensions>
  72. <configuration>
  73. <!-- for showing output of sentry-cli -->
  74. <debugSentryCli>true</debugSentryCli>
  75. <org>${params.organization.slug}</org>
  76. <project>${params.projectSlug}</project>
  77. <!-- in case you're self hosting, provide the URL here -->
  78. <!--<url>http://localhost:8000/</url>-->
  79. <!-- provide your auth token via SENTRY_AUTH_TOKEN environment variable -->
  80. <authToken>\${env.SENTRY_AUTH_TOKEN}</authToken>
  81. </configuration>
  82. <executions>
  83. <execution>
  84. <goals>
  85. <!--
  86. Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
  87. This enables source context, allowing you to see your source
  88. code as part of your stack traces in Sentry.
  89. -->
  90. <goal>uploadSourceBundle</goal>
  91. </goals>
  92. </execution>
  93. </executions>
  94. </plugin>
  95. </plugins>
  96. ...
  97. </build>`;
  98. const getConfigureSnippet = (params: Params) => `
  99. import io.sentry.Sentry
  100. Sentry.init { options ->
  101. options.dsn = "${params.dsn}"${
  102. params.isPerformanceSelected
  103. ? `
  104. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  105. // We recommend adjusting this value in production.
  106. options.tracesSampleRate = 1.0`
  107. : ''
  108. }
  109. // When first trying Sentry it's good to see what the SDK is doing:
  110. options.isDebug = true
  111. }`;
  112. const getVerifySnippet = () => `
  113. import java.lang.Exception
  114. import io.sentry.Sentry
  115. try {
  116. throw Exception("This is a test.")
  117. } catch (e: Exception) {
  118. Sentry.captureException(e)
  119. }`;
  120. const onboarding: OnboardingConfig<PlatformOptions> = {
  121. introduction: () =>
  122. tct(
  123. "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].",
  124. {
  125. gettingStartedWithAndroidLink: (
  126. <ExternalLink href="https://docs.sentry.io/platforms/android/" />
  127. ),
  128. gettingStartedWithJavaLink: (
  129. <ExternalLink href="https://docs.sentry.io/platforms/java/" />
  130. ),
  131. strong: <strong />,
  132. strong2: <strong />,
  133. }
  134. ),
  135. install: params => [
  136. {
  137. type: StepType.INSTALL,
  138. description: t(
  139. `Install the SDK via %s:`,
  140. packageManagerName[params.platformOptions.packageManager]
  141. ),
  142. configurations: [
  143. {
  144. description: tct(
  145. '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.',
  146. {
  147. link: <Link to="/settings/auth-tokens/" />,
  148. }
  149. ),
  150. language: 'bash',
  151. code: `SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___`,
  152. },
  153. ...(params.platformOptions.packageManager === PackageManager.GRADLE
  154. ? [
  155. {
  156. language: 'groovy',
  157. partialLoading: params.sourcePackageRegistries.isLoading,
  158. description: tct(
  159. '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:',
  160. {
  161. code: <code />,
  162. link: (
  163. <ExternalLink href="https://github.com/getsentry/sentry-android-gradle-plugin" />
  164. ),
  165. }
  166. ),
  167. code: getGradleInstallSnippet(params),
  168. },
  169. ]
  170. : []),
  171. ...(params.platformOptions.packageManager === PackageManager.MAVEN
  172. ? [
  173. {
  174. language: 'xml',
  175. partialLoading: params.sourcePackageRegistries.isLoading,
  176. description: tct(
  177. '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:',
  178. {
  179. code: <code />,
  180. link: (
  181. <ExternalLink href="https://github.com/getsentry/sentry-maven-plugin" />
  182. ),
  183. }
  184. ),
  185. code: getMavenInstallSnippet(params),
  186. },
  187. ]
  188. : []),
  189. ],
  190. additionalInfo: tct(
  191. 'If you prefer to manually upload your source code to Sentry, please refer to [link:Manually Uploading Source Context].',
  192. {
  193. link: (
  194. <ExternalLink href="https://docs.sentry.io/platforms/java/source-context/#manually-uploading-source-context" />
  195. ),
  196. }
  197. ),
  198. },
  199. ],
  200. configure: params => [
  201. {
  202. type: StepType.CONFIGURE,
  203. description: t(
  204. "Configure Sentry as soon as possible in your application's lifecycle:"
  205. ),
  206. configurations: [
  207. {
  208. language: 'kotlin',
  209. code: getConfigureSnippet(params),
  210. },
  211. ],
  212. },
  213. ],
  214. verify: () => [
  215. {
  216. type: StepType.VERIFY,
  217. description: tct(
  218. '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:',
  219. {code: <code />}
  220. ),
  221. configurations: [
  222. {
  223. language: 'kotlin',
  224. code: getVerifySnippet(),
  225. additionalInfo: (
  226. <Fragment>
  227. {t(
  228. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  229. )}
  230. <p>
  231. {t(
  232. "If you're an existing user and have disabled alerts, you won't receive this email."
  233. )}
  234. </p>
  235. </Fragment>
  236. ),
  237. },
  238. ],
  239. },
  240. ],
  241. nextSteps: () => [
  242. {
  243. id: 'examples',
  244. name: t('Examples'),
  245. description: t('Check out our sample applications.'),
  246. link: 'https://github.com/getsentry/sentry-java/tree/main/sentry-samples',
  247. },
  248. {
  249. id: 'performance-monitoring',
  250. name: t('Performance Monitoring'),
  251. description: t(
  252. 'Stay ahead of latency issues and trace every slow transaction to a poor-performing API call or database query.'
  253. ),
  254. link: 'https://docs.sentry.io/platforms/java/performance/',
  255. },
  256. ],
  257. };
  258. const feedbackOnboardingCrashApi: OnboardingConfig = {
  259. introduction: () => getCrashReportApiIntroduction(),
  260. install: () => [
  261. {
  262. type: StepType.INSTALL,
  263. description: getCrashReportInstallDescription(),
  264. configurations: [
  265. {
  266. code: [
  267. {
  268. label: 'Kotlin',
  269. value: 'kotlin',
  270. language: 'kotlin',
  271. code: `import io.sentry.kotlin.multiplatform.Sentry
  272. import io.sentry.kotlin.multiplatform.protocol.UserFeedback
  273. val sentryId = Sentry.captureMessage("My message")
  274. val userFeedback = UserFeedback(sentryId).apply {
  275. comments = "It broke."
  276. email = "john.doe@example.com"
  277. name = "John Doe"
  278. }
  279. Sentry.captureUserFeedback(userFeedback)`,
  280. },
  281. ],
  282. },
  283. ],
  284. },
  285. ],
  286. configure: () => [],
  287. verify: () => [],
  288. nextSteps: () => [],
  289. };
  290. const docs: Docs<PlatformOptions> = {
  291. platformOptions,
  292. feedbackOnboardingCrashApi,
  293. onboarding,
  294. };
  295. export default docs;