martini.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import {Fragment} from 'react';
  2. import {Alert} from 'sentry/components/core/alert';
  3. import ExternalLink from 'sentry/components/links/externalLink';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. Docs,
  7. DocsParams,
  8. OnboardingConfig,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {
  11. getCrashReportGenericInstallStep,
  12. getCrashReportModalConfigDescription,
  13. getCrashReportModalIntroduction,
  14. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  15. import {
  16. feedbackOnboardingJsLoader,
  17. replayOnboardingJsLoader,
  18. } from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  19. import {t, tct} from 'sentry/locale';
  20. type Params = DocsParams;
  21. const getConfigureSnippet = (params: Params) => `
  22. import (
  23. "fmt"
  24. "github.com/getsentry/sentry-go"
  25. sentrymartini "github.com/getsentry/sentry-go/martini"
  26. "github.com/go-martini/martini"
  27. )
  28. // To initialize Sentry's handler, you need to initialize Sentry itself beforehand
  29. if err := sentry.Init(sentry.ClientOptions{
  30. Dsn: "${params.dsn.public}",${
  31. params.isPerformanceSelected
  32. ? `
  33. // Set TracesSampleRate to 1.0 to capture 100%
  34. // of transactions for tracing.
  35. // We recommend adjusting this value in production,
  36. TracesSampleRate: 1.0,`
  37. : ''
  38. }
  39. ); err != nil {
  40. fmt.Printf("Sentry initialization failed: %v\\n", err)
  41. }
  42. // Then create your app
  43. app := martini.Classic()
  44. // Once it's done, you can attach the handler as one of your middleware
  45. app.Use(sentrymartini.New(sentrymartini.Options{}))
  46. // Set up routes
  47. app.Get("/", func() string {
  48. return "Hello world!"
  49. })
  50. // And run it
  51. app.Run()`;
  52. const getOptionsSnippet = () => `
  53. // Whether Sentry should repanic after recovery, in most cases it should be set to true,
  54. // as martini.Classic includes its own Recovery middleware that handles http responses.
  55. Repanic bool
  56. // Whether you want to block the request before moving forward with the response.
  57. // Because Martini's default "Recovery" handler doesn't restart the application,
  58. // it's safe to either skip this option or set it to "false".
  59. WaitForDelivery bool
  60. // Timeout for the event delivery requests.
  61. Timeout time.Duration`;
  62. const getUsageSnippet = () => `
  63. app := martini.Classic()
  64. app.Use(sentrymartini.New(sentrymartini.Options{
  65. Repanic: true,
  66. }))
  67. app.Use(func(rw http.ResponseWriter, r *http.Request, c martini.Context, hub *sentry.Hub) {
  68. hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
  69. })
  70. app.Get("/", func(rw http.ResponseWriter, r *http.Request, hub *sentry.Hub) {
  71. if someCondition {
  72. hub.WithScope(func (scope *sentry.Scope) {
  73. scope.SetExtra("unwantedQuery", rw.URL.RawQuery)
  74. hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
  75. })
  76. }
  77. rw.WriteHeader(http.StatusOK)
  78. })
  79. app.Get("/foo", func() string {
  80. // sentrymartini handler will catch it just fine. Also, because we attached "someRandomTag"
  81. // in the middleware before, it will be sent through as well
  82. panic("y tho")
  83. })
  84. app.Run()`;
  85. const getBeforeSendSnippet = (params: any) => `
  86. sentry.Init(sentry.ClientOptions{
  87. Dsn: "${params.dsn.public}",
  88. BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
  89. if hint.Context != nil {
  90. if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
  91. // You have access to the original Request here
  92. }
  93. }
  94. return event
  95. },
  96. })`;
  97. const onboarding: OnboardingConfig = {
  98. install: () => [
  99. {
  100. type: StepType.INSTALL,
  101. description: tct('Install our Go Martini SDK using [code:go get]:', {
  102. code: <code />,
  103. }),
  104. configurations: [
  105. {
  106. language: 'bash',
  107. code: 'go get github.com/getsentry/sentry-go/martini',
  108. },
  109. ],
  110. },
  111. ],
  112. configure: params => [
  113. {
  114. type: StepType.CONFIGURE,
  115. description: t(
  116. "Import and initialize the Sentry SDK early in your application's setup:"
  117. ),
  118. configurations: [
  119. {
  120. language: 'go',
  121. code: getConfigureSnippet(params),
  122. },
  123. {
  124. description: (
  125. <Fragment>
  126. <strong>{t('Options')}</strong>
  127. <p>
  128. {tct(
  129. '[code:sentrymartini] accepts a struct of [code:Options] that allows you to configure how the handler will behave.',
  130. {code: <code />}
  131. )}
  132. </p>
  133. {t('Currently it respects 3 options:')}
  134. </Fragment>
  135. ),
  136. language: 'go',
  137. code: getOptionsSnippet(),
  138. },
  139. ],
  140. },
  141. {
  142. title: t('Usage'),
  143. description: (
  144. <Fragment>
  145. <p>
  146. {tct(
  147. "[code:sentrymartini] maps an instance of [sentryHubLink:*sentry.Hub] as one of the services available throughout the rest of the request's lifetime. You can access it by providing a hub [code:*sentry.Hub] parameter in any of your proceeding middleware and routes. And it should be used instead of the global [code:sentry.CaptureMessage], [code:sentry.CaptureException], or any other calls, as it keeps the separation of data between the requests.",
  148. {
  149. code: <code />,
  150. sentryHubLink: (
  151. <ExternalLink href="https://pkg.go.dev/github.com/getsentry/sentry-go#Hub" />
  152. ),
  153. }
  154. )}
  155. </p>
  156. <Alert type="info">
  157. {tct(
  158. "Keep in mind that [code:*sentry.Hub] won't be available in middleware attached before [code:sentrymartini]!",
  159. {code: <code />}
  160. )}
  161. </Alert>
  162. </Fragment>
  163. ),
  164. configurations: [
  165. {
  166. language: 'go',
  167. code: getUsageSnippet(),
  168. },
  169. {
  170. description: (
  171. <strong>
  172. {tct('Accessing Request in [beforeSendCode:BeforeSend] callback', {
  173. beforeSendCode: <code />,
  174. })}
  175. </strong>
  176. ),
  177. language: 'go',
  178. code: getBeforeSendSnippet(params),
  179. },
  180. ],
  181. },
  182. ],
  183. verify: () => [],
  184. };
  185. const crashReportOnboarding: OnboardingConfig = {
  186. introduction: () => getCrashReportModalIntroduction(),
  187. install: (params: Params) => getCrashReportGenericInstallStep(params),
  188. configure: () => [
  189. {
  190. type: StepType.CONFIGURE,
  191. description: getCrashReportModalConfigDescription({
  192. link: 'https://docs.sentry.io/platforms/go/guides/martini/user-feedback/configuration/#crash-report-modal',
  193. }),
  194. },
  195. ],
  196. verify: () => [],
  197. nextSteps: () => [],
  198. };
  199. const docs: Docs = {
  200. onboarding,
  201. replayOnboardingJsLoader,
  202. crashReportOnboarding,
  203. feedbackOnboardingJsLoader,
  204. };
  205. export default docs;