flask.tsx 7.3 KB

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