content.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Alert} from 'sentry/components/alert';
  4. import FeatureBadge from 'sentry/components/badge/featureBadge';
  5. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  6. import * as Layout from 'sentry/components/layouts/thirds';
  7. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  8. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  9. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  10. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  11. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  12. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import {browserHistory} from 'sentry/utils/browserHistory';
  17. import {decodeInteger} from 'sentry/utils/queryString';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {ExploreContent} from 'sentry/views/explore/content';
  21. import * as ModuleLayout from 'sentry/views/insights/common/components/moduleLayout';
  22. import {usePageParams} from './hooks/usePageParams';
  23. import {useTraces} from './hooks/useTraces';
  24. import {TracesChart} from './tracesChart';
  25. import {TracesSearchBar} from './tracesSearchBar';
  26. import {TracesTable} from './tracesTable';
  27. import {normalizeTraces} from './utils';
  28. const TRACE_EXPLORER_DOCS_URL = 'https://docs.sentry.io/product/explore/traces/';
  29. const DEFAULT_STATS_PERIOD = '24h';
  30. const DEFAULT_PER_PAGE = 50;
  31. export default function Wrapper(props) {
  32. const location = useLocation();
  33. const organization = useOrganization();
  34. if (
  35. location.query.view !== 'trace' &&
  36. organization.features.includes('visibility-explore-view')
  37. ) {
  38. return <ExploreContent {...props} />;
  39. }
  40. return <Content {...props} />;
  41. }
  42. function Content() {
  43. const location = useLocation();
  44. const organization = useOrganization();
  45. const limit = useMemo(() => {
  46. return decodeInteger(location.query.perPage, DEFAULT_PER_PAGE);
  47. }, [location.query.perPage]);
  48. const {queries} = usePageParams(location);
  49. const handleSearch = useCallback(
  50. (searchIndex: number, searchQuery: string) => {
  51. const newQueries = [...queries];
  52. if (newQueries.length === 0) {
  53. // In the odd case someone wants to add search bars before any query has been made, we add both the default one shown and a new one.
  54. newQueries[0] = '';
  55. }
  56. newQueries[searchIndex] = searchQuery;
  57. browserHistory.push({
  58. ...location,
  59. query: {
  60. ...location.query,
  61. cursor: undefined,
  62. query: typeof searchQuery === 'string' ? newQueries : queries,
  63. },
  64. });
  65. },
  66. [location, queries]
  67. );
  68. const handleClearSearch = useCallback(
  69. (searchIndex: number) => {
  70. const newQueries = [...queries];
  71. if (typeof newQueries[searchIndex] !== undefined) {
  72. delete newQueries[searchIndex];
  73. browserHistory.push({
  74. ...location,
  75. query: {
  76. ...location.query,
  77. cursor: undefined,
  78. query: newQueries,
  79. },
  80. });
  81. return true;
  82. }
  83. return false;
  84. },
  85. [location, queries]
  86. );
  87. const tracesQuery = useTraces({
  88. limit,
  89. query: queries,
  90. });
  91. const isLoading = tracesQuery.isFetching;
  92. const isError = !isLoading && tracesQuery.isError;
  93. const isEmpty = !isLoading && !isError && (tracesQuery?.data?.data?.length ?? 0) === 0;
  94. const rawData = !isLoading && !isError ? tracesQuery?.data?.data : undefined;
  95. const data = normalizeTraces(rawData);
  96. return (
  97. <SentryDocumentTitle title={t('Traces')} orgSlug={organization.slug}>
  98. <PageFiltersContainer
  99. defaultSelection={{
  100. datetime: {start: null, end: null, utc: null, period: DEFAULT_STATS_PERIOD},
  101. }}
  102. >
  103. <Layout.Page>
  104. <Layout.Header>
  105. <Layout.HeaderContent>
  106. <HeaderContentBar>
  107. <Layout.Title>
  108. {t('Traces')}
  109. <PageHeadingQuestionTooltip
  110. docsUrl={TRACE_EXPLORER_DOCS_URL}
  111. title={t(
  112. 'Traces lets you search for individual spans that make up a trace, linked by a trace id.'
  113. )}
  114. />
  115. <FeatureBadge type="beta" />
  116. </Layout.Title>
  117. <FeedbackWidgetButton />
  118. </HeaderContentBar>
  119. </Layout.HeaderContent>
  120. </Layout.Header>
  121. <Layout.Body>
  122. <LayoutMain fullWidth>
  123. <PageFilterBar condensed>
  124. <ProjectPageFilter />
  125. <EnvironmentPageFilter />
  126. <DatePageFilter defaultPeriod="2h" />
  127. </PageFilterBar>
  128. {isError && typeof tracesQuery.error?.responseJSON?.detail === 'string' ? (
  129. <StyledAlert type="error" showIcon>
  130. {tracesQuery.error?.responseJSON?.detail}
  131. </StyledAlert>
  132. ) : null}
  133. <TracesSearchBar
  134. queries={queries}
  135. handleSearch={handleSearch}
  136. handleClearSearch={handleClearSearch}
  137. />
  138. <ModuleLayout.Full>
  139. <TracesChart />
  140. </ModuleLayout.Full>
  141. <TracesTable
  142. isEmpty={isEmpty}
  143. isError={isError}
  144. isLoading={isLoading}
  145. queries={queries}
  146. data={data}
  147. />
  148. </LayoutMain>
  149. </Layout.Body>
  150. </Layout.Page>
  151. </PageFiltersContainer>
  152. </SentryDocumentTitle>
  153. );
  154. }
  155. const HeaderContentBar = styled('div')`
  156. display: flex;
  157. align-items: center;
  158. justify-content: space-between;
  159. flex-direction: row;
  160. `;
  161. const LayoutMain = styled(Layout.Main)`
  162. display: flex;
  163. flex-direction: column;
  164. gap: ${space(2)};
  165. `;
  166. const StyledAlert = styled(Alert)`
  167. margin-bottom: 0;
  168. `;