aspnetcore.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import List from 'sentry/components/list';
  4. import ListItem from 'sentry/components/list/listItem';
  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. getCrashReportModalConfigDescription,
  13. getCrashReportModalIntroduction,
  14. getCrashReportSDKInstallFirstStep,
  15. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  16. import {getDotnetMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  17. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  18. import {t, tct} from 'sentry/locale';
  19. import {getPackageVersion} from 'sentry/utils/gettingStartedDocs/getPackageVersion';
  20. type Params = DocsParams;
  21. const getInstallSnippetPackageManager = (params: Params) => `
  22. Install-Package Sentry.AspNetCore -Version ${getPackageVersion(
  23. params,
  24. 'sentry.dotnet.aspnetcore',
  25. params.isProfilingSelected ? '4.3.0' : '3.34.0'
  26. )}`;
  27. const getInstallSnippetCoreCli = (params: Params) => `
  28. dotnet add package Sentry.AspNetCore -v ${getPackageVersion(
  29. params,
  30. 'sentry.dotnet.aspnetcore',
  31. params.isProfilingSelected ? '4.3.0' : '3.34.0'
  32. )}`;
  33. const getInstallProfilingSnippetPackageManager = (params: Params) => `
  34. Install-Package Sentry.Profiling -Version ${getPackageVersion(
  35. params,
  36. 'sentry.dotnet.profiling',
  37. '4.3.0'
  38. )}`;
  39. const getInstallProfilingSnippetCoreCli = (params: Params) => `
  40. dotnet add package Sentry.Profiling -v ${getPackageVersion(
  41. params,
  42. 'sentry.dotnet.profiling',
  43. '4.3.0'
  44. )}`;
  45. const getConfigureSnippet = (params: Params) => `
  46. public static IHostBuilder CreateHostBuilder(string[] args) =>
  47. Host.CreateDefaultBuilder(args)
  48. .ConfigureWebHostDefaults(webBuilder =>
  49. {
  50. // Add the following line:
  51. webBuilder.UseSentry(o =>
  52. {
  53. o.Dsn = "${params.dsn.public}";
  54. // When configuring for the first time, to see what the SDK is doing:
  55. o.Debug = true;${
  56. params.isPerformanceSelected
  57. ? `
  58. // Set TracesSampleRate to 1.0 to capture 100%
  59. // of transactions for tracing.
  60. // We recommend adjusting this value in production
  61. o.TracesSampleRate = 1.0;`
  62. : ''
  63. }${
  64. params.isProfilingSelected
  65. ? `
  66. // Sample rate for profiling, applied on top of othe TracesSampleRate,
  67. // e.g. 0.2 means we want to profile 20 % of the captured transactions.
  68. // We recommend adjusting this value in production.
  69. o.ProfilesSampleRate = 1.0;
  70. // Requires NuGet package: Sentry.Profiling
  71. // Note: By default, the profiler is initialized asynchronously. This can
  72. // be tuned by passing a desired initialization timeout to the constructor.
  73. o.AddIntegration(new ProfilingIntegration(
  74. // During startup, wait up to 500ms to profile the app startup code.
  75. // This could make launching the app a bit slower so comment it out if you
  76. // prefer profiling to start asynchronously.
  77. TimeSpan.FromMilliseconds(500)
  78. ));`
  79. : ''
  80. }
  81. });
  82. });`;
  83. const getPerformanceSpansSnippet = () => `
  84. using Microsoft.AspNetCore.Mvc;
  85. using Microsoft.Extensions.Logging;
  86. using Sentry;
  87. public class HomeController : Controller
  88. {
  89. private readonly IHub _sentryHub;
  90. public HomeController(IHub sentryHub) => _sentryHub = sentryHub;
  91. [HttpGet("/person/{id}")]
  92. public IActionResult Person(string id)
  93. {
  94. var childSpan = _sentryHub.GetSpan()?.StartChild("additional-work");
  95. try
  96. {
  97. // Do the work that gets measured.
  98. childSpan?.Finish(SpanStatus.Ok);
  99. }
  100. catch (Exception e)
  101. {
  102. childSpan?.Finish(e);
  103. throw;
  104. }
  105. }
  106. }`;
  107. const onboarding: OnboardingConfig = {
  108. install: params => [
  109. {
  110. type: StepType.INSTALL,
  111. description: tct('Install the [strong:NuGet] package:', {
  112. strong: <strong />,
  113. }),
  114. configurations: [
  115. {
  116. partialLoading: params.sourcePackageRegistries.isLoading,
  117. code: [
  118. {
  119. language: 'shell',
  120. label: 'Package Manager',
  121. value: 'packageManager',
  122. code: getInstallSnippetPackageManager(params),
  123. },
  124. {
  125. language: 'shell',
  126. label: '.NET Core CLI',
  127. value: 'coreCli',
  128. code: getInstallSnippetCoreCli(params),
  129. },
  130. ],
  131. },
  132. ...(params.isProfilingSelected
  133. ? [
  134. {
  135. code: [
  136. {
  137. language: 'shell',
  138. label: 'Package Manager',
  139. value: 'packageManager',
  140. code: getInstallProfilingSnippetPackageManager(params),
  141. },
  142. {
  143. language: 'shell',
  144. label: '.NET Core CLI',
  145. value: 'coreCli',
  146. code: getInstallProfilingSnippetCoreCli(params),
  147. },
  148. ],
  149. },
  150. ]
  151. : []),
  152. ],
  153. },
  154. ],
  155. configure: params => [
  156. {
  157. type: StepType.CONFIGURE,
  158. description: tct(
  159. 'Add Sentry to [code:Program.cs] through the [code:WebHostBuilder]:',
  160. {
  161. code: <code />,
  162. }
  163. ),
  164. configurations: [
  165. {
  166. language: 'csharp',
  167. code: getConfigureSnippet(params),
  168. },
  169. ],
  170. },
  171. ],
  172. verify: params => [
  173. {
  174. type: StepType.VERIFY,
  175. description: t('To verify your set up, you can capture a message with the SDK:'),
  176. configurations: [
  177. {
  178. language: 'csharp',
  179. code: 'SentrySdk.CaptureMessage("Hello Sentry");',
  180. },
  181. ],
  182. additionalInfo: tct(
  183. "If you don't want to depend on the static class, the SDK registers a client in the DI container. In this case, you can [link:take [code:IHub] as a dependency].",
  184. {
  185. code: <code />,
  186. link: (
  187. <ExternalLink href="https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/unit-testing/" />
  188. ),
  189. }
  190. ),
  191. },
  192. ...(params.isPerformanceSelected
  193. ? [
  194. {
  195. title: t('Tracing'),
  196. description: tct(
  197. 'You can measure the performance of your endpoints by adding a middleware to [code:Startup.cs]:',
  198. {
  199. code: <code />,
  200. }
  201. ),
  202. configurations: [
  203. {
  204. description: t(
  205. "You'll be able to monitor the performance of your actions automatically. To add additional spans to it, you can use the API:"
  206. ),
  207. language: 'csharp',
  208. code: getPerformanceSpansSnippet(),
  209. },
  210. ],
  211. },
  212. ]
  213. : []),
  214. {
  215. title: t('Samples'),
  216. description: (
  217. <Fragment>
  218. {t(
  219. 'See the following examples that demonstrate how to integrate Sentry with various frameworks.'
  220. )}
  221. <List symbol="bullet">
  222. <ListItem>
  223. {tct(
  224. '[link:Multiple samples in the [code:dotnet] SDK repository] [strong:(C#)]',
  225. {
  226. link: (
  227. <ExternalLink href="https://github.com/getsentry/sentry-dotnet/tree/main/samples" />
  228. ),
  229. code: <code />,
  230. strong: <strong />,
  231. }
  232. )}
  233. </ListItem>
  234. <ListItem>
  235. {tct('[link:Basic F# sample] [strong:(F#)]', {
  236. link: <ExternalLink href="https://github.com/sentry-demos/fsharp" />,
  237. strong: <strong />,
  238. })}
  239. </ListItem>
  240. <ListItem>
  241. {tct('[link:Giraffe F# sample] [strong:(F#)]', {
  242. link: <ExternalLink href="https://github.com/sentry-demos/giraffe" />,
  243. strong: <strong />,
  244. })}
  245. </ListItem>
  246. </List>
  247. </Fragment>
  248. ),
  249. },
  250. ],
  251. };
  252. const crashReportOnboarding: OnboardingConfig = {
  253. introduction: () => getCrashReportModalIntroduction(),
  254. install: (params: Params) => [
  255. {
  256. type: StepType.INSTALL,
  257. configurations: [
  258. getCrashReportSDKInstallFirstStep(params),
  259. {
  260. description: tct(
  261. 'If you are rendering the page from the server, for example on ASP.NET MVC, the [code:Error.cshtml] razor page can be:',
  262. {code: <code />}
  263. ),
  264. code: [
  265. {
  266. label: 'cshtml',
  267. value: 'html',
  268. language: 'html',
  269. code: `@using Sentry
  270. @using Sentry.AspNetCore
  271. @inject Microsoft.Extensions.Options.IOptions<SentryAspNetCoreOptions> SentryOptions
  272. @if (SentrySdk.LastEventId != SentryId.Empty) {
  273. <script>
  274. Sentry.init({ dsn: "@(SentryOptions.Value.Dsn)" });
  275. Sentry.showReportDialog({ eventId: "@SentrySdk.LastEventId" });
  276. </script>
  277. }`,
  278. },
  279. ],
  280. },
  281. ],
  282. },
  283. ],
  284. configure: () => [
  285. {
  286. type: StepType.CONFIGURE,
  287. description: getCrashReportModalConfigDescription({
  288. link: 'https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/user-feedback/configuration/#crash-report-modal',
  289. }),
  290. },
  291. ],
  292. verify: () => [],
  293. nextSteps: () => [],
  294. };
  295. const docs: Docs = {
  296. onboarding,
  297. replayOnboardingJsLoader,
  298. customMetricsOnboarding: getDotnetMetricsOnboarding({packageName: 'Sentry.AspNetCore'}),
  299. crashReportOnboarding,
  300. };
  301. export default docs;