fasthttp.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {Fragment} from 'react';
  2. import {Alert} from 'sentry/components/core/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. sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
  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. params.isPerformanceSelected
  32. ? `
  33. EnableTracing: true,
  34. // Set TracesSampleRate to 1.0 to capture 100%
  35. // of transactions for tracing.
  36. // We recommend adjusting this value in production,
  37. TracesSampleRate: 1.0,`
  38. : ''
  39. }
  40. }); err != nil {
  41. fmt.Printf("Sentry initialization failed: %v\\n", err)
  42. }
  43. // Create an instance of sentryfasthttp
  44. sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{})
  45. // After creating the instance, you can attach the handler as one of your middleware
  46. fastHTTPHandler := sentryHandler.Handle(func(ctx *fasthttp.RequestCtx) {
  47. panic("y tho")
  48. })
  49. fmt.Println("Listening and serving HTTP on :3000")
  50. // And run it
  51. if err := fasthttp.ListenAndServe(":3000", fastHTTPHandler); err != nil {
  52. panic(err)
  53. }`;
  54. const getOptionsSnippet = () => `
  55. // Repanic configures whether Sentry should repanic after recovery, in most cases, it defaults to false,
  56. // as fasthttp doesn't include its own Recovery handler.
  57. Repanic bool
  58. // WaitForDelivery configures whether you want to block the request before moving forward with the response.
  59. // Because fasthttp doesn't include its own "Recovery" handler, it will restart the application,
  60. // and the event won't be delivered otherwise.
  61. WaitForDelivery bool
  62. // Timeout for the event delivery requests.
  63. Timeout time.Duration`;
  64. const getUsageSnippet = () => `
  65. func enhanceSentryEvent(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
  66. return func(ctx *fasthttp.RequestCtx) {
  67. if hub := sentryfasthttp.GetHubFromContext(ctx); hub != nil {
  68. hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
  69. }
  70. handler(ctx)
  71. }
  72. }
  73. // Later in the code
  74. sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{
  75. Repanic: true,
  76. WaitForDelivery: true,
  77. })
  78. defaultHandler := func(ctx *fasthttp.RequestCtx) {
  79. if hub := sentryfasthttp.GetHubFromContext(ctx); hub != nil {
  80. hub.WithScope(func(scope *sentry.Scope) {
  81. scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
  82. hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
  83. })
  84. }
  85. ctx.SetStatusCode(fasthttp.StatusOK)
  86. }
  87. fooHandler := enhanceSentryEvent(func(ctx *fasthttp.RequestCtx) {
  88. panic("y tho")
  89. })
  90. fastHTTPHandler := func(ctx *fasthttp.RequestCtx) {
  91. switch string(ctx.Path()) {
  92. case "/foo":
  93. fooHandler(ctx)
  94. default:
  95. defaultHandler(ctx)
  96. }
  97. }
  98. fmt.Println("Listening and serving HTTP on :3000")
  99. if err := fasthttp.ListenAndServe(":3000", sentryHandler.Handle(fastHTTPHandler)); err != nil {
  100. panic(err)
  101. }`;
  102. const getBeforeSendSnippet = (params: any) => `
  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 ctx, ok := hint.Context.Value(sentry.RequestContextKey).(*fasthttp.RequestCtx); ok {
  108. // You have access to the original Context if it panicked
  109. fmt.Println(string(ctx.Request.Host()))
  110. }
  111. }
  112. return event
  113. },
  114. })`;
  115. const onboarding: OnboardingConfig = {
  116. install: () => [
  117. {
  118. type: StepType.INSTALL,
  119. description: tct('Install our Go FastHTTP SDK using [code:go get]:', {
  120. code: <code />,
  121. }),
  122. configurations: [
  123. {
  124. language: 'bash',
  125. code: 'go get github.com/getsentry/sentry-go/fasthttp',
  126. },
  127. ],
  128. },
  129. ],
  130. configure: params => [
  131. {
  132. type: StepType.CONFIGURE,
  133. description: t(
  134. "Import and initialize the Sentry SDK early in your application's setup:"
  135. ),
  136. configurations: [
  137. {
  138. language: 'go',
  139. code: getConfigureSnippet(params),
  140. },
  141. {
  142. description: (
  143. <Fragment>
  144. <strong>{t('Options')}</strong>
  145. <p>
  146. {tct(
  147. '[code:sentryfasthttp] accepts a struct of [code:Options] that allows you to configure how the handler will behave.',
  148. {code: <code />}
  149. )}
  150. </p>
  151. {t('Currently it respects 3 options:')}
  152. </Fragment>
  153. ),
  154. language: 'go',
  155. code: getOptionsSnippet(),
  156. },
  157. ],
  158. },
  159. {
  160. title: t('Usage'),
  161. description: (
  162. <Fragment>
  163. <p>
  164. {tct(
  165. "[code:sentryfasthttp] attaches an instance of [sentryHubLink:*sentry.Hub] to the request's context, which makes it available throughout the rest of the request's lifetime. You can access it by using the [code:sentryfasthttp.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.",
  166. {
  167. sentryHubLink: (
  168. <ExternalLink href="https://pkg.go.dev/github.com/getsentry/sentry-go#Hub" />
  169. ),
  170. code: <code />,
  171. }
  172. )}
  173. </p>
  174. <Alert type="info">
  175. {tct(
  176. "Keep in mind that [code:*sentry.Hub] won't be available in middleware attached before [code:sentryfasthttp]!",
  177. {code: <code />}
  178. )}
  179. </Alert>
  180. </Fragment>
  181. ),
  182. configurations: [
  183. {
  184. language: 'go',
  185. code: getUsageSnippet(),
  186. },
  187. {
  188. description: (
  189. <strong>
  190. {tct('Accessing Request in [beforeSendCode:BeforeSend] callback', {
  191. beforeSendCode: <code />,
  192. })}
  193. </strong>
  194. ),
  195. language: 'go',
  196. code: getBeforeSendSnippet(params),
  197. },
  198. ],
  199. },
  200. ],
  201. verify: () => [],
  202. };
  203. const crashReportOnboarding: OnboardingConfig = {
  204. introduction: () => getCrashReportModalIntroduction(),
  205. install: (params: Params) => getCrashReportGenericInstallStep(params),
  206. configure: () => [
  207. {
  208. type: StepType.CONFIGURE,
  209. description: getCrashReportModalConfigDescription({
  210. link: 'https://docs.sentry.io/platforms/go/guides/fasthttp/user-feedback/configuration/#crash-report-modal',
  211. }),
  212. },
  213. ],
  214. verify: () => [],
  215. nextSteps: () => [],
  216. };
  217. const docs: Docs = {
  218. onboarding,
  219. replayOnboardingJsLoader,
  220. crashReportOnboarding,
  221. feedbackOnboardingJsLoader,
  222. };
  223. export default docs;