python.tsx 8.2 KB

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