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