iris.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  12. import {t, tct} from 'sentry/locale';
  13. type Params = DocsParams;
  14. const getConfigureSnippet = (params: Params) => `
  15. import (
  16. "fmt"
  17. "github.com/getsentry/sentry-go"
  18. sentryiris "github.com/getsentry/sentry-go/iris"
  19. "github.com/kataras/iris/v12"
  20. )
  21. // To initialize Sentry's handler, you need to initialize Sentry itself beforehand
  22. if err := sentry.Init(sentry.ClientOptions{
  23. Dsn: "${params.dsn}",
  24. EnableTracing: true,
  25. // Set TracesSampleRate to 1.0 to capture 100%
  26. // of transactions for performance monitoring.
  27. // We recommend adjusting this value in production,
  28. TracesSampleRate: 1.0,
  29. }); err != nil {
  30. fmt.Printf("Sentry initialization failed: %v\n", err)
  31. }
  32. // Then create your app
  33. app := iris.Default()
  34. // Once it's done, you can attach the handler as one of your middleware
  35. app.Use(sentryiris.New(sentryiris.Options{}))
  36. // Set up routes
  37. app.Get("/", func(ctx iris.Context) {
  38. ctx.Writef("Hello world!")
  39. })
  40. // And run it
  41. app.Run(iris.Addr(":3000"))`;
  42. const getOptionsSnippet = () => `
  43. // Whether Sentry should repanic after recovery, in most cases it should be set to true,
  44. // as iris.Default includes its own Recovery middleware what handles http responses.
  45. Repanic bool
  46. // Whether you want to block the request before moving forward with the response.
  47. // Because Iris's default "Recovery" handler doesn't restart the application,
  48. // it's safe to either skip this option or set it to "false".
  49. WaitForDelivery bool
  50. // Timeout for the event delivery requests.
  51. Timeout time.Duration`;
  52. const getUsageSnippet = () => `
  53. app := iris.Default()
  54. app.Use(sentryiris.New(sentryiris.Options{
  55. Repanic: true,
  56. }))
  57. app.Use(func(ctx iris.Context) {
  58. if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
  59. hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
  60. }
  61. ctx.Next()
  62. })
  63. app.Get("/", func(ctx iris.Context) {
  64. if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
  65. hub.WithScope(func(scope *sentry.Scope) {
  66. scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
  67. hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
  68. })
  69. }
  70. })
  71. app.Get("/foo", func(ctx iris.Context) {
  72. // sentryiris handler will catch it just fine. Also, because we attached "someRandomTag"
  73. // in the middleware before, it will be sent through as well
  74. panic("y tho")
  75. })
  76. app.Run(iris.Addr(":3000"))`;
  77. const getBeforeSendSnippet = params => `
  78. sentry.Init(sentry.ClientOptions{
  79. Dsn: "${params.dsn}",
  80. BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
  81. if hint.Context != nil {
  82. if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
  83. // You have access to the original Request here
  84. }
  85. }
  86. return event
  87. },
  88. })`;
  89. const onboarding: OnboardingConfig = {
  90. install: () => [
  91. {
  92. type: StepType.INSTALL,
  93. description: tct('Install our Go Iris SDK using [code:go get]:', {
  94. code: <code />,
  95. }),
  96. configurations: [
  97. {
  98. language: 'bash',
  99. code: 'go get github.com/getsentry/sentry-go/iris',
  100. },
  101. ],
  102. },
  103. ],
  104. configure: params => [
  105. {
  106. type: StepType.CONFIGURE,
  107. description: t(
  108. "Import and initialize the Sentry SDK early in your application's setup:"
  109. ),
  110. configurations: [
  111. {
  112. language: 'go',
  113. code: getConfigureSnippet(params),
  114. },
  115. {
  116. description: (
  117. <Fragment>
  118. <strong>{t('Options')}</strong>
  119. <p>
  120. {tct(
  121. '[sentryirisCode:sentryiris] accepts a struct of [optionsCode:Options] that allows you to configure how the handler will behave.',
  122. {sentryirisCode: <code />, optionsCode: <code />}
  123. )}
  124. </p>
  125. {t('Currently it respects 3 options:')}
  126. </Fragment>
  127. ),
  128. language: 'go',
  129. code: getOptionsSnippet(),
  130. },
  131. ],
  132. },
  133. {
  134. title: t('Usage'),
  135. description: (
  136. <Fragment>
  137. <p>
  138. {tct(
  139. "[sentryirisCode:sentryiris] attaches an instance of [sentryHubLink:*sentry.Hub] to the [irisContextCode:iris.Context], which makes it available throughout the rest of the request's lifetime. You can access it by using the [getHubFromContextCode:sentryiris.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.",
  140. {
  141. sentryirisCode: <code />,
  142. sentryHubLink: (
  143. <ExternalLink href="https://godoc.org/github.com/getsentry/sentry-go#Hub" />
  144. ),
  145. irisContextCode: <code />,
  146. getHubFromContextCode: <code />,
  147. captureMessageCode: <code />,
  148. captureExceptionCode: <code />,
  149. }
  150. )}
  151. </p>
  152. <AlertWithoutMarginBottom>
  153. {tct(
  154. "Keep in mind that [sentryHubCode:*sentry.Hub] won't be available in middleware attached before [sentryirisCode:sentryiris]!",
  155. {sentryirisCode: <code />, sentryHubCode: <code />}
  156. )}
  157. </AlertWithoutMarginBottom>
  158. </Fragment>
  159. ),
  160. configurations: [
  161. {
  162. language: 'go',
  163. code: getUsageSnippet(),
  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: getBeforeSendSnippet(params),
  175. },
  176. ],
  177. },
  178. ],
  179. verify: () => [],
  180. };
  181. const docs: Docs = {
  182. onboarding,
  183. replayOnboardingJsLoader,
  184. };
  185. export default docs;
  186. const AlertWithoutMarginBottom = styled(Alert)`
  187. margin-bottom: 0;
  188. `;