martini.tsx 6.8 KB

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