python.tsx 8.6 KB

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