fiber.tsx 7.4 KB

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