rails.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {
  9. getCrashReportModalConfigDescription,
  10. getCrashReportModalIntroduction,
  11. getCrashReportSDKInstallFirstStepRails,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  13. import {getRubyMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  14. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  15. import {t, tct} from 'sentry/locale';
  16. type Params = DocsParams;
  17. const getInstallSnippet = (
  18. params: Params
  19. ) => `${params.isProfilingSelected ? 'gem "stackprof"\n' : ''}gem "sentry-ruby"
  20. gem "sentry-rails"`;
  21. const generatorSnippet = 'bin/rails generate sentry';
  22. const getConfigureSnippet = (params: Params) => `
  23. Sentry.init do |config|
  24. config.dsn = '${params.dsn.public}'
  25. config.breadcrumbs_logger = [:active_support_logger, :http_logger]${
  26. params.isPerformanceSelected
  27. ? `
  28. # Set traces_sample_rate to 1.0 to capture 100%
  29. # of transactions for tracing.
  30. # We recommend adjusting this value in production.
  31. config.traces_sample_rate = 1.0
  32. # or
  33. config.traces_sampler = lambda do |context|
  34. true
  35. end`
  36. : ''
  37. }${
  38. params.isProfilingSelected
  39. ? `
  40. # Set profiles_sample_rate to profile 100%
  41. # of sampled transactions.
  42. # We recommend adjusting this value in production.
  43. config.profiles_sample_rate = 1.0`
  44. : ''
  45. }
  46. end`;
  47. const getVerifySnippet = () => `
  48. begin
  49. 1 / 0
  50. rescue ZeroDivisionError => exception
  51. Sentry.capture_exception(exception)
  52. end
  53. Sentry.capture_message("test message")`;
  54. const onboarding: OnboardingConfig = {
  55. introduction: () =>
  56. t(
  57. 'In Rails, all uncaught exceptions will be automatically reported. We support Rails 5 and newer.'
  58. ),
  59. install: (params: Params) => [
  60. {
  61. type: StepType.INSTALL,
  62. description: tct(
  63. 'The Sentry SDK for Rails comes as two gems that should be added to your [gemfileCode:Gemfile]:',
  64. {
  65. gemfileCode: <code />,
  66. }
  67. ),
  68. configurations: [
  69. {
  70. language: 'ruby',
  71. code: getInstallSnippet(params),
  72. additionalInfo: params.isProfilingSelected
  73. ? tct(
  74. 'Ruby Profiling beta is available since SDK version 5.9.0. We use the [stackprofLink:stackprof gem] to collect profiles for Ruby. Make sure [code:stackprof] is loaded before [code:sentry-ruby].',
  75. {
  76. stackprofLink: (
  77. <ExternalLink href="https://github.com/tmm1/stackprof" />
  78. ),
  79. code: <code />,
  80. }
  81. )
  82. : undefined,
  83. },
  84. {
  85. description: t('After adding the gems, run the following to install the SDK:'),
  86. language: 'ruby',
  87. code: 'bundle install',
  88. },
  89. ],
  90. },
  91. ],
  92. configure: (params: Params) => [
  93. {
  94. type: StepType.CONFIGURE,
  95. description: tct(
  96. 'Run the following Rails generator to create the initializer file [code:config/initializers/sentry.rb].',
  97. {
  98. code: <code />,
  99. }
  100. ),
  101. configurations: [
  102. {
  103. language: 'ruby',
  104. code: generatorSnippet,
  105. },
  106. {
  107. description: t('You can then change the Sentry configuration as follows:'),
  108. },
  109. {
  110. language: 'ruby',
  111. code: getConfigureSnippet(params),
  112. },
  113. ],
  114. },
  115. ],
  116. verify: () => [
  117. {
  118. type: StepType.VERIFY,
  119. description: t(
  120. "This snippet contains a deliberate error and message sent to Sentry and can be used as a test to make sure that everything's working as expected."
  121. ),
  122. configurations: [
  123. {
  124. code: [
  125. {
  126. label: 'ruby',
  127. value: 'ruby',
  128. language: 'ruby',
  129. code: getVerifySnippet(),
  130. },
  131. ],
  132. },
  133. ],
  134. },
  135. ],
  136. nextSteps: () => [],
  137. };
  138. const crashReportOnboarding: OnboardingConfig = {
  139. introduction: () => getCrashReportModalIntroduction(),
  140. install: (params: Params) => [
  141. {
  142. type: StepType.INSTALL,
  143. description: tct(
  144. "In Rails, being able to serve dynamic pages in response to errors is required to pass the needed [codeEvent:event_id] to the JavaScript SDK. [link:Read our docs] to learn more. Once you're able to serve dynamic exception pages, you can support user feedback.",
  145. {
  146. codeEvent: <code />,
  147. link: (
  148. <ExternalLink href="https://docs.sentry.io/platforms/ruby/guides/rails/user-feedback/#integration" />
  149. ),
  150. }
  151. ),
  152. configurations: [
  153. getCrashReportSDKInstallFirstStepRails(params),
  154. {
  155. description: t(
  156. 'Additionally, you need the template that brings up the dialog:'
  157. ),
  158. code: [
  159. {
  160. label: 'ERB',
  161. value: 'erb',
  162. language: 'erb',
  163. code: `<% sentry_id = request.env["sentry.error_event_id"] %>
  164. <% if sentry_id.present? %>
  165. <script>
  166. Sentry.init({ dsn: "${params.dsn.public}" });
  167. Sentry.showReportDialog({ eventId: "<%= sentry_id %>" });
  168. </script>
  169. <% end %>`,
  170. },
  171. ],
  172. },
  173. ],
  174. },
  175. ],
  176. configure: () => [
  177. {
  178. type: StepType.CONFIGURE,
  179. description: getCrashReportModalConfigDescription({
  180. link: 'https://docs.sentry.io/platforms/ruby/guides/rails/user-feedback/configuration/#crash-report-modal',
  181. }),
  182. },
  183. ],
  184. verify: () => [],
  185. nextSteps: () => [],
  186. };
  187. const docs: Docs = {
  188. onboarding,
  189. customMetricsOnboarding: getRubyMetricsOnboarding(),
  190. replayOnboardingJsLoader,
  191. crashReportOnboarding,
  192. };
  193. export default docs;