newTraceDetailsTransactionBar.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. import {createRef, Fragment, useCallback, useEffect, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import omit from 'lodash/omit';
  6. import {Observer} from 'mobx-react';
  7. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  8. import Count from 'sentry/components/count';
  9. import * as DividerHandlerManager from 'sentry/components/events/interfaces/spans/dividerHandlerManager';
  10. import {SpanDetailProps} from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanDetails';
  11. import NewTraceDetailsSpanTree from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanTree';
  12. import * as ScrollbarManager from 'sentry/components/events/interfaces/spans/scrollbarManager';
  13. import * as SpanContext from 'sentry/components/events/interfaces/spans/spanContext';
  14. import {MeasurementMarker} from 'sentry/components/events/interfaces/spans/styles';
  15. import {
  16. getMeasurementBounds,
  17. SpanBoundsType,
  18. SpanGeneratedBoundsType,
  19. transactionTargetHash,
  20. VerticalMark,
  21. } from 'sentry/components/events/interfaces/spans/utils';
  22. import WaterfallModel from 'sentry/components/events/interfaces/spans/waterfallModel';
  23. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  24. import Link from 'sentry/components/links/link';
  25. import {ROW_HEIGHT, SpanBarType} from 'sentry/components/performance/waterfall/constants';
  26. import {MessageRow} from 'sentry/components/performance/waterfall/messageRow';
  27. import {
  28. Row,
  29. RowCell,
  30. RowCellContainer,
  31. RowReplayTimeIndicators,
  32. } from 'sentry/components/performance/waterfall/row';
  33. import {DurationPill, RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
  34. import {
  35. DividerContainer,
  36. DividerLine,
  37. DividerLineGhostContainer,
  38. EmbeddedTransactionBadge,
  39. ErrorBadge,
  40. } from 'sentry/components/performance/waterfall/rowDivider';
  41. import {
  42. RowTitle,
  43. RowTitleContainer,
  44. RowTitleContent,
  45. } from 'sentry/components/performance/waterfall/rowTitle';
  46. import {
  47. ConnectorBar,
  48. TOGGLE_BORDER_BOX,
  49. TreeConnector,
  50. TreeToggle,
  51. TreeToggleContainer,
  52. TreeToggleIcon,
  53. } from 'sentry/components/performance/waterfall/treeConnector';
  54. import {
  55. getDurationDisplay,
  56. getHumanDuration,
  57. } from 'sentry/components/performance/waterfall/utils';
  58. import {TransactionProfileIdProvider} from 'sentry/components/profiling/transactionProfileIdProvider';
  59. import {generateIssueEventTarget} from 'sentry/components/quickTrace/utils';
  60. import {Tooltip} from 'sentry/components/tooltip';
  61. import {t} from 'sentry/locale';
  62. import {EventTransaction, Organization} from 'sentry/types';
  63. import {defined} from 'sentry/utils';
  64. import toPercent from 'sentry/utils/number/toPercent';
  65. import QuickTraceQuery from 'sentry/utils/performance/quickTrace/quickTraceQuery';
  66. import {TraceError, TraceFullDetailed} from 'sentry/utils/performance/quickTrace/types';
  67. import {
  68. isTraceError,
  69. isTraceRoot,
  70. isTraceTransaction,
  71. } from 'sentry/utils/performance/quickTrace/utils';
  72. import Projects from 'sentry/utils/projects';
  73. import {useApiQuery} from 'sentry/utils/queryClient';
  74. import useRouter from 'sentry/utils/useRouter';
  75. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  76. import {ProfileContext, ProfilesProvider} from 'sentry/views/profiling/profilesProvider';
  77. import {EventDetail} from './newTraceDetailsContent';
  78. import {ProjectBadgeContainer} from './styles';
  79. import {TraceInfo, TraceRoot, TreeDepth} from './types';
  80. import {shortenErrorTitle} from './utils';
  81. const MARGIN_LEFT = 0;
  82. type Props = {
  83. addContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  84. continuingDepths: TreeDepth[];
  85. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  86. hasGuideAnchor: boolean;
  87. index: number;
  88. isExpanded: boolean;
  89. isLast: boolean;
  90. isOrphan: boolean;
  91. isVisible: boolean;
  92. location: Location;
  93. onWheel: (deltaX: number) => void;
  94. organization: Organization;
  95. removeContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  96. toggleExpandedState: () => void;
  97. traceInfo: TraceInfo;
  98. traceViewRef: React.RefObject<HTMLDivElement>;
  99. transaction: TraceRoot | TraceFullDetailed | TraceError;
  100. barColor?: string;
  101. isOrphanError?: boolean;
  102. measurements?: Map<number, VerticalMark>;
  103. numOfOrphanErrors?: number;
  104. onRowClick?: (detailKey: EventDetail | SpanDetailProps | undefined) => void;
  105. onlyOrphanErrors?: boolean;
  106. };
  107. function NewTraceDetailsTransactionBar(props: Props) {
  108. const detail_id = props.location.query.detail;
  109. const span_id = props.location.query.span;
  110. const detailInQueryParam = !!(
  111. isTraceTransaction(props.transaction) &&
  112. detail_id &&
  113. detail_id === props.transaction.event_id
  114. );
  115. const isHighlighted = !!(!span_id && detailInQueryParam);
  116. const highlightEmbeddedSpan = !!(span_id && detailInQueryParam);
  117. const [showEmbeddedChildren, setShowEmbeddedChildren] = useState(highlightEmbeddedSpan);
  118. const transactionRowDOMRef = createRef<HTMLDivElement>();
  119. const transactionTitleRef = createRef<HTMLDivElement>();
  120. let spanContentRef: HTMLDivElement | null = null;
  121. const router = useRouter();
  122. const handleWheel = useCallback(
  123. (event: WheelEvent) => {
  124. // https://stackoverflow.com/q/57358640
  125. // https://github.com/facebook/react/issues/14856
  126. if (Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
  127. return;
  128. }
  129. event.preventDefault();
  130. event.stopPropagation();
  131. if (Math.abs(event.deltaY) === Math.abs(event.deltaX)) {
  132. return;
  133. }
  134. const {onWheel} = props;
  135. onWheel(event.deltaX);
  136. },
  137. [props]
  138. );
  139. const scrollIntoView = useCallback(() => {
  140. const element = transactionRowDOMRef.current;
  141. if (!element) {
  142. return;
  143. }
  144. const boundingRect = element.getBoundingClientRect();
  145. const offset = boundingRect.top + window.scrollY;
  146. window.scrollTo(0, offset);
  147. }, [transactionRowDOMRef]);
  148. useEffect(() => {
  149. const {location, transaction} = props;
  150. const transactionTitleRefCurrentCopy = transactionTitleRef.current;
  151. if (
  152. 'event_id' in transaction &&
  153. transactionTargetHash(transaction.event_id) === location.hash
  154. ) {
  155. scrollIntoView();
  156. }
  157. if (transactionTitleRefCurrentCopy) {
  158. transactionTitleRefCurrentCopy.addEventListener('wheel', handleWheel, {
  159. passive: false,
  160. });
  161. }
  162. return () => {
  163. if (transactionTitleRefCurrentCopy) {
  164. transactionTitleRefCurrentCopy.removeEventListener('wheel', handleWheel);
  165. }
  166. };
  167. }, [handleWheel, props, scrollIntoView, transactionTitleRef]);
  168. const transactionEvent =
  169. isTraceTransaction<TraceFullDetailed>(props.transaction) ||
  170. isTraceError(props.transaction)
  171. ? props.transaction
  172. : undefined;
  173. const {
  174. data: embeddedChildren,
  175. isLoading: isEmbeddedChildrenLoading,
  176. error: embeddedChildrenError,
  177. } = useApiQuery<EventTransaction>(
  178. [
  179. `/organizations/${props.organization.slug}/events/${transactionEvent?.project_slug}:${transactionEvent?.event_id}/`,
  180. ],
  181. {
  182. staleTime: 2 * 60 * 1000,
  183. enabled: showEmbeddedChildren || isHighlighted,
  184. }
  185. );
  186. useEffect(() => {
  187. if (isTraceTransaction(props.transaction)) {
  188. if (isHighlighted && props.onRowClick) {
  189. props.onRowClick({
  190. traceFullDetailedEvent: props.transaction,
  191. event: embeddedChildren,
  192. });
  193. }
  194. }
  195. }, [isHighlighted, embeddedChildren, props, props.transaction]);
  196. const renderEmbeddedChildrenState = () => {
  197. if (showEmbeddedChildren) {
  198. if (isEmbeddedChildrenLoading) {
  199. return (
  200. <MessageRow>
  201. <span>{t('Loading embedded transaction')}</span>
  202. </MessageRow>
  203. );
  204. }
  205. if (embeddedChildrenError) {
  206. return (
  207. <MessageRow>
  208. <span>{t('Error loading embedded transaction')}</span>
  209. </MessageRow>
  210. );
  211. }
  212. }
  213. return null;
  214. };
  215. const handleRowCellClick = () => {
  216. const {transaction, organization, location} = props;
  217. if (isTraceError(transaction)) {
  218. browserHistory.push(generateIssueEventTarget(transaction, organization));
  219. }
  220. if (isTraceTransaction<TraceFullDetailed>(transaction)) {
  221. router.replace({
  222. pathname: location.pathname,
  223. query: isHighlighted
  224. ? omit(router.location.query, 'detail')
  225. : {...location.query, detail: transaction.event_id},
  226. });
  227. }
  228. };
  229. const getCurrentOffset = () => {
  230. const {transaction} = props;
  231. const {generation} = transaction;
  232. return getOffset(generation);
  233. };
  234. const renderMeasurements = () => {
  235. const {measurements, generateBounds} = props;
  236. if (!measurements) {
  237. return null;
  238. }
  239. return (
  240. <Fragment>
  241. {Array.from(measurements.values()).map(verticalMark => {
  242. const mark = Object.values(verticalMark.marks)[0];
  243. const {timestamp} = mark;
  244. const bounds = getMeasurementBounds(timestamp, generateBounds);
  245. const shouldDisplay = defined(bounds.left) && defined(bounds.width);
  246. if (!shouldDisplay || !bounds.isSpanVisibleInView) {
  247. return null;
  248. }
  249. return (
  250. <MeasurementMarker
  251. key={String(timestamp)}
  252. style={{
  253. left: `clamp(0%, ${toPercent(bounds.left || 0)}, calc(100% - 1px))`,
  254. }}
  255. failedThreshold={verticalMark.failedThreshold}
  256. />
  257. );
  258. })}
  259. </Fragment>
  260. );
  261. };
  262. const renderConnector = (hasToggle: boolean) => {
  263. const {continuingDepths, isExpanded, isOrphan, isLast, transaction} = props;
  264. const {generation = 0} = transaction;
  265. const eventId =
  266. isTraceTransaction<TraceFullDetailed>(transaction) || isTraceError(transaction)
  267. ? transaction.event_id
  268. : transaction.traceSlug;
  269. if (generation === 0) {
  270. if (hasToggle) {
  271. return (
  272. <ConnectorBar
  273. style={{right: '15px', height: '10px', bottom: '-5px', top: 'auto'}}
  274. orphanBranch={false}
  275. />
  276. );
  277. }
  278. return null;
  279. }
  280. const connectorBars: Array<React.ReactNode> = continuingDepths.map(
  281. ({depth, isOrphanDepth}) => {
  282. if (generation - depth <= 1) {
  283. // If the difference is less than or equal to 1, then it means that the continued
  284. // bar is from its direct parent. In this case, do not render a connector bar
  285. // because the tree connector below will suffice.
  286. return null;
  287. }
  288. const left = -1 * getOffset(generation - depth - 1) - 2;
  289. return (
  290. <ConnectorBar
  291. style={{left}}
  292. key={`${eventId}-${depth}`}
  293. orphanBranch={isOrphanDepth}
  294. />
  295. );
  296. }
  297. );
  298. if (hasToggle && (isExpanded || showEmbeddedChildren)) {
  299. connectorBars.push(
  300. <ConnectorBar
  301. style={{
  302. right: '15px',
  303. height: '10px',
  304. bottom: isLast ? `-${ROW_HEIGHT / 2 + 1}px` : '0',
  305. top: 'auto',
  306. }}
  307. key={`${eventId}-last`}
  308. orphanBranch={false}
  309. />
  310. );
  311. }
  312. return (
  313. <TreeConnector isLast={isLast} hasToggler={hasToggle} orphanBranch={isOrphan}>
  314. {connectorBars}
  315. </TreeConnector>
  316. );
  317. };
  318. const renderEmbeddedTransactionsBadge = (): React.ReactNode => {
  319. return (
  320. <Tooltip
  321. title={
  322. <span>
  323. {showEmbeddedChildren
  324. ? t(
  325. 'This transaction is showing a direct child. Remove transaction to hide'
  326. )
  327. : t('This transaction has a direct child. Add transaction to view')}
  328. </span>
  329. }
  330. position="top"
  331. containerDisplayMode="block"
  332. >
  333. <EmbeddedTransactionBadge
  334. inTraceView
  335. expanded={showEmbeddedChildren}
  336. onClick={() => {
  337. setShowEmbeddedChildren(prev => !prev);
  338. if (
  339. (props.isExpanded && !showEmbeddedChildren) ||
  340. (!props.isExpanded && showEmbeddedChildren)
  341. ) {
  342. props.toggleExpandedState();
  343. }
  344. }}
  345. />
  346. </Tooltip>
  347. );
  348. };
  349. const renderEmbeddedChildren = () => {
  350. if (!embeddedChildren || !showEmbeddedChildren) {
  351. return null;
  352. }
  353. const {organization, traceViewRef, location, isLast, traceInfo} = props;
  354. const waterfallModel = new WaterfallModel(embeddedChildren);
  355. const profileId = embeddedChildren.contexts?.profile?.profile_id ?? null;
  356. return (
  357. <Fragment>
  358. <QuickTraceQuery
  359. event={embeddedChildren}
  360. location={location}
  361. orgSlug={organization.slug}
  362. >
  363. {results => (
  364. <ProfilesProvider
  365. orgSlug={organization.slug}
  366. projectSlug={embeddedChildren.projectSlug ?? ''}
  367. profileId={profileId || ''}
  368. >
  369. <ProfileContext.Consumer>
  370. {profiles => (
  371. <ProfileGroupProvider
  372. type="flamechart"
  373. input={profiles?.type === 'resolved' ? profiles.data : null}
  374. traceID={profileId || ''}
  375. >
  376. <TransactionProfileIdProvider
  377. projectId={embeddedChildren.projectID}
  378. timestamp={embeddedChildren.dateReceived}
  379. transactionId={embeddedChildren.id}
  380. >
  381. <SpanContext.Provider>
  382. <SpanContext.Consumer>
  383. {spanContextProps => (
  384. <Observer>
  385. {() => (
  386. <NewTraceDetailsSpanTree
  387. quickTrace={results}
  388. location={props.location}
  389. onRowClick={props.onRowClick}
  390. traceInfo={traceInfo}
  391. traceViewHeaderRef={traceViewRef}
  392. traceViewRef={traceViewRef}
  393. parentHasContinuingDepths={
  394. props.continuingDepths.length > 0
  395. }
  396. traceHasMultipleRoots={props.continuingDepths.some(
  397. c => c.depth === 0 && c.isOrphanDepth
  398. )}
  399. parentIsLast={isLast}
  400. parentGeneration={transaction.generation ?? 0}
  401. organization={organization}
  402. waterfallModel={waterfallModel}
  403. filterSpans={waterfallModel.filterSpans}
  404. spans={waterfallModel
  405. .getWaterfall({
  406. viewStart: 0,
  407. viewEnd: 1,
  408. traceInfo,
  409. })
  410. .slice(1)}
  411. focusedSpanIds={waterfallModel.focusedSpanIds}
  412. spanContextProps={spanContextProps}
  413. operationNameFilters={
  414. waterfallModel.operationNameFilters
  415. }
  416. />
  417. )}
  418. </Observer>
  419. )}
  420. </SpanContext.Consumer>
  421. </SpanContext.Provider>
  422. </TransactionProfileIdProvider>
  423. </ProfileGroupProvider>
  424. )}
  425. </ProfileContext.Consumer>
  426. </ProfilesProvider>
  427. )}
  428. </QuickTraceQuery>
  429. </Fragment>
  430. );
  431. };
  432. const renderToggle = (errored: boolean) => {
  433. const {isExpanded, transaction, toggleExpandedState, numOfOrphanErrors} = props;
  434. const left = getCurrentOffset();
  435. const hasOrphanErrors = numOfOrphanErrors && numOfOrphanErrors > 0;
  436. let childrenLength =
  437. (!isTraceError(transaction) && transaction.children?.length) || 0;
  438. const generation = transaction.generation || 0;
  439. if (childrenLength <= 0 && !hasOrphanErrors && !showEmbeddedChildren) {
  440. return (
  441. <TreeToggleContainer style={{left: `${left}px`}}>
  442. {renderConnector(false)}
  443. </TreeToggleContainer>
  444. );
  445. }
  446. if (showEmbeddedChildren && embeddedChildren) {
  447. const waterfallModel = new WaterfallModel(embeddedChildren);
  448. childrenLength = waterfallModel.rootSpan.children.length;
  449. } else {
  450. childrenLength = childrenLength + (numOfOrphanErrors ?? 0);
  451. }
  452. const isRoot = generation === 0;
  453. return (
  454. <TreeToggleContainer style={{left: `${left}px`}} hasToggler>
  455. {renderConnector(true)}
  456. <TreeToggle
  457. disabled={isRoot}
  458. isExpanded={isExpanded}
  459. errored={errored}
  460. onClick={event => {
  461. event.stopPropagation();
  462. if (isRoot || showEmbeddedChildren) {
  463. return;
  464. }
  465. toggleExpandedState();
  466. setShowEmbeddedChildren(false);
  467. }}
  468. >
  469. <Count value={childrenLength} />
  470. {!isRoot && !showEmbeddedChildren && (
  471. <div>
  472. <TreeToggleIcon direction={isExpanded ? 'up' : 'down'} />
  473. </div>
  474. )}
  475. </TreeToggle>
  476. </TreeToggleContainer>
  477. );
  478. };
  479. const renderTitle = (_: ScrollbarManager.ScrollbarManagerChildrenProps) => {
  480. const {organization, transaction, addContentSpanBarRef, removeContentSpanBarRef} =
  481. props;
  482. const left = getCurrentOffset();
  483. const errored = isTraceTransaction<TraceFullDetailed>(transaction)
  484. ? transaction.errors &&
  485. transaction.errors.length + transaction.performance_issues.length > 0
  486. : false;
  487. const projectBadge = (isTraceTransaction<TraceFullDetailed>(transaction) ||
  488. isTraceError(transaction)) && (
  489. <Projects orgId={organization.slug} slugs={[transaction.project_slug]}>
  490. {({projects}) => {
  491. const project = projects.find(p => p.slug === transaction.project_slug);
  492. return (
  493. <ProjectBadgeContainer>
  494. <Tooltip title={transaction.project_slug}>
  495. <ProjectBadge
  496. project={project ? project : {slug: transaction.project_slug}}
  497. avatarSize={16}
  498. hideName
  499. />
  500. </Tooltip>
  501. </ProjectBadgeContainer>
  502. );
  503. }}
  504. </Projects>
  505. );
  506. const content = isTraceError(transaction) ? (
  507. <Fragment>
  508. {projectBadge}
  509. <RowTitleContent errored>
  510. <ErrorLink to={generateIssueEventTarget(transaction, organization)}>
  511. <strong>{'Unknown \u2014 '}</strong>
  512. {shortenErrorTitle(transaction.title)}
  513. </ErrorLink>
  514. </RowTitleContent>
  515. </Fragment>
  516. ) : isTraceTransaction<TraceFullDetailed>(transaction) ? (
  517. <Fragment>
  518. {projectBadge}
  519. <RowTitleContent errored={errored}>
  520. <strong>
  521. {transaction['transaction.op']}
  522. {' \u2014 '}
  523. </strong>
  524. {transaction.transaction}
  525. </RowTitleContent>
  526. </Fragment>
  527. ) : (
  528. <RowTitleContent errored={false}>
  529. <strong>{'Trace \u2014 '}</strong>
  530. {transaction.traceSlug}
  531. </RowTitleContent>
  532. );
  533. return (
  534. <RowTitleContainer
  535. ref={ref => {
  536. if (!ref) {
  537. removeContentSpanBarRef(spanContentRef);
  538. return;
  539. }
  540. addContentSpanBarRef(ref);
  541. spanContentRef = ref;
  542. }}
  543. >
  544. {renderToggle(errored)}
  545. <RowTitle
  546. style={{
  547. left: `${left}px`,
  548. width: '100%',
  549. }}
  550. >
  551. {content}
  552. </RowTitle>
  553. </RowTitleContainer>
  554. );
  555. };
  556. const renderDivider = (
  557. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  558. ) => {
  559. if (isHighlighted) {
  560. // Mock component to preserve layout spacing
  561. return (
  562. <DividerLine
  563. showDetail={isHighlighted}
  564. style={{
  565. position: 'absolute',
  566. }}
  567. />
  568. );
  569. }
  570. const {addDividerLineRef} = dividerHandlerChildrenProps;
  571. return (
  572. <DividerLine
  573. ref={addDividerLineRef()}
  574. style={{
  575. position: 'absolute',
  576. }}
  577. onMouseEnter={() => {
  578. dividerHandlerChildrenProps.setHover(true);
  579. }}
  580. onMouseLeave={() => {
  581. dividerHandlerChildrenProps.setHover(false);
  582. }}
  583. onMouseOver={() => {
  584. dividerHandlerChildrenProps.setHover(true);
  585. }}
  586. onMouseDown={e => {
  587. dividerHandlerChildrenProps.onDragStart(e);
  588. }}
  589. onClick={event => {
  590. // we prevent the propagation of the clicks from this component to prevent
  591. // the span detail from being opened.
  592. event.stopPropagation();
  593. }}
  594. />
  595. );
  596. };
  597. const renderGhostDivider = (
  598. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  599. ) => {
  600. const {dividerPosition, addGhostDividerLineRef} = dividerHandlerChildrenProps;
  601. return (
  602. <DividerLineGhostContainer
  603. style={{
  604. width: `calc(${toPercent(dividerPosition)} + 0.5px)`,
  605. display: 'none',
  606. }}
  607. >
  608. <DividerLine
  609. ref={addGhostDividerLineRef()}
  610. style={{
  611. right: 0,
  612. }}
  613. className="hovering"
  614. onClick={event => {
  615. // the ghost divider line should not be interactive.
  616. // we prevent the propagation of the clicks from this component to prevent
  617. // the span detail from being opened.
  618. event.stopPropagation();
  619. }}
  620. />
  621. </DividerLineGhostContainer>
  622. );
  623. };
  624. const renderErrorBadge = () => {
  625. const {transaction} = props;
  626. if (
  627. isTraceRoot(transaction) ||
  628. isTraceError(transaction) ||
  629. !(transaction.errors.length + transaction.performance_issues.length)
  630. ) {
  631. return null;
  632. }
  633. return <ErrorBadge />;
  634. };
  635. const renderRectangle = () => {
  636. const {transaction, traceInfo, barColor} = props;
  637. // Use 1 as the difference in the case that startTimestamp === endTimestamp
  638. const delta = Math.abs(traceInfo.endTimestamp - traceInfo.startTimestamp) || 1;
  639. const start_timestamp = isTraceError(transaction)
  640. ? transaction.timestamp
  641. : transaction.start_timestamp;
  642. if (!(start_timestamp && transaction.timestamp)) {
  643. return null;
  644. }
  645. const startPosition = Math.abs(start_timestamp - traceInfo.startTimestamp);
  646. const startPercentage = startPosition / delta;
  647. const duration = Math.abs(transaction.timestamp - start_timestamp);
  648. const widthPercentage = duration / delta;
  649. return (
  650. <StyledRowRectangle
  651. style={{
  652. backgroundColor: barColor,
  653. left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
  654. width: toPercent(widthPercentage || 0),
  655. }}
  656. >
  657. {renderPerformanceIssues()}
  658. {isTraceError(transaction) ? (
  659. <ErrorBadge />
  660. ) : (
  661. <Fragment>
  662. {renderErrorBadge()}
  663. <DurationPill
  664. durationDisplay={getDurationDisplay({
  665. left: startPercentage,
  666. width: widthPercentage,
  667. })}
  668. showDetail={isHighlighted}
  669. >
  670. {getHumanDuration(duration)}
  671. </DurationPill>
  672. </Fragment>
  673. )}
  674. </StyledRowRectangle>
  675. );
  676. };
  677. const renderPerformanceIssues = () => {
  678. const {transaction, barColor} = props;
  679. if (isTraceError(transaction) || isTraceRoot(transaction)) {
  680. return null;
  681. }
  682. const rows: React.ReactElement[] = [];
  683. // Use 1 as the difference in the case that startTimestamp === endTimestamp
  684. const delta = Math.abs(transaction.timestamp - transaction.start_timestamp) || 1;
  685. for (let i = 0; i < transaction.performance_issues.length; i++) {
  686. const issue = transaction.performance_issues[i];
  687. const startPosition = Math.abs(issue.start - transaction.start_timestamp);
  688. const startPercentage = startPosition / delta;
  689. const duration = Math.abs(issue.end - issue.start);
  690. const widthPercentage = duration / delta;
  691. rows.push(
  692. <RowRectangle
  693. style={{
  694. backgroundColor: barColor,
  695. left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
  696. width: toPercent(widthPercentage || 0),
  697. }}
  698. spanBarType={SpanBarType.AFFECTED}
  699. />
  700. );
  701. }
  702. return rows;
  703. };
  704. const renderHeader = ({
  705. dividerHandlerChildrenProps,
  706. scrollbarManagerChildrenProps,
  707. }: {
  708. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps;
  709. scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps;
  710. }) => {
  711. const {hasGuideAnchor, index, transaction, onlyOrphanErrors = false} = props;
  712. const {dividerPosition} = dividerHandlerChildrenProps;
  713. const hideDurationRectangle = isTraceRoot(transaction) && onlyOrphanErrors;
  714. return (
  715. <RowCellContainer showDetail={isHighlighted}>
  716. <RowCell
  717. data-test-id="transaction-row-title"
  718. data-type="span-row-cell"
  719. style={{
  720. width: `calc(${toPercent(dividerPosition)} - 0.5px)`,
  721. paddingTop: 0,
  722. }}
  723. showDetail={isHighlighted}
  724. onClick={handleRowCellClick}
  725. ref={transactionTitleRef}
  726. >
  727. <GuideAnchor target="trace_view_guide_row" disabled={!hasGuideAnchor}>
  728. {renderTitle(scrollbarManagerChildrenProps)}
  729. </GuideAnchor>
  730. </RowCell>
  731. <DividerContainer>
  732. {renderDivider(dividerHandlerChildrenProps)}
  733. {!isTraceRoot(transaction) &&
  734. !isTraceError(transaction) &&
  735. renderEmbeddedTransactionsBadge()}
  736. </DividerContainer>
  737. <RowCell
  738. data-test-id="transaction-row-duration"
  739. data-type="span-row-cell"
  740. showStriping={index % 2 !== 0}
  741. style={{
  742. width: `calc(${toPercent(1 - dividerPosition)} - 0.5px)`,
  743. paddingTop: 0,
  744. overflow: 'visible',
  745. }}
  746. showDetail={isHighlighted}
  747. onClick={handleRowCellClick}
  748. >
  749. <RowReplayTimeIndicators />
  750. <GuideAnchor target="trace_view_guide_row_details" disabled={!hasGuideAnchor}>
  751. {!hideDurationRectangle && renderRectangle()}
  752. {renderMeasurements()}
  753. </GuideAnchor>
  754. </RowCell>
  755. {!isHighlighted && renderGhostDivider(dividerHandlerChildrenProps)}
  756. </RowCellContainer>
  757. );
  758. };
  759. const {isVisible, transaction} = props;
  760. return (
  761. <Wrapper showingChildren={showEmbeddedChildren}>
  762. <StyledRow
  763. ref={transactionRowDOMRef}
  764. visible={isVisible}
  765. showBorder={isHighlighted}
  766. cursor={
  767. isTraceTransaction<TraceFullDetailed>(transaction) ? 'pointer' : 'default'
  768. }
  769. >
  770. <ScrollbarManager.Consumer>
  771. {scrollbarManagerChildrenProps => (
  772. <DividerHandlerManager.Consumer>
  773. {dividerHandlerChildrenProps =>
  774. renderHeader({
  775. dividerHandlerChildrenProps,
  776. scrollbarManagerChildrenProps,
  777. })
  778. }
  779. </DividerHandlerManager.Consumer>
  780. )}
  781. </ScrollbarManager.Consumer>
  782. </StyledRow>
  783. {renderEmbeddedChildrenState()}
  784. {renderEmbeddedChildren()}
  785. </Wrapper>
  786. );
  787. }
  788. function getOffset(generation) {
  789. return generation * (TOGGLE_BORDER_BOX / 2) + MARGIN_LEFT;
  790. }
  791. export default NewTraceDetailsTransactionBar;
  792. const StyledRow = styled(Row)`
  793. &,
  794. ${RowCellContainer} {
  795. overflow: visible;
  796. }
  797. `;
  798. const Wrapper = styled('div')<{showingChildren: boolean}>`
  799. ${p =>
  800. p.showingChildren &&
  801. 'outline: 1px solid black; border-top: 1px solid black; border-bottom: 1px solid black; border-radius: 2px;'}
  802. `;
  803. const ErrorLink = styled(Link)`
  804. color: ${p => p.theme.error};
  805. `;
  806. const StyledRowRectangle = styled(RowRectangle)`
  807. display: flex;
  808. align-items: center;
  809. `;