fiber.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.public}",
  29. // Set TracesSampleRate to 1.0 to capture 100%
  30. // of transactions for tracing.
  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.public}",
  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. '[code:sentryfiber] accepts a struct of [code:Options] that allows you to configure how the handler will behave.',
  147. {code: <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. "[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.",
  165. {
  166. code: <code />,
  167. sentryHubLink: (
  168. <ExternalLink href="https://godoc.org/github.com/getsentry/sentry-go#Hub" />
  169. ),
  170. }
  171. )}
  172. </p>
  173. <AlertWithoutMarginBottom>
  174. {tct(
  175. "Keep in mind that [code:*sentry.Hub] won't be available in middleware attached before [code:sentryfiber]!",
  176. {code: <code />}
  177. )}
  178. </AlertWithoutMarginBottom>
  179. </Fragment>
  180. ),
  181. configurations: [
  182. {
  183. language: 'go',
  184. code: getUsageSnippet(),
  185. },
  186. {
  187. description: (
  188. <strong>
  189. {tct('Accessing Request in [beforeSendCode:BeforeSend] callback', {
  190. beforeSendCode: <code />,
  191. })}
  192. </strong>
  193. ),
  194. language: 'go',
  195. code: getBeforeSendSnippet(params),
  196. },
  197. ],
  198. },
  199. ],
  200. verify: () => [],
  201. };
  202. const crashReportOnboarding: OnboardingConfig = {
  203. introduction: () => getCrashReportModalIntroduction(),
  204. install: (params: Params) => getCrashReportGenericInstallStep(params),
  205. configure: () => [
  206. {
  207. type: StepType.CONFIGURE,
  208. description: getCrashReportModalConfigDescription({
  209. link: 'https://docs.sentry.io/platforms/go/guides/fiber/user-feedback/configuration/#crash-report-modal',
  210. }),
  211. },
  212. ],
  213. verify: () => [],
  214. nextSteps: () => [],
  215. };
  216. const docs: Docs = {
  217. onboarding,
  218. replayOnboardingJsLoader,
  219. crashReportOnboarding,
  220. };
  221. export default docs;
  222. const AlertWithoutMarginBottom = styled(Alert)`
  223. margin-bottom: 0;
  224. `;