kotlin.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  4. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  5. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  6. import {
  7. PlatformOption,
  8. useUrlPlatformOptions,
  9. } from 'sentry/components/onboarding/platformOptionsControl';
  10. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  11. import {t, tct} from 'sentry/locale';
  12. export enum PackageManager {
  13. GRADLE = 'gradle',
  14. MAVEN = 'maven',
  15. }
  16. type PlaformOptionKey = 'packageManager';
  17. interface StepsParams {
  18. dsn: string;
  19. hasPerformance: boolean;
  20. packageManager: PackageManager;
  21. sourcePackageRegistries?: ModuleProps['sourcePackageRegistries'];
  22. }
  23. // Configuration Start
  24. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  25. packageManager: {
  26. label: t('Package Manager'),
  27. items: [
  28. {
  29. label: t('Gradle'),
  30. value: PackageManager.GRADLE,
  31. },
  32. {
  33. label: t('Maven'),
  34. value: PackageManager.MAVEN,
  35. },
  36. ],
  37. },
  38. };
  39. const introduction = (
  40. <p>
  41. {tct(
  42. "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 [strong:Spring Boot, Spring, Logback, JUL, or Log4j2], head over to our [gettingStartedWithJavaLink:Getting Started for Sentry Java].",
  43. {
  44. gettingStartedWithAndroidLink: (
  45. <ExternalLink href="https://docs.sentry.io/platforms/android/" />
  46. ),
  47. gettingStartedWithJavaLink: (
  48. <ExternalLink href="https://docs.sentry.io/platforms/java/" />
  49. ),
  50. strong: <strong />,
  51. }
  52. )}
  53. </p>
  54. );
  55. export const steps = ({
  56. dsn,
  57. sourcePackageRegistries,
  58. hasPerformance,
  59. packageManager,
  60. }: StepsParams): LayoutProps['steps'] => [
  61. {
  62. type: StepType.INSTALL,
  63. configurations:
  64. packageManager === PackageManager.GRADLE
  65. ? [
  66. {
  67. language: 'groovy',
  68. description: (
  69. <p>
  70. {tct('Add the Sentry SDK to your [code:build.gradle] file:', {
  71. code: <code />,
  72. })}
  73. </p>
  74. ),
  75. partialLoading: sourcePackageRegistries?.isLoading,
  76. code: `
  77. // Make sure mavenCentral is there.
  78. repositories {
  79. mavenCentral()
  80. }
  81. dependencies {
  82. implementation 'io.sentry:sentry:${
  83. sourcePackageRegistries?.isLoading
  84. ? t('\u2026loading')
  85. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '4.0.0'
  86. }'
  87. }
  88. `,
  89. },
  90. ]
  91. : [
  92. {
  93. language: 'xml',
  94. partialLoading: sourcePackageRegistries?.isLoading,
  95. description: (
  96. <p>
  97. {tct('Add the Sentry SDK to your [code:pom.xml] file:', {
  98. code: <code />,
  99. })}
  100. </p>
  101. ),
  102. code: `
  103. <dependency>
  104. <groupId>io.sentry</groupId>
  105. <artifactId>sentry</artifactId>
  106. <version>${
  107. sourcePackageRegistries?.isLoading
  108. ? t('\u2026loading')
  109. : sourcePackageRegistries?.data?.['sentry.java']?.version ?? '6.25.0'
  110. }</version>
  111. </dependency>
  112. `,
  113. },
  114. ],
  115. },
  116. {
  117. type: StepType.CONFIGURE,
  118. description: t(
  119. "Configure Sentry as soon as possible in your application's lifecycle:"
  120. ),
  121. configurations: [
  122. {
  123. language: 'kotlin',
  124. code: `
  125. import io.sentry.Sentry
  126. Sentry.init { options ->
  127. options.dsn = "${dsn}"${
  128. hasPerformance
  129. ? `
  130. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  131. // We recommend adjusting this value in production.
  132. options.tracesSampleRate = 1.0`
  133. : ''
  134. }
  135. // When first trying Sentry it's good to see what the SDK is doing:
  136. options.isDebug = true
  137. }
  138. `,
  139. },
  140. ],
  141. },
  142. {
  143. type: StepType.VERIFY,
  144. description: (
  145. <p>
  146. {tct(
  147. '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:',
  148. {code: <code />}
  149. )}
  150. </p>
  151. ),
  152. configurations: [
  153. {
  154. language: 'kotlin',
  155. code: `
  156. import java.lang.Exception
  157. import io.sentry.Sentry
  158. try {
  159. throw Exception("This is a test.")
  160. } catch (e: Exception) {
  161. Sentry.captureException(e)
  162. }`,
  163. additionalInfo: (
  164. <Fragment>
  165. {t(
  166. "If you're new to Sentry, use the email alert to access your account and complete a product tour."
  167. )}
  168. <p>
  169. {t(
  170. "If you're an existing user and have disabled alerts, you won't receive this email."
  171. )}
  172. </p>
  173. </Fragment>
  174. ),
  175. },
  176. ...(hasPerformance
  177. ? [
  178. {
  179. description: <h5>{t('Measure Performance')}</h5>,
  180. configurations: [
  181. {
  182. description: t(
  183. 'You can capture transactions using the SDK. For example:'
  184. ),
  185. language: 'kotlin',
  186. code: `
  187. import io.sentry.Sentry
  188. import io.sentry.SpanStatus
  189. // A good name for the transaction is key, to help identify what this is about
  190. val transaction = Sentry.startTransaction("processOrderBatch()", "task")
  191. try {
  192. processOrderBatch()
  193. } catch (e: Exception) {
  194. transaction.throwable = e
  195. transaction.status = SpanStatus.INTERNAL_ERROR
  196. throw e
  197. } finally {
  198. transaction.finish();
  199. }`,
  200. additionalInfo: (
  201. <p>
  202. {tct(
  203. 'For more information about the API and automatic instrumentations included in the SDK, visit the docs.',
  204. {
  205. docsLink: (
  206. <ExternalLink href="https://docs.sentry.io/platforms/java/performance/" />
  207. ),
  208. }
  209. )}
  210. </p>
  211. ),
  212. },
  213. ],
  214. },
  215. ]
  216. : []),
  217. ],
  218. },
  219. ];
  220. // Configuration End
  221. export function GettingStartedWithKotlin({
  222. dsn,
  223. sourcePackageRegistries,
  224. activeProductSelection = [],
  225. ...props
  226. }: ModuleProps) {
  227. const optionValues = useUrlPlatformOptions(platformOptions);
  228. const hasPerformance = activeProductSelection.includes(
  229. ProductSolution.PERFORMANCE_MONITORING
  230. );
  231. return (
  232. <Layout
  233. steps={steps({
  234. dsn,
  235. sourcePackageRegistries,
  236. packageManager: optionValues.packageManager as PackageManager,
  237. hasPerformance,
  238. })}
  239. introduction={introduction}
  240. platformOptions={platformOptions}
  241. {...props}
  242. />
  243. );
  244. }
  245. export default GettingStartedWithKotlin;