echo.tsx 6.6 KB

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