fiber.tsx 7.4 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 {
  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. 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. <AlertWithoutMarginBottom>
  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. </AlertWithoutMarginBottom>
  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;
  226. const AlertWithoutMarginBottom = styled(Alert)`
  227. margin-bottom: 0;
  228. `;