martini.tsx 6.3 KB

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