newTraceDetailsTransactionBar.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. import {Fragment, useCallback, useEffect, useMemo, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Observer} from 'mobx-react';
  5. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  6. import * as DividerHandlerManager from 'sentry/components/events/interfaces/spans/dividerHandlerManager';
  7. import type {SpanDetailProps} from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanDetails';
  8. import NewTraceDetailsSpanTree from 'sentry/components/events/interfaces/spans/newTraceDetailsSpanTree';
  9. import * as ScrollbarManager from 'sentry/components/events/interfaces/spans/scrollbarManager';
  10. import * as SpanContext from 'sentry/components/events/interfaces/spans/spanContext';
  11. import {MeasurementMarker} from 'sentry/components/events/interfaces/spans/styles';
  12. import type {
  13. SpanBoundsType,
  14. SpanGeneratedBoundsType,
  15. VerticalMark,
  16. } from 'sentry/components/events/interfaces/spans/utils';
  17. import {
  18. getMeasurementBounds,
  19. parseTraceDetailsURLHash,
  20. transactionTargetHash,
  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. ErrorBadge,
  39. } from 'sentry/components/performance/waterfall/rowDivider';
  40. import {
  41. RowTitle,
  42. RowTitleContainer,
  43. RowTitleContent,
  44. } from 'sentry/components/performance/waterfall/rowTitle';
  45. import {
  46. ConnectorBar,
  47. TOGGLE_BORDER_BOX,
  48. TreeConnector,
  49. TreeToggle,
  50. TreeToggleContainer,
  51. TreeToggleIcon,
  52. } from 'sentry/components/performance/waterfall/treeConnector';
  53. import {
  54. getDurationDisplay,
  55. getHumanDuration,
  56. } from 'sentry/components/performance/waterfall/utils';
  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 type {EventTransaction} from 'sentry/types/event';
  63. import type {Organization} from 'sentry/types/organization';
  64. import {defined} from 'sentry/utils';
  65. import toPercent from 'sentry/utils/number/toPercent';
  66. import QuickTraceQuery from 'sentry/utils/performance/quickTrace/quickTraceQuery';
  67. import type {
  68. TraceError,
  69. TraceFullDetailed,
  70. } from 'sentry/utils/performance/quickTrace/types';
  71. import {
  72. isTraceError,
  73. isTraceRoot,
  74. isTraceTransaction,
  75. } from 'sentry/utils/performance/quickTrace/utils';
  76. import Projects from 'sentry/utils/projects';
  77. import {useApiQuery} from 'sentry/utils/queryClient';
  78. import {decodeScalar} from 'sentry/utils/queryString';
  79. import {useNavigate} from 'sentry/utils/useNavigate';
  80. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  81. import {ProfileContext, ProfilesProvider} from 'sentry/views/profiling/profilesProvider';
  82. import type {EventDetail} from './newTraceDetailsContent';
  83. import {ProjectBadgeContainer} from './styles';
  84. import type {TraceInfo, TraceRoot, TreeDepth} from './types';
  85. import {shortenErrorTitle} from './utils';
  86. const MARGIN_LEFT = 0;
  87. const TRANSACTION_BAR_HEIGHT = 24;
  88. type Props = {
  89. addContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  90. continuingDepths: TreeDepth[];
  91. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  92. hasGuideAnchor: boolean;
  93. index: number;
  94. isBarScrolledTo: boolean;
  95. isExpanded: boolean;
  96. isLast: boolean;
  97. isOrphan: boolean;
  98. isVisible: boolean;
  99. location: Location;
  100. onBarScrolledTo: () => void;
  101. onWheel: (deltaX: number) => void;
  102. organization: Organization;
  103. removeContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  104. toggleExpandedState: () => void;
  105. traceInfo: TraceInfo;
  106. traceViewRef: React.RefObject<HTMLDivElement>;
  107. transaction: TraceRoot | TraceFullDetailed | TraceError;
  108. barColor?: string;
  109. isOrphanError?: boolean;
  110. measurements?: Map<number, VerticalMark>;
  111. numOfOrphanErrors?: number;
  112. onRowClick?: (detailKey: EventDetail | SpanDetailProps | undefined) => void;
  113. onlyOrphanErrors?: boolean;
  114. };
  115. function NewTraceDetailsTransactionBar(props: Props) {
  116. const hashValues = parseTraceDetailsURLHash(props.location.hash);
  117. const openPanel = decodeScalar(props.location.query.openPanel);
  118. const eventIDInQueryParam = !!(
  119. isTraceTransaction(props.transaction) &&
  120. hashValues?.eventId &&
  121. hashValues.eventId === props.transaction.event_id
  122. );
  123. const isHighlighted = !!(!hashValues?.spanId && eventIDInQueryParam);
  124. const highlightEmbeddedSpan = !!(hashValues?.spanId && eventIDInQueryParam);
  125. const [showEmbeddedChildren, setShowEmbeddedChildren] = useState(
  126. isHighlighted || highlightEmbeddedSpan
  127. );
  128. const [isIntersecting, setIntersecting] = useState(false);
  129. const transactionRowDOMRef = useRef<HTMLDivElement>(null);
  130. const transactionTitleRef = useRef<HTMLDivElement>(null);
  131. let spanContentRef: HTMLDivElement | null = null;
  132. const navigate = useNavigate();
  133. const handleWheel = useCallback(
  134. (event: WheelEvent) => {
  135. // https://stackoverflow.com/q/57358640
  136. // https://github.com/facebook/react/issues/14856
  137. if (Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
  138. return;
  139. }
  140. event.preventDefault();
  141. event.stopPropagation();
  142. if (Math.abs(event.deltaY) === Math.abs(event.deltaX)) {
  143. return;
  144. }
  145. const {onWheel} = props;
  146. onWheel(event.deltaX);
  147. },
  148. [props]
  149. );
  150. const scrollIntoView = useCallback(() => {
  151. const element = transactionRowDOMRef.current;
  152. if (!element) {
  153. return;
  154. }
  155. const boundingRect = element.getBoundingClientRect();
  156. const offset = boundingRect.top + window.scrollY - TRANSACTION_BAR_HEIGHT;
  157. window.scrollTo(0, offset);
  158. props.onBarScrolledTo();
  159. }, [transactionRowDOMRef, props]);
  160. useEffect(() => {
  161. const {transaction, isBarScrolledTo} = props;
  162. const observer = new IntersectionObserver(([entry]) =>
  163. setIntersecting(entry!.isIntersecting)
  164. );
  165. if (transactionRowDOMRef.current) {
  166. observer.observe(transactionRowDOMRef.current);
  167. }
  168. if (
  169. 'event_id' in transaction &&
  170. hashValues?.eventId === transaction.event_id &&
  171. !isIntersecting &&
  172. !isBarScrolledTo
  173. ) {
  174. scrollIntoView();
  175. }
  176. if (isIntersecting) {
  177. props.onBarScrolledTo();
  178. }
  179. return () => {
  180. observer.disconnect();
  181. };
  182. }, [
  183. setIntersecting,
  184. hashValues?.eventId,
  185. hashValues?.spanId,
  186. props,
  187. scrollIntoView,
  188. isIntersecting,
  189. transactionRowDOMRef,
  190. ]);
  191. useEffect(() => {
  192. const transactionTitleRefCurrentCopy = transactionTitleRef.current;
  193. if (transactionTitleRefCurrentCopy) {
  194. transactionTitleRefCurrentCopy.addEventListener('wheel', handleWheel, {
  195. passive: false,
  196. });
  197. }
  198. return () => {
  199. if (transactionTitleRefCurrentCopy) {
  200. transactionTitleRefCurrentCopy.removeEventListener('wheel', handleWheel);
  201. }
  202. };
  203. }, [handleWheel, props, transactionTitleRef]);
  204. const transactionEvent =
  205. isTraceTransaction<TraceFullDetailed>(props.transaction) ||
  206. isTraceError(props.transaction)
  207. ? props.transaction
  208. : undefined;
  209. const {
  210. data: embeddedChildren,
  211. isPending: isEmbeddedChildrenLoading,
  212. error: embeddedChildrenError,
  213. } = useApiQuery<EventTransaction>(
  214. [
  215. `/organizations/${props.organization.slug}/events/${transactionEvent?.project_slug}:${transactionEvent?.event_id}/`,
  216. ],
  217. {
  218. staleTime: 2 * 60 * 1000,
  219. enabled: showEmbeddedChildren || isHighlighted,
  220. }
  221. );
  222. const waterfallModel = useMemo(() => {
  223. return embeddedChildren
  224. ? new WaterfallModel(
  225. embeddedChildren,
  226. undefined,
  227. undefined,
  228. undefined,
  229. props.traceInfo
  230. )
  231. : null;
  232. }, [embeddedChildren, props.traceInfo]);
  233. useEffect(() => {
  234. if (isTraceTransaction(props.transaction) && !isTraceError(props.transaction)) {
  235. if (isHighlighted && props.onRowClick) {
  236. props.onRowClick({
  237. traceFullDetailedEvent: props.transaction,
  238. event: embeddedChildren,
  239. openPanel,
  240. });
  241. }
  242. }
  243. }, [isHighlighted, embeddedChildren, props, props.transaction, openPanel]);
  244. const renderEmbeddedChildrenState = () => {
  245. if (showEmbeddedChildren) {
  246. if (isEmbeddedChildrenLoading) {
  247. return (
  248. <MessageRow>
  249. <span>{t('Loading embedded transaction')}</span>
  250. </MessageRow>
  251. );
  252. }
  253. if (embeddedChildrenError) {
  254. return (
  255. <MessageRow>
  256. <span>{t('Error loading embedded transaction')}</span>
  257. </MessageRow>
  258. );
  259. }
  260. }
  261. return null;
  262. };
  263. const handleRowCellClick = () => {
  264. const {transaction, organization, location} = props;
  265. if (isTraceError(transaction)) {
  266. navigate(generateIssueEventTarget(transaction, organization));
  267. return;
  268. }
  269. if (isTraceTransaction<TraceFullDetailed>(transaction)) {
  270. navigate(
  271. {
  272. ...location,
  273. hash: transactionTargetHash(transaction.event_id),
  274. query: {
  275. ...location.query,
  276. openPanel: 'open',
  277. },
  278. },
  279. {replace: true}
  280. );
  281. }
  282. };
  283. const getCurrentOffset = () => {
  284. const {transaction} = props;
  285. const {generation} = transaction;
  286. return getOffset(generation);
  287. };
  288. const renderMeasurements = () => {
  289. const {measurements, generateBounds} = props;
  290. if (!measurements) {
  291. return null;
  292. }
  293. return (
  294. <Fragment>
  295. {Array.from(measurements.values()).map(verticalMark => {
  296. const mark = Object.values(verticalMark.marks)[0]!;
  297. const {timestamp} = mark;
  298. const bounds = getMeasurementBounds(timestamp, generateBounds);
  299. const shouldDisplay = defined(bounds.left) && defined(bounds.width);
  300. if (!shouldDisplay || !bounds.isSpanVisibleInView) {
  301. return null;
  302. }
  303. return (
  304. <MeasurementMarker
  305. key={String(timestamp)}
  306. style={{
  307. left: `clamp(0%, ${toPercent(bounds.left || 0)}, calc(100% - 1px))`,
  308. }}
  309. failedThreshold={verticalMark.failedThreshold}
  310. />
  311. );
  312. })}
  313. </Fragment>
  314. );
  315. };
  316. const renderConnector = (hasToggle: boolean) => {
  317. const {continuingDepths, isExpanded, isOrphan, isLast, transaction} = props;
  318. const {generation = 0} = transaction;
  319. const eventId =
  320. isTraceTransaction<TraceFullDetailed>(transaction) || isTraceError(transaction)
  321. ? transaction.event_id
  322. : transaction.traceSlug;
  323. if (generation === 0) {
  324. if (hasToggle) {
  325. return (
  326. <ConnectorBar
  327. style={{right: '15px', height: '10px', bottom: '-5px', top: 'auto'}}
  328. orphanBranch={false}
  329. />
  330. );
  331. }
  332. return null;
  333. }
  334. const connectorBars: React.ReactNode[] = continuingDepths.map(
  335. ({depth, isOrphanDepth}) => {
  336. if (generation - depth <= 1) {
  337. // If the difference is less than or equal to 1, then it means that the continued
  338. // bar is from its direct parent. In this case, do not render a connector bar
  339. // because the tree connector below will suffice.
  340. return null;
  341. }
  342. const left = -1 * getOffset(generation - depth - 1) - 2;
  343. return (
  344. <ConnectorBar
  345. style={{left}}
  346. key={`${eventId}-${depth}`}
  347. orphanBranch={isOrphanDepth}
  348. />
  349. );
  350. }
  351. );
  352. const embeddedChildrenLength =
  353. (embeddedChildren && waterfallModel && waterfallModel.rootSpan.children.length) ??
  354. 0;
  355. if (
  356. hasToggle &&
  357. (isExpanded || (showEmbeddedChildren && embeddedChildrenLength > 0))
  358. ) {
  359. connectorBars.push(
  360. <ConnectorBar
  361. style={{
  362. right: '15px',
  363. height: '10px',
  364. bottom: isLast ? `-${ROW_HEIGHT / 2 + 1}px` : '0',
  365. top: 'auto',
  366. }}
  367. key={`${eventId}-last`}
  368. orphanBranch={false}
  369. />
  370. );
  371. }
  372. return (
  373. <TreeConnector isLast={isLast} hasToggler={hasToggle} orphanBranch={isOrphan}>
  374. {connectorBars}
  375. </TreeConnector>
  376. );
  377. };
  378. const renderEmbeddedTransactionsBadge = (): React.ReactNode => {
  379. return (
  380. <Tooltip
  381. title={
  382. <span>
  383. {showEmbeddedChildren
  384. ? t(
  385. 'This transaction is showing a direct child. Remove transaction to hide'
  386. )
  387. : t('This transaction has a direct child. Add transaction to view')}
  388. </span>
  389. }
  390. position="top"
  391. containerDisplayMode="block"
  392. delay={400}
  393. >
  394. <StyledZoomIcon
  395. isZoomIn={!showEmbeddedChildren}
  396. onClick={() => {
  397. setShowEmbeddedChildren(prev => !prev);
  398. if (
  399. (props.isExpanded && !showEmbeddedChildren) ||
  400. (!props.isExpanded && showEmbeddedChildren)
  401. ) {
  402. props.toggleExpandedState();
  403. }
  404. }}
  405. />
  406. </Tooltip>
  407. );
  408. };
  409. const renderEmbeddedChildren = () => {
  410. if (!embeddedChildren || !showEmbeddedChildren || !waterfallModel) {
  411. return null;
  412. }
  413. const {
  414. organization,
  415. traceViewRef,
  416. location,
  417. isLast,
  418. traceInfo,
  419. isExpanded,
  420. toggleExpandedState,
  421. } = props;
  422. const profileId = embeddedChildren.contexts?.profile?.profile_id ?? null;
  423. if (isExpanded) {
  424. toggleExpandedState();
  425. }
  426. return (
  427. <Fragment>
  428. <QuickTraceQuery
  429. event={embeddedChildren}
  430. location={location}
  431. orgSlug={organization.slug}
  432. skipLight={false}
  433. >
  434. {results => (
  435. <ProfilesProvider
  436. orgSlug={organization.slug}
  437. projectSlug={embeddedChildren.projectSlug ?? ''}
  438. profileMeta={profileId || ''}
  439. >
  440. <ProfileContext.Consumer>
  441. {profiles => (
  442. <ProfileGroupProvider
  443. type="flamechart"
  444. input={profiles?.type === 'resolved' ? profiles.data : null}
  445. traceID={profileId || ''}
  446. >
  447. <SpanContext.Provider>
  448. <SpanContext.Consumer>
  449. {spanContextProps => (
  450. <Observer>
  451. {() => (
  452. <NewTraceDetailsSpanTree
  453. measurements={props.measurements}
  454. quickTrace={results}
  455. location={props.location}
  456. onRowClick={props.onRowClick}
  457. traceInfo={traceInfo}
  458. traceViewHeaderRef={traceViewRef}
  459. traceViewRef={traceViewRef}
  460. parentContinuingDepths={props.continuingDepths}
  461. traceHasMultipleRoots={props.continuingDepths.some(
  462. c => c.depth === 0 && c.isOrphanDepth
  463. )}
  464. parentIsOrphan={props.isOrphan}
  465. parentIsLast={isLast}
  466. parentGeneration={transaction.generation ?? 0}
  467. organization={organization}
  468. waterfallModel={waterfallModel}
  469. filterSpans={waterfallModel.filterSpans}
  470. spans={waterfallModel
  471. .getWaterfall({
  472. viewStart: 0,
  473. viewEnd: 1,
  474. })
  475. .slice(1)}
  476. focusedSpanIds={waterfallModel.focusedSpanIds}
  477. spanContextProps={spanContextProps}
  478. operationNameFilters={waterfallModel.operationNameFilters}
  479. />
  480. )}
  481. </Observer>
  482. )}
  483. </SpanContext.Consumer>
  484. </SpanContext.Provider>
  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: any) {
  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. `;