iris.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 Iris 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/iris',
  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. "github.com/getsentry/sentry-go"
  41. sentryiris "github.com/getsentry/sentry-go/iris"
  42. "github.com/kataras/iris/v12"
  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. // Then create your app
  56. app := iris.Default()
  57. // Once it's done, you can attach the handler as one of your middleware
  58. app.Use(sentryiris.New(sentryiris.Options{}))
  59. // Set up routes
  60. app.Get("/", func(ctx iris.Context) {
  61. ctx.Writef("Hello world!")
  62. })
  63. // And run it
  64. app.Run(iris.Addr(":3000"))
  65. `,
  66. },
  67. {
  68. description: (
  69. <Fragment>
  70. <strong>{t('Options')}</strong>
  71. <p>
  72. {tct(
  73. '[sentryirisCode:sentryiris] accepts a struct of [optionsCode:Options] that allows you to configure how the handler will behave.',
  74. {sentryirisCode: <code />, optionsCode: <code />}
  75. )}
  76. </p>
  77. {t('Currently it respects 3 options:')}
  78. </Fragment>
  79. ),
  80. language: 'go',
  81. code: `
  82. // Whether Sentry should repanic after recovery, in most cases it should be set to true,
  83. // as iris.Default includes its own Recovery middleware what handles http responses.
  84. Repanic bool
  85. // Whether you want to block the request before moving forward with the response.
  86. // Because Iris's default "Recovery" handler doesn't restart the application,
  87. // it's safe to either skip this option or set it to "false".
  88. WaitForDelivery bool
  89. // Timeout for the event delivery requests.
  90. Timeout time.Duration
  91. `,
  92. },
  93. ],
  94. },
  95. {
  96. title: t('Usage'),
  97. description: (
  98. <Fragment>
  99. <p>
  100. {tct(
  101. "[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.",
  102. {
  103. sentryirisCode: <code />,
  104. sentryHubLink: (
  105. <ExternalLink href="https://godoc.org/github.com/getsentry/sentry-go#Hub" />
  106. ),
  107. irisContextCode: <code />,
  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 [sentryirisCode:sentryiris]!",
  117. {sentryirisCode: <code />, sentryHubCode: <code />}
  118. )}
  119. </AlertWithoutMarginBottom>
  120. </Fragment>
  121. ),
  122. configurations: [
  123. {
  124. language: 'go',
  125. code: `
  126. app := iris.Default()
  127. app.Use(sentryiris.New(sentryiris.Options{
  128. Repanic: true,
  129. }))
  130. app.Use(func(ctx iris.Context) {
  131. if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
  132. hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
  133. }
  134. ctx.Next()
  135. })
  136. app.Get("/", func(ctx iris.Context) {
  137. if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
  138. hub.WithScope(func(scope *sentry.Scope) {
  139. scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
  140. hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
  141. })
  142. }
  143. })
  144. app.Get("/foo", func(ctx iris.Context) {
  145. // sentryiris handler will catch it just fine. Also, because we attached "someRandomTag"
  146. // in the middleware before, it will be sent through as well
  147. panic("y tho")
  148. })
  149. app.Run(iris.Addr(":3000"))
  150. `,
  151. },
  152. {
  153. description: (
  154. <strong>
  155. {tct('Accessing Request in [beforeSendCode:BeforeSend] callback', {
  156. beforeSendCode: <code />,
  157. })}
  158. </strong>
  159. ),
  160. language: 'go',
  161. code: `
  162. sentry.Init(sentry.ClientOptions{
  163. Dsn: "${dsn}",
  164. BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
  165. if hint.Context != nil {
  166. if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
  167. // You have access to the original Request here
  168. }
  169. }
  170. return event
  171. },
  172. })
  173. `,
  174. },
  175. ],
  176. },
  177. ];
  178. // Configuration End
  179. export function GettingStartedWithIris({dsn, ...props}: ModuleProps) {
  180. return <Layout steps={steps({dsn})} {...props} />;
  181. }
  182. export default GettingStartedWithIris;
  183. const AlertWithoutMarginBottom = styled(Alert)`
  184. margin-bottom: 0;
  185. `;