echo.tsx 7.4 KB

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