python.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import {
  4. type Docs,
  5. DocsPageLocation,
  6. type DocsParams,
  7. type OnboardingConfig,
  8. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  9. import {
  10. getCrashReportBackendInstallStep,
  11. getCrashReportModalConfigDescription,
  12. getCrashReportModalIntroduction,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  14. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  15. import {t, tct} from 'sentry/locale';
  16. type Params = DocsParams;
  17. const getInstallSnippet = () => `pip install --upgrade sentry-sdk`;
  18. const getSdkSetupSnippet = (params: Params) => `
  19. import sentry_sdk
  20. sentry_sdk.init(
  21. dsn="${params.dsn.public}",${
  22. params.isPerformanceSelected
  23. ? `
  24. # Set traces_sample_rate to 1.0 to capture 100%
  25. # of transactions for tracing.
  26. traces_sample_rate=1.0,`
  27. : ''
  28. }${
  29. params.isProfilingSelected &&
  30. params.profilingOptions?.defaultProfilingMode === 'transaction'
  31. ? `
  32. # Set profiles_sample_rate to 1.0 to profile 100%
  33. # of sampled transactions.
  34. # We recommend adjusting this value in production.
  35. profiles_sample_rate=1.0,`
  36. : ''
  37. }
  38. )${
  39. params.isProfilingSelected &&
  40. params.profilingOptions?.defaultProfilingMode === 'continuous'
  41. ? `
  42. # Manually call start_profiler and stop_profiler
  43. # to profile the code in between
  44. sentry_sdk.profiler.start_profiler()
  45. # do some work here
  46. sentry_sdk.profiler.stop_profiler()`
  47. : ''
  48. }`;
  49. const onboarding: OnboardingConfig = {
  50. install: (params: Params) => [
  51. {
  52. type: StepType.INSTALL,
  53. description: tct('Install our Python SDK using [code:pip]:', {
  54. code: <code />,
  55. }),
  56. configurations: [
  57. {
  58. description:
  59. params.docsLocation === DocsPageLocation.PROFILING_PAGE
  60. ? tct(
  61. 'You need a minimum version [code:1.18.0] of the [code:sentry-python] SDK for the profiling feature.',
  62. {
  63. code: <code />,
  64. }
  65. )
  66. : undefined,
  67. language: 'bash',
  68. code: getInstallSnippet(),
  69. },
  70. ],
  71. },
  72. ],
  73. configure: (params: Params) => [
  74. {
  75. type: StepType.CONFIGURE,
  76. description: t(
  77. "Import and initialize the Sentry SDK early in your application's setup:"
  78. ),
  79. configurations: [
  80. {
  81. language: 'python',
  82. code: getSdkSetupSnippet(params),
  83. },
  84. ],
  85. additionalInfo: params.isProfilingSelected &&
  86. params.profilingOptions?.defaultProfilingMode === 'continuous' && (
  87. <AlternativeConfiguration />
  88. ),
  89. },
  90. ],
  91. verify: () => [
  92. {
  93. type: StepType.VERIFY,
  94. description: t(
  95. 'One way to verify your setup is by intentionally causing an error that breaks your application.'
  96. ),
  97. configurations: [
  98. {
  99. language: 'python',
  100. description: t(
  101. 'Raise an unhandled Python exception by inserting a divide by zero expression into your application:'
  102. ),
  103. code: 'division_by_zero = 1 / 0',
  104. },
  105. ],
  106. },
  107. ],
  108. };
  109. export const crashReportOnboardingPython: OnboardingConfig = {
  110. introduction: () => getCrashReportModalIntroduction(),
  111. install: (params: Params) => getCrashReportBackendInstallStep(params),
  112. configure: () => [
  113. {
  114. type: StepType.CONFIGURE,
  115. description: getCrashReportModalConfigDescription({
  116. link: 'https://docs.sentry.io/platforms/python/user-feedback/configuration/#crash-report-modal',
  117. }),
  118. },
  119. ],
  120. verify: () => [],
  121. nextSteps: () => [],
  122. };
  123. export const performanceOnboarding: OnboardingConfig = {
  124. introduction: () =>
  125. t(
  126. "Adding Performance to your Python project is simple. Make sure you've got these basics down."
  127. ),
  128. install: onboarding.install,
  129. configure: params => [
  130. {
  131. type: StepType.CONFIGURE,
  132. description: t(
  133. "Configuration should happen as early as possible in your application's lifecycle."
  134. ),
  135. configurations: [
  136. {
  137. description: tct(
  138. "Once this is done, Sentry's Python SDK captures all unhandled exceptions and transactions. Note that [code:enable_tracing] is available in Sentry Python SDK version [code:≥ 1.16.0]. To enable tracing in older SDK versions ([code:≥ 0.11.2]), use [code:traces_sample_rate=1.0].",
  139. {code: <code />}
  140. ),
  141. language: 'python',
  142. code: `
  143. import sentry-sdk
  144. sentry_sdk.init(
  145. dsn="${params.dsn.public}",
  146. enable_tracing=True,
  147. )`,
  148. additionalInfo: tct(
  149. 'Learn more about tracing [linkTracingOptions:options], how to use the [linkTracesSampler:traces_sampler] function, or how to [linkSampleTransactions:sample transactions].',
  150. {
  151. linkTracingOptions: (
  152. <ExternalLink href="https://docs.sentry.io/platforms/python/configuration/options/#tracing-options" />
  153. ),
  154. linkTracesSampler: (
  155. <ExternalLink href="https://docs.sentry.io/platforms/python/configuration/sampling/" />
  156. ),
  157. linkSampleTransactions: (
  158. <ExternalLink href="https://docs.sentry.io/platforms/python/configuration/sampling/" />
  159. ),
  160. }
  161. ),
  162. },
  163. ],
  164. },
  165. ],
  166. verify: () => [
  167. {
  168. type: StepType.VERIFY,
  169. description: tct(
  170. 'Verify that performance monitoring is working correctly with our [link:automatic instrumentation] by simply using your Python application.',
  171. {
  172. link: (
  173. <ExternalLink href="https://docs.sentry.io/platforms/python/tracing/instrumentation/automatic-instrumentation/" />
  174. ),
  175. }
  176. ),
  177. additionalInfo: tct(
  178. 'You have the option to manually construct a transaction using [link:custom instrumentation].',
  179. {
  180. link: (
  181. <ExternalLink href="https://docs.sentry.io/platforms/python/tracing/instrumentation/custom-instrumentation/" />
  182. ),
  183. }
  184. ),
  185. },
  186. ],
  187. nextSteps: () => [],
  188. };
  189. export function AlternativeConfiguration() {
  190. return (
  191. <div>
  192. {tct(
  193. 'Alternatively, you can also explicitly control continuous profiling or use transaction profiling. See our [link:documentation] for more information.',
  194. {
  195. link: (
  196. <ExternalLink href="https://docs.sentry.io/platforms/python/profiling/" />
  197. ),
  198. }
  199. )}
  200. </div>
  201. );
  202. }
  203. const docs: Docs = {
  204. onboarding,
  205. performanceOnboarding,
  206. customMetricsOnboarding: getPythonMetricsOnboarding({
  207. installSnippet: getInstallSnippet(),
  208. }),
  209. crashReportOnboarding: crashReportOnboardingPython,
  210. };
  211. export default docs;