martini.tsx 6.0 KB

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