fasthttp.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  6. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  7. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  8. import {t, tct} from 'sentry/locale';
  9. // Configuration Start
  10. export const steps = ({
  11. dsn,
  12. }: Partial<Pick<ModuleProps, 'dsn'>> = {}): LayoutProps['steps'] => [
  13. {
  14. type: StepType.INSTALL,
  15. description: (
  16. <p>
  17. {tct('Install our Go FastHTTP SDK using [code:go get]:', {
  18. code: <code />,
  19. })}
  20. </p>
  21. ),
  22. configurations: [
  23. {
  24. language: 'bash',
  25. code: 'go get github.com/getsentry/sentry-go/fasthttp',
  26. },
  27. ],
  28. },
  29. {
  30. type: StepType.CONFIGURE,
  31. description: t(
  32. "Import and initialize the Sentry SDK early in your application's setup:"
  33. ),
  34. configurations: [
  35. {
  36. language: 'go',
  37. code: `
  38. import (
  39. "fmt"
  40. "net/http"
  41. "github.com/getsentry/sentry-go"
  42. sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
  43. )
  44. // To initialize Sentry's handler, you need to initialize Sentry itself beforehand
  45. if err := sentry.Init(sentry.ClientOptions{
  46. Dsn: "${dsn}",
  47. EnableTracing: true,
  48. // Set TracesSampleRate to 1.0 to capture 100%
  49. // of transactions for performance monitoring.
  50. // We recommend adjusting this value in production,
  51. TracesSampleRate: 1.0,
  52. }); err != nil {
  53. fmt.Printf("Sentry initialization failed: %v\n", err)
  54. }
  55. // Create an instance of sentryfasthttp
  56. sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{})
  57. // After creating the instance, you can attach the handler as one of your middleware
  58. fastHTTPHandler := sentryHandler.Handle(func(ctx *fasthttp.RequestCtx) {
  59. panic("y tho")
  60. })
  61. fmt.Println("Listening and serving HTTP on :3000")
  62. // And run it
  63. if err := fasthttp.ListenAndServe(":3000", fastHTTPHandler); err != nil {
  64. panic(err)
  65. }
  66. `,
  67. },
  68. {
  69. description: (
  70. <Fragment>
  71. <strong>{t('Options')}</strong>
  72. <p>
  73. {tct(
  74. '[sentryfasthttpCode:sentryfasthttp] accepts a struct of [optionsCode:Options] that allows you to configure how the handler will behave.',
  75. {sentryfasthttpCode: <code />, optionsCode: <code />}
  76. )}
  77. </p>
  78. {t('Currently it respects 3 options:')}
  79. </Fragment>
  80. ),
  81. language: 'go',
  82. code: `
  83. // Repanic configures whether Sentry should repanic after recovery, in most cases, it defaults to false,
  84. // as fasthttp doesn't include its own Recovery handler.
  85. Repanic bool
  86. // WaitForDelivery configures whether you want to block the request before moving forward with the response.
  87. // Because fasthttp doesn't include its own "Recovery" handler, it will restart the application,
  88. // and the event won't be delivered otherwise.
  89. WaitForDelivery bool
  90. // Timeout for the event delivery requests.
  91. Timeout time.Duration
  92. `,
  93. },
  94. ],
  95. },
  96. {
  97. title: t('Usage'),
  98. description: (
  99. <Fragment>
  100. <p>
  101. {tct(
  102. "[sentryfasthttpCode: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 [getHubFromContextCode:sentryfasthttp.GetHubFromContext()] method on the context itself in any of your proceeding middleware and routes. And it should be used instead of the global [captureMessageCode:sentry.CaptureMessage], [captureExceptionCode:sentry.CaptureException], or any other calls, as it keeps the separation of data between the requests.",
  103. {
  104. sentryfasthttpCode: <code />,
  105. sentryHubLink: (
  106. <ExternalLink href="https://godoc.org/github.com/getsentry/sentry-go#Hub" />
  107. ),
  108. getHubFromContextCode: <code />,
  109. captureMessageCode: <code />,
  110. captureExceptionCode: <code />,
  111. }
  112. )}
  113. </p>
  114. <AlertWithoutMarginBottom>
  115. {tct(
  116. "Keep in mind that [sentryHubCode:*sentry.Hub] won't be available in middleware attached before [sentryfasthttpCode:sentryfasthttp]!",
  117. {sentryfasthttpCode: <code />, sentryHubCode: <code />}
  118. )}
  119. </AlertWithoutMarginBottom>
  120. </Fragment>
  121. ),
  122. configurations: [
  123. {
  124. language: 'go',
  125. code: `
  126. func enhanceSentryEvent(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
  127. return func(ctx *fasthttp.RequestCtx) {
  128. if hub := sentryfasthttp.GetHubFromContext(ctx); hub != nil {
  129. hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
  130. }
  131. handler(ctx)
  132. }
  133. }
  134. // Later in the code
  135. sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{
  136. Repanic: true,
  137. WaitForDelivery: true,
  138. })
  139. defaultHandler := func(ctx *fasthttp.RequestCtx) {
  140. if hub := sentryfasthttp.GetHubFromContext(ctx); hub != nil {
  141. hub.WithScope(func(scope *sentry.Scope) {
  142. scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
  143. hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
  144. })
  145. }
  146. ctx.SetStatusCode(fasthttp.StatusOK)
  147. }
  148. fooHandler := enhanceSentryEvent(func(ctx *fasthttp.RequestCtx) {
  149. panic("y tho")
  150. })
  151. fastHTTPHandler := func(ctx *fasthttp.RequestCtx) {
  152. switch string(ctx.Path()) {
  153. case "/foo":
  154. fooHandler(ctx)
  155. default:
  156. defaultHandler(ctx)
  157. }
  158. }
  159. fmt.Println("Listening and serving HTTP on :3000")
  160. if err := fasthttp.ListenAndServe(":3000", sentryHandler.Handle(fastHTTPHandler)); err != nil {
  161. panic(err)
  162. }
  163. `,
  164. },
  165. {
  166. description: (
  167. <strong>
  168. {tct('Accessing Request in [beforeSendCode:BeforeSend] callback', {
  169. beforeSendCode: <code />,
  170. })}
  171. </strong>
  172. ),
  173. language: 'go',
  174. code: `
  175. sentry.Init(sentry.ClientOptions{
  176. Dsn: "${dsn}",
  177. BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
  178. if hint.Context != nil {
  179. if ctx, ok := hint.Context.Value(sentry.RequestContextKey).(*fasthttp.RequestCtx); ok {
  180. // You have access to the original Context if it panicked
  181. fmt.Println(string(ctx.Request.Host()))
  182. }
  183. }
  184. return event
  185. },
  186. })
  187. `,
  188. },
  189. ],
  190. },
  191. ];
  192. // Configuration End
  193. export function GettingStartedWithFastHttp({dsn, ...props}: ModuleProps) {
  194. return <Layout steps={steps({dsn})} {...props} />;
  195. }
  196. export default GettingStartedWithFastHttp;
  197. const AlertWithoutMarginBottom = styled(Alert)`
  198. margin-bottom: 0;
  199. `;