index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import {PureComponent} from 'react';
  2. import {withRouter, WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Observer} from 'mobx-react';
  5. import Alert from 'sentry/components/alert';
  6. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  7. import {Panel} from 'sentry/components/panels';
  8. import SearchBar from 'sentry/components/searchBar';
  9. import {t, tn} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {Organization} from 'sentry/types';
  12. import {EventTransaction} from 'sentry/types/event';
  13. import {objectIsEmpty} from 'sentry/utils';
  14. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  15. import * as QuickTraceContext from 'sentry/utils/performance/quickTrace/quickTraceContext';
  16. import {TraceError} from 'sentry/utils/performance/quickTrace/types';
  17. import withOrganization from 'sentry/utils/withOrganization';
  18. import * as AnchorLinkManager from './anchorLinkManager';
  19. import Filter from './filter';
  20. import TraceErrorList from './traceErrorList';
  21. import TraceView from './traceView';
  22. import {FocusedSpanIDMap, ParsedTraceType} from './types';
  23. import {getCumulativeAlertLevelFromErrors, parseTrace, scrollToSpan} from './utils';
  24. import WaterfallModel from './waterfallModel';
  25. type Props = {
  26. event: EventTransaction;
  27. organization: Organization;
  28. focusedSpanIds?: FocusedSpanIDMap;
  29. } & WithRouterProps;
  30. type State = {
  31. parsedTrace: ParsedTraceType;
  32. waterfallModel: WaterfallModel;
  33. };
  34. class SpansInterface extends PureComponent<Props, State> {
  35. state: State = {
  36. parsedTrace: parseTrace(this.props.event),
  37. waterfallModel: new WaterfallModel(this.props.event, this.props.focusedSpanIds),
  38. };
  39. static getDerivedStateFromProps(props: Readonly<Props>, state: State): State {
  40. if (state.waterfallModel.isEvent(props.event)) {
  41. return state;
  42. }
  43. return {
  44. ...state,
  45. parsedTrace: parseTrace(props.event),
  46. waterfallModel: new WaterfallModel(props.event, props.focusedSpanIds),
  47. };
  48. }
  49. handleSpanFilter = (searchQuery: string) => {
  50. const {waterfallModel} = this.state;
  51. const {organization} = this.props;
  52. waterfallModel.querySpanSearch(searchQuery);
  53. trackAdvancedAnalyticsEvent('performance_views.event_details.search_query', {
  54. organization,
  55. });
  56. };
  57. renderTraceErrorsAlert({
  58. isLoading,
  59. errors,
  60. parsedTrace,
  61. }: {
  62. errors: TraceError[] | undefined;
  63. isLoading: boolean;
  64. parsedTrace: ParsedTraceType;
  65. }) {
  66. if (isLoading) {
  67. return null;
  68. }
  69. if (!errors || errors.length <= 0) {
  70. return null;
  71. }
  72. // This is intentional as unbalanced string formatters in `tn()` are problematic
  73. const label =
  74. errors.length === 1
  75. ? t('There is an error event associated with this transaction event.')
  76. : tn(
  77. `There are %s error events associated with this transaction event.`,
  78. `There are %s error events associated with this transaction event.`,
  79. errors.length
  80. );
  81. return (
  82. <AlertContainer>
  83. <Alert type={getCumulativeAlertLevelFromErrors(errors)}>
  84. <ErrorLabel>{label}</ErrorLabel>
  85. <AnchorLinkManager.Consumer>
  86. {({scrollToHash}) => (
  87. <TraceErrorList
  88. trace={parsedTrace}
  89. errors={errors}
  90. onClickSpan={(event, spanId) => {
  91. return scrollToSpan(
  92. spanId,
  93. scrollToHash,
  94. this.props.location,
  95. this.props.organization
  96. )(event);
  97. }}
  98. />
  99. )}
  100. </AnchorLinkManager.Consumer>
  101. </Alert>
  102. </AlertContainer>
  103. );
  104. }
  105. render() {
  106. const {event, organization} = this.props;
  107. const {parsedTrace, waterfallModel} = this.state;
  108. return (
  109. <Container hasErrors={!objectIsEmpty(event.errors)}>
  110. <QuickTraceContext.Consumer>
  111. {quickTrace => (
  112. <AnchorLinkManager.Provider>
  113. {this.renderTraceErrorsAlert({
  114. isLoading: quickTrace?.isLoading || false,
  115. errors: quickTrace?.currentEvent?.errors,
  116. parsedTrace,
  117. })}
  118. <Observer>
  119. {() => {
  120. return (
  121. <Search>
  122. <Filter
  123. operationNameCounts={waterfallModel.operationNameCounts}
  124. operationNameFilter={waterfallModel.operationNameFilters}
  125. toggleOperationNameFilter={
  126. waterfallModel.toggleOperationNameFilter
  127. }
  128. />
  129. <StyledSearchBar
  130. defaultQuery=""
  131. query={waterfallModel.searchQuery || ''}
  132. placeholder={t('Search for spans')}
  133. onSearch={this.handleSpanFilter}
  134. />
  135. </Search>
  136. );
  137. }}
  138. </Observer>
  139. <Panel>
  140. <Observer>
  141. {() => {
  142. return (
  143. <TraceView
  144. waterfallModel={waterfallModel}
  145. organization={organization}
  146. />
  147. );
  148. }}
  149. </Observer>
  150. <GuideAnchorWrapper>
  151. <GuideAnchor target="span_tree" position="bottom" />
  152. </GuideAnchorWrapper>
  153. </Panel>
  154. </AnchorLinkManager.Provider>
  155. )}
  156. </QuickTraceContext.Consumer>
  157. </Container>
  158. );
  159. }
  160. }
  161. const GuideAnchorWrapper = styled('div')`
  162. height: 0;
  163. width: 0;
  164. margin-left: 50%;
  165. `;
  166. const Container = styled('div')<{hasErrors: boolean}>`
  167. ${p =>
  168. p.hasErrors &&
  169. `
  170. padding: ${space(2)} 0;
  171. @media (min-width: ${p.theme.breakpoints.small}) {
  172. padding: ${space(3)} 0 0 0;
  173. }
  174. `}
  175. `;
  176. const Search = styled('div')`
  177. display: grid;
  178. gap: ${space(2)};
  179. grid-template-columns: max-content 1fr;
  180. width: 100%;
  181. margin-bottom: ${space(2)};
  182. `;
  183. const StyledSearchBar = styled(SearchBar)`
  184. flex-grow: 1;
  185. `;
  186. const AlertContainer = styled('div')`
  187. margin-bottom: ${space(1)};
  188. `;
  189. const ErrorLabel = styled('div')`
  190. margin-bottom: ${space(1)};
  191. `;
  192. export default withRouter(withOrganization(SpansInterface));