martini.tsx 6.0 KB

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