python.tsx 9.4 KB

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