echo.tsx 6.5 KB

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