echo.tsx 6.8 KB

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