fiber.tsx 7.7 KB

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