fasthttp.tsx 6.7 KB

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