iris.tsx 7.1 KB

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