martini.tsx 6.2 KB

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