iris.tsx 6.3 KB

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