flask.tsx 7.4 KB

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