flask.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {Fragment} from 'react';
  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 {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  11. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  12. import {
  13. AlternativeConfiguration,
  14. crashReportOnboardingPython,
  15. featureFlagOnboarding,
  16. } from 'sentry/gettingStartedDocs/python/python';
  17. import {t, tct} from 'sentry/locale';
  18. type Params = DocsParams;
  19. const getInstallSnippet = () => `pip install --upgrade 'sentry-sdk[flask]'`;
  20. const getSdkSetupSnippet = (params: Params) => `
  21. import sentry_sdk
  22. from flask import Flask
  23. sentry_sdk.init(
  24. dsn="${params.dsn.public}",${
  25. params.isPerformanceSelected
  26. ? `
  27. # Set traces_sample_rate to 1.0 to capture 100%
  28. # of transactions for tracing.
  29. traces_sample_rate=1.0,`
  30. : ''
  31. }${
  32. params.isProfilingSelected &&
  33. params.profilingOptions?.defaultProfilingMode !== 'continuous'
  34. ? `
  35. # Set profiles_sample_rate to 1.0 to profile 100%
  36. # of sampled transactions.
  37. # We recommend adjusting this value in production.
  38. profiles_sample_rate=1.0,`
  39. : params.isProfilingSelected &&
  40. params.profilingOptions?.defaultProfilingMode === 'continuous'
  41. ? `
  42. _experiments={
  43. # Set continuous_profiling_auto_start to True
  44. # to automatically start the profiler on when
  45. # possible.
  46. "continuous_profiling_auto_start": True,
  47. },`
  48. : ''
  49. }
  50. )
  51. `;
  52. const onboarding: OnboardingConfig = {
  53. introduction: () =>
  54. tct('The Flask integration adds support for the [link:Flask Framework].', {
  55. link: <ExternalLink href="https://flask.palletsprojects.com" />,
  56. }),
  57. install: (params: Params) => [
  58. {
  59. type: StepType.INSTALL,
  60. description: tct(
  61. 'Install [code:sentry-sdk] from PyPI with the [code:flask] extra:',
  62. {
  63. code: <code />,
  64. }
  65. ),
  66. configurations: [
  67. {
  68. description:
  69. params.docsLocation === DocsPageLocation.PROFILING_PAGE
  70. ? tct(
  71. 'You need a minimum version [code:1.18.0] of the [code:sentry-python] SDK for the profiling feature.',
  72. {
  73. code: <code />,
  74. }
  75. )
  76. : undefined,
  77. language: 'bash',
  78. code: getInstallSnippet(),
  79. },
  80. ],
  81. },
  82. ],
  83. configure: (params: Params) => [
  84. {
  85. type: StepType.CONFIGURE,
  86. description: tct(
  87. 'If you have the [codeFlask:flask] package in your dependencies, the Flask integration will be enabled automatically when you initialize the Sentry SDK. Initialize the Sentry SDK before your app has been initialized:',
  88. {
  89. codeFlask: <code />,
  90. }
  91. ),
  92. configurations: [
  93. {
  94. language: 'python',
  95. code: `
  96. ${getSdkSetupSnippet(params)}
  97. app = Flask(__name__)
  98. `,
  99. },
  100. ],
  101. additionalInfo: (
  102. <Fragment>
  103. {params.isProfilingSelected &&
  104. params.profilingOptions?.defaultProfilingMode === 'continuous' && (
  105. <Fragment>
  106. <AlternativeConfiguration />
  107. <br />
  108. </Fragment>
  109. )}
  110. {tct(
  111. 'The above configuration captures both error and performance data. To reduce the volume of performance data captured, change [code:traces_sample_rate] to a value between 0 and 1.',
  112. {code: <code />}
  113. )}
  114. </Fragment>
  115. ),
  116. },
  117. ],
  118. verify: (params: Params) => [
  119. {
  120. type: StepType.VERIFY,
  121. description: t(
  122. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  123. ),
  124. configurations: [
  125. {
  126. language: 'python',
  127. code: `
  128. ${getSdkSetupSnippet(params)}
  129. app = Flask(__name__)
  130. @app.route("/")
  131. def hello_world():
  132. 1/0 # raises an error
  133. return "<p>Hello, World!</p>"
  134. `,
  135. },
  136. ],
  137. additionalInfo: (
  138. <span>
  139. <p>
  140. {tct(
  141. 'When you point your browser to [link:http://localhost:5000/] a transaction in the Performance section of Sentry will be created.',
  142. {
  143. link: <ExternalLink href="http://localhost:5000/" />,
  144. }
  145. )}
  146. </p>
  147. <p>
  148. {t(
  149. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  150. )}
  151. </p>
  152. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  153. </span>
  154. ),
  155. },
  156. ],
  157. nextSteps: () => [],
  158. };
  159. const performanceOnboarding: OnboardingConfig = {
  160. introduction: () =>
  161. t(
  162. "Adding Performance to your Flask 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: tct(
  169. 'To configure the Sentry SDK, initialize it in your [code:settings.py] file:',
  170. {code: <code />}
  171. ),
  172. configurations: [
  173. {
  174. language: 'python',
  175. code: `
  176. import sentry-sdk
  177. sentry_sdk.init(
  178. dsn: "${params.dsn.public}",
  179. // Set traces_sample_rate to 1.0 to capture 100%
  180. // of transactions for performance monitoring.
  181. traces_sample_rate=1.0,
  182. )`,
  183. additionalInfo: tct(
  184. 'Learn more about tracing [linkTracingOptions:options], how to use the [linkTracesSampler:traces_sampler] function, or how to [linkSampleTransactions:sample transactions].',
  185. {
  186. linkTracingOptions: (
  187. <ExternalLink href="https://docs.sentry.io/platforms/python/configuration/options/#tracing-options" />
  188. ),
  189. linkTracesSampler: (
  190. <ExternalLink href="https://docs.sentry.io/platforms/python/configuration/sampling/" />
  191. ),
  192. linkSampleTransactions: (
  193. <ExternalLink href="https://docs.sentry.io/platforms/python/configuration/sampling/" />
  194. ),
  195. }
  196. ),
  197. },
  198. ],
  199. },
  200. ],
  201. verify: () => [
  202. {
  203. type: StepType.VERIFY,
  204. description: tct(
  205. 'Verify that performance monitoring is working correctly with our [link:automatic instrumentation] by simply using your Python application.',
  206. {
  207. link: (
  208. <ExternalLink href="https://docs.sentry.io/platforms/python/tracing/instrumentation/automatic-instrumentation/" />
  209. ),
  210. }
  211. ),
  212. additionalInfo: tct(
  213. 'You have the option to manually construct a transaction using [link:custom instrumentation].',
  214. {
  215. link: (
  216. <ExternalLink href="https://docs.sentry.io/platforms/python/tracing/instrumentation/custom-instrumentation/" />
  217. ),
  218. }
  219. ),
  220. },
  221. ],
  222. nextSteps: () => [],
  223. };
  224. const docs: Docs = {
  225. onboarding,
  226. replayOnboardingJsLoader,
  227. customMetricsOnboarding: getPythonMetricsOnboarding({
  228. installSnippet: getInstallSnippet(),
  229. }),
  230. performanceOnboarding,
  231. crashReportOnboarding: crashReportOnboardingPython,
  232. featureFlagOnboarding: featureFlagOnboarding,
  233. };
  234. export default docs;