fiber.tsx 7.3 KB

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