newTraceDetailsTransactionBar.tsx 30 KB

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