httpSnippet.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {generateSentryTraceHeader} from '@sentry/utils';
  4. import {CodeSnippet} from 'sentry/components/codeSnippet';
  5. import {t} from 'sentry/locale';
  6. import {safeURL} from 'sentry/utils/url/safeURL';
  7. interface Props {
  8. body: string | null;
  9. headers: Array<[key: string, value: string]>;
  10. method: string;
  11. traceSampling: boolean;
  12. url: string;
  13. }
  14. export function HTTPSnippet({body, headers, method, url, traceSampling}: Props) {
  15. const exampleTrace = useMemo(
  16. () =>
  17. generateSentryTraceHeader(undefined, undefined, traceSampling ? undefined : false),
  18. [traceSampling]
  19. );
  20. const urlObject = safeURL(url);
  21. if (urlObject === undefined) {
  22. return null;
  23. }
  24. const pathname = urlObject.search
  25. ? `${urlObject.pathname}${urlObject.search}`
  26. : urlObject.pathname;
  27. const appendedBody = body ? `\r\n${body}` : '';
  28. const additionaLheaders = [
  29. ...headers,
  30. [
  31. 'User-Agent',
  32. 'SentryUptimeBot/1.0 (+http://docs.sentry.io/product/alerts/uptime-monitoring/',
  33. ],
  34. ['Sentry-Trace', exampleTrace],
  35. ];
  36. if (appendedBody !== '') {
  37. additionaLheaders.push(['Content-Size', new Blob([appendedBody]).size.toString()]);
  38. }
  39. const joinedHeaders =
  40. additionaLheaders.map(([key, value]) => `${key}: ${value}`).join('\r\n') + '\r\n';
  41. const request = `${method} ${pathname} HTTP/1.1\r\nHost: ${urlObject.host}\r\n${joinedHeaders}${appendedBody}`;
  42. return (
  43. <MaxSizedSnippet filename={t('Expected Check Request')} language="http">
  44. {request}
  45. </MaxSizedSnippet>
  46. );
  47. }
  48. const MaxSizedSnippet = styled(CodeSnippet)`
  49. pre {
  50. overflow-y: scroll;
  51. max-height: 400px;
  52. }
  53. `;