iris.tsx 6.1 KB

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