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