python.tsx 8.7 KB

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