newTraceDetailsTransactionBar.tsx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. import {createRef, Fragment, useCallback, useEffect, useMemo, 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. MetricsBadge,
  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 {IconZoom} from 'sentry/icons/iconZoom';
  62. import {t} from 'sentry/locale';
  63. import {space} from 'sentry/styles/space';
  64. import type {EventTransaction, Organization} from 'sentry/types';
  65. import {defined} from 'sentry/utils';
  66. import {browserHistory} from 'sentry/utils/browserHistory';
  67. import {hasMetricsExperimentalFeature} from 'sentry/utils/metrics/features';
  68. import toPercent from 'sentry/utils/number/toPercent';
  69. import QuickTraceQuery from 'sentry/utils/performance/quickTrace/quickTraceQuery';
  70. import type {
  71. TraceError,
  72. TraceFullDetailed,
  73. } from 'sentry/utils/performance/quickTrace/types';
  74. import {
  75. isTraceError,
  76. isTraceRoot,
  77. isTraceTransaction,
  78. } from 'sentry/utils/performance/quickTrace/utils';
  79. import Projects from 'sentry/utils/projects';
  80. import {useApiQuery} from 'sentry/utils/queryClient';
  81. import {decodeScalar} from 'sentry/utils/queryString';
  82. import useRouter from 'sentry/utils/useRouter';
  83. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  84. import {ProfileContext, ProfilesProvider} from 'sentry/views/profiling/profilesProvider';
  85. import type {EventDetail} from './newTraceDetailsContent';
  86. import {ProjectBadgeContainer} from './styles';
  87. import type {TraceInfo, TraceRoot, TreeDepth} from './types';
  88. import {shortenErrorTitle} from './utils';
  89. const MARGIN_LEFT = 0;
  90. const TRANSACTION_BAR_HEIGHT = 24;
  91. type Props = {
  92. addContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  93. continuingDepths: TreeDepth[];
  94. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  95. hasGuideAnchor: boolean;
  96. index: number;
  97. isBarScrolledTo: boolean;
  98. isExpanded: boolean;
  99. isLast: boolean;
  100. isOrphan: boolean;
  101. isVisible: boolean;
  102. location: Location;
  103. onBarScrolledTo: () => void;
  104. onWheel: (deltaX: number) => void;
  105. organization: Organization;
  106. removeContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  107. toggleExpandedState: () => void;
  108. traceInfo: TraceInfo;
  109. traceViewRef: React.RefObject<HTMLDivElement>;
  110. transaction: TraceRoot | TraceFullDetailed | TraceError;
  111. barColor?: string;
  112. isOrphanError?: boolean;
  113. measurements?: Map<number, VerticalMark>;
  114. numOfOrphanErrors?: number;
  115. onRowClick?: (detailKey: EventDetail | SpanDetailProps | undefined) => void;
  116. onlyOrphanErrors?: boolean;
  117. };
  118. function NewTraceDetailsTransactionBar(props: Props) {
  119. const hashValues = parseTraceDetailsURLHash(props.location.hash);
  120. const openPanel = decodeScalar(props.location.query.openPanel);
  121. const eventIDInQueryParam = !!(
  122. isTraceTransaction(props.transaction) &&
  123. hashValues?.eventId &&
  124. hashValues.eventId === props.transaction.event_id
  125. );
  126. const isHighlighted = !!(!hashValues?.spanId && eventIDInQueryParam);
  127. const highlightEmbeddedSpan = !!(hashValues?.spanId && eventIDInQueryParam);
  128. const [showEmbeddedChildren, setShowEmbeddedChildren] = useState(
  129. isHighlighted || highlightEmbeddedSpan
  130. );
  131. const [isIntersecting, setIntersecting] = useState(false);
  132. const transactionRowDOMRef = createRef<HTMLDivElement>();
  133. const transactionTitleRef = createRef<HTMLDivElement>();
  134. let spanContentRef: HTMLDivElement | null = null;
  135. const router = useRouter();
  136. const handleWheel = useCallback(
  137. (event: WheelEvent) => {
  138. // https://stackoverflow.com/q/57358640
  139. // https://github.com/facebook/react/issues/14856
  140. if (Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
  141. return;
  142. }
  143. event.preventDefault();
  144. event.stopPropagation();
  145. if (Math.abs(event.deltaY) === Math.abs(event.deltaX)) {
  146. return;
  147. }
  148. const {onWheel} = props;
  149. onWheel(event.deltaX);
  150. },
  151. [props]
  152. );
  153. const scrollIntoView = useCallback(() => {
  154. const element = transactionRowDOMRef.current;
  155. if (!element) {
  156. return;
  157. }
  158. const boundingRect = element.getBoundingClientRect();
  159. const offset = boundingRect.top + window.scrollY - TRANSACTION_BAR_HEIGHT;
  160. window.scrollTo(0, offset);
  161. props.onBarScrolledTo();
  162. }, [transactionRowDOMRef, props]);
  163. useEffect(() => {
  164. const {transaction, isBarScrolledTo} = props;
  165. const observer = new IntersectionObserver(([entry]) =>
  166. setIntersecting(entry.isIntersecting)
  167. );
  168. if (transactionRowDOMRef.current) {
  169. observer.observe(transactionRowDOMRef.current);
  170. }
  171. if (
  172. 'event_id' in transaction &&
  173. hashValues?.eventId === transaction.event_id &&
  174. !isIntersecting &&
  175. !isBarScrolledTo
  176. ) {
  177. scrollIntoView();
  178. }
  179. if (isIntersecting) {
  180. props.onBarScrolledTo();
  181. }
  182. return () => {
  183. observer.disconnect();
  184. };
  185. }, [
  186. setIntersecting,
  187. hashValues?.eventId,
  188. hashValues?.spanId,
  189. props,
  190. scrollIntoView,
  191. isIntersecting,
  192. transactionRowDOMRef,
  193. ]);
  194. useEffect(() => {
  195. const transactionTitleRefCurrentCopy = transactionTitleRef.current;
  196. if (transactionTitleRefCurrentCopy) {
  197. transactionTitleRefCurrentCopy.addEventListener('wheel', handleWheel, {
  198. passive: false,
  199. });
  200. }
  201. return () => {
  202. if (transactionTitleRefCurrentCopy) {
  203. transactionTitleRefCurrentCopy.removeEventListener('wheel', handleWheel);
  204. }
  205. };
  206. }, [handleWheel, props, transactionTitleRef]);
  207. const transactionEvent =
  208. isTraceTransaction<TraceFullDetailed>(props.transaction) ||
  209. isTraceError(props.transaction)
  210. ? props.transaction
  211. : undefined;
  212. const {
  213. data: embeddedChildren,
  214. isLoading: isEmbeddedChildrenLoading,
  215. error: embeddedChildrenError,
  216. } = useApiQuery<EventTransaction>(
  217. [
  218. `/organizations/${props.organization.slug}/events/${transactionEvent?.project_slug}:${transactionEvent?.event_id}/`,
  219. ],
  220. {
  221. staleTime: 2 * 60 * 1000,
  222. enabled: showEmbeddedChildren || isHighlighted,
  223. }
  224. );
  225. const waterfallModel = useMemo(() => {
  226. return embeddedChildren
  227. ? new WaterfallModel(
  228. embeddedChildren,
  229. undefined,
  230. undefined,
  231. undefined,
  232. props.traceInfo
  233. )
  234. : null;
  235. }, [embeddedChildren, props.traceInfo]);
  236. useEffect(() => {
  237. if (isTraceTransaction(props.transaction) && !isTraceError(props.transaction)) {
  238. if (isHighlighted && props.onRowClick) {
  239. props.onRowClick({
  240. traceFullDetailedEvent: props.transaction,
  241. event: embeddedChildren,
  242. openPanel,
  243. });
  244. }
  245. }
  246. }, [isHighlighted, embeddedChildren, props, props.transaction, openPanel]);
  247. const renderEmbeddedChildrenState = () => {
  248. if (showEmbeddedChildren) {
  249. if (isEmbeddedChildrenLoading) {
  250. return (
  251. <MessageRow>
  252. <span>{t('Loading embedded transaction')}</span>
  253. </MessageRow>
  254. );
  255. }
  256. if (embeddedChildrenError) {
  257. return (
  258. <MessageRow>
  259. <span>{t('Error loading embedded transaction')}</span>
  260. </MessageRow>
  261. );
  262. }
  263. }
  264. return null;
  265. };
  266. const handleRowCellClick = () => {
  267. const {transaction, organization, location} = props;
  268. if (isTraceError(transaction)) {
  269. browserHistory.push(generateIssueEventTarget(transaction, organization));
  270. return;
  271. }
  272. if (isTraceTransaction<TraceFullDetailed>(transaction)) {
  273. router.replace({
  274. ...location,
  275. hash: transactionTargetHash(transaction.event_id),
  276. query: {
  277. ...location.query,
  278. openPanel: 'open',
  279. },
  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: Array<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. >
  433. {results => (
  434. <ProfilesProvider
  435. orgSlug={organization.slug}
  436. projectSlug={embeddedChildren.projectSlug ?? ''}
  437. profileId={profileId || ''}
  438. >
  439. <ProfileContext.Consumer>
  440. {profiles => (
  441. <ProfileGroupProvider
  442. type="flamechart"
  443. input={profiles?.type === 'resolved' ? profiles.data : null}
  444. traceID={profileId || ''}
  445. >
  446. <TransactionProfileIdProvider
  447. projectId={embeddedChildren.projectID}
  448. timestamp={embeddedChildren.dateReceived}
  449. transactionId={embeddedChildren.id}
  450. >
  451. <SpanContext.Provider>
  452. <SpanContext.Consumer>
  453. {spanContextProps => (
  454. <Observer>
  455. {() => (
  456. <NewTraceDetailsSpanTree
  457. measurements={props.measurements}
  458. quickTrace={results}
  459. location={props.location}
  460. onRowClick={props.onRowClick}
  461. traceInfo={traceInfo}
  462. traceViewHeaderRef={traceViewRef}
  463. traceViewRef={traceViewRef}
  464. parentContinuingDepths={props.continuingDepths}
  465. traceHasMultipleRoots={props.continuingDepths.some(
  466. c => c.depth === 0 && c.isOrphanDepth
  467. )}
  468. parentIsOrphan={props.isOrphan}
  469. parentIsLast={isLast}
  470. parentGeneration={transaction.generation ?? 0}
  471. organization={organization}
  472. waterfallModel={waterfallModel}
  473. filterSpans={waterfallModel.filterSpans}
  474. spans={waterfallModel
  475. .getWaterfall({
  476. viewStart: 0,
  477. viewEnd: 1,
  478. })
  479. .slice(1)}
  480. focusedSpanIds={waterfallModel.focusedSpanIds}
  481. spanContextProps={spanContextProps}
  482. operationNameFilters={
  483. waterfallModel.operationNameFilters
  484. }
  485. />
  486. )}
  487. </Observer>
  488. )}
  489. </SpanContext.Consumer>
  490. </SpanContext.Provider>
  491. </TransactionProfileIdProvider>
  492. </ProfileGroupProvider>
  493. )}
  494. </ProfileContext.Consumer>
  495. </ProfilesProvider>
  496. )}
  497. </QuickTraceQuery>
  498. </Fragment>
  499. );
  500. };
  501. const renderToggle = (errored: boolean) => {
  502. const {isExpanded, transaction, toggleExpandedState, numOfOrphanErrors} = props;
  503. const left = getCurrentOffset();
  504. const hasOrphanErrors = numOfOrphanErrors && numOfOrphanErrors > 0;
  505. let childrenLength: number | string =
  506. (!isTraceError(transaction) && transaction.children?.length) || 0;
  507. const generation = transaction.generation || 0;
  508. if (childrenLength <= 0 && !hasOrphanErrors && !showEmbeddedChildren) {
  509. return (
  510. <TreeToggleContainer style={{left: `${left}px`}}>
  511. {renderConnector(false)}
  512. </TreeToggleContainer>
  513. );
  514. }
  515. if (showEmbeddedChildren) {
  516. childrenLength =
  517. embeddedChildren && waterfallModel
  518. ? waterfallModel.rootSpan.children.length
  519. : '?';
  520. } else {
  521. childrenLength = childrenLength + (numOfOrphanErrors ?? 0);
  522. }
  523. const isRoot = generation === 0;
  524. return (
  525. <TreeToggleContainer style={{left: `${left}px`}} hasToggler>
  526. {renderConnector(true)}
  527. <TreeToggle
  528. disabled={isRoot}
  529. isExpanded={isExpanded}
  530. errored={errored}
  531. onClick={event => {
  532. event.stopPropagation();
  533. if (isRoot || showEmbeddedChildren) {
  534. return;
  535. }
  536. toggleExpandedState();
  537. setShowEmbeddedChildren(false);
  538. }}
  539. >
  540. <span>{childrenLength}</span>
  541. {!isRoot && !showEmbeddedChildren && (
  542. <div>
  543. <TreeToggleIcon direction={isExpanded ? 'up' : 'down'} />
  544. </div>
  545. )}
  546. </TreeToggle>
  547. </TreeToggleContainer>
  548. );
  549. };
  550. const renderTitle = (_: ScrollbarManager.ScrollbarManagerChildrenProps) => {
  551. const {organization, transaction, addContentSpanBarRef, removeContentSpanBarRef} =
  552. props;
  553. const left = getCurrentOffset();
  554. const errored = isTraceTransaction<TraceFullDetailed>(transaction)
  555. ? transaction.errors &&
  556. transaction.errors.length + transaction.performance_issues.length > 0
  557. : false;
  558. const projectBadge = (isTraceTransaction<TraceFullDetailed>(transaction) ||
  559. isTraceError(transaction)) && (
  560. <Projects orgId={organization.slug} slugs={[transaction.project_slug]}>
  561. {({projects}) => {
  562. const project = projects.find(p => p.slug === transaction.project_slug);
  563. return (
  564. <ProjectBadgeContainer>
  565. <Tooltip title={transaction.project_slug}>
  566. <ProjectBadge
  567. project={project ? project : {slug: transaction.project_slug}}
  568. avatarSize={16}
  569. hideName
  570. />
  571. </Tooltip>
  572. </ProjectBadgeContainer>
  573. );
  574. }}
  575. </Projects>
  576. );
  577. const content = isTraceError(transaction) ? (
  578. <Fragment>
  579. {projectBadge}
  580. <RowTitleContent errored>
  581. <ErrorLink to={generateIssueEventTarget(transaction, organization)}>
  582. <strong>{'Unknown \u2014 '}</strong>
  583. {shortenErrorTitle(transaction.title)}
  584. </ErrorLink>
  585. </RowTitleContent>
  586. </Fragment>
  587. ) : isTraceTransaction<TraceFullDetailed>(transaction) ? (
  588. <Fragment>
  589. {projectBadge}
  590. <RowTitleContent errored={errored}>
  591. <strong>
  592. {transaction['transaction.op']}
  593. {' \u2014 '}
  594. </strong>
  595. {transaction.transaction}
  596. </RowTitleContent>
  597. </Fragment>
  598. ) : (
  599. <RowTitleContent errored={false}>
  600. <strong>{'Trace \u2014 '}</strong>
  601. {transaction.traceSlug}
  602. </RowTitleContent>
  603. );
  604. return (
  605. <RowTitleContainer
  606. ref={ref => {
  607. if (!ref) {
  608. removeContentSpanBarRef(spanContentRef);
  609. return;
  610. }
  611. addContentSpanBarRef(ref);
  612. spanContentRef = ref;
  613. }}
  614. >
  615. {renderToggle(errored)}
  616. <RowTitle
  617. style={{
  618. left: `${left}px`,
  619. width: '100%',
  620. }}
  621. >
  622. {content}
  623. </RowTitle>
  624. </RowTitleContainer>
  625. );
  626. };
  627. const renderDivider = (
  628. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  629. ) => {
  630. if (isHighlighted) {
  631. // Mock component to preserve layout spacing
  632. return (
  633. <DividerLine
  634. showDetail={isHighlighted}
  635. style={{
  636. position: 'absolute',
  637. }}
  638. />
  639. );
  640. }
  641. const {addDividerLineRef} = dividerHandlerChildrenProps;
  642. return (
  643. <DividerLine
  644. ref={addDividerLineRef()}
  645. style={{
  646. position: 'absolute',
  647. }}
  648. onMouseEnter={() => {
  649. dividerHandlerChildrenProps.setHover(true);
  650. }}
  651. onMouseLeave={() => {
  652. dividerHandlerChildrenProps.setHover(false);
  653. }}
  654. onMouseOver={() => {
  655. dividerHandlerChildrenProps.setHover(true);
  656. }}
  657. onMouseDown={e => {
  658. dividerHandlerChildrenProps.onDragStart(e);
  659. }}
  660. onClick={event => {
  661. // we prevent the propagation of the clicks from this component to prevent
  662. // the span detail from being opened.
  663. event.stopPropagation();
  664. }}
  665. />
  666. );
  667. };
  668. const renderGhostDivider = (
  669. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  670. ) => {
  671. const {dividerPosition, addGhostDividerLineRef} = dividerHandlerChildrenProps;
  672. return (
  673. <DividerLineGhostContainer
  674. style={{
  675. width: `calc(${toPercent(dividerPosition)} + 0.5px)`,
  676. display: 'none',
  677. }}
  678. >
  679. <DividerLine
  680. ref={addGhostDividerLineRef()}
  681. style={{
  682. right: 0,
  683. }}
  684. className="hovering"
  685. onClick={event => {
  686. // the ghost divider line should not be interactive.
  687. // we prevent the propagation of the clicks from this component to prevent
  688. // the span detail from being opened.
  689. event.stopPropagation();
  690. }}
  691. />
  692. </DividerLineGhostContainer>
  693. );
  694. };
  695. const renderErrorBadge = () => {
  696. const {transaction} = props;
  697. if (
  698. isTraceRoot(transaction) ||
  699. isTraceError(transaction) ||
  700. !(transaction.errors.length + transaction.performance_issues.length)
  701. ) {
  702. return null;
  703. }
  704. return <ErrorBadge />;
  705. };
  706. const renderMetricsBadge = () => {
  707. const {organization} = props;
  708. const hasMetrics = Object.keys(embeddedChildren?._metrics_summary ?? {}).length > 0;
  709. if (
  710. !hasMetricsExperimentalFeature(organization) ||
  711. isTraceRoot(transaction) ||
  712. isTraceError(transaction) ||
  713. !hasMetrics
  714. ) {
  715. return null;
  716. }
  717. return <MetricsBadge />;
  718. };
  719. const renderRectangle = () => {
  720. const {transaction, traceInfo, barColor} = props;
  721. // Use 1 as the difference in the case that startTimestamp === endTimestamp
  722. const delta = Math.abs(traceInfo.endTimestamp - traceInfo.startTimestamp) || 1;
  723. const start_timestamp = isTraceError(transaction)
  724. ? transaction.timestamp
  725. : transaction.start_timestamp;
  726. if (!(start_timestamp && transaction.timestamp)) {
  727. return null;
  728. }
  729. const startPosition = Math.abs(start_timestamp - traceInfo.startTimestamp);
  730. const startPercentage = startPosition / delta;
  731. const duration = Math.abs(transaction.timestamp - start_timestamp);
  732. const widthPercentage = duration / delta;
  733. return (
  734. <StyledRowRectangle
  735. style={{
  736. backgroundColor: barColor,
  737. left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
  738. width: toPercent(widthPercentage || 0),
  739. }}
  740. >
  741. {renderPerformanceIssues()}
  742. {isTraceError(transaction) ? (
  743. <ErrorBadge />
  744. ) : (
  745. <Fragment>
  746. {renderMetricsBadge()}
  747. {renderErrorBadge()}
  748. <DurationPill
  749. durationDisplay={getDurationDisplay({
  750. left: startPercentage,
  751. width: widthPercentage,
  752. })}
  753. showDetail={isHighlighted}
  754. >
  755. {getHumanDuration(duration)}
  756. </DurationPill>
  757. </Fragment>
  758. )}
  759. </StyledRowRectangle>
  760. );
  761. };
  762. const renderPerformanceIssues = () => {
  763. const {transaction, barColor} = props;
  764. if (isTraceError(transaction) || isTraceRoot(transaction)) {
  765. return null;
  766. }
  767. const rows: React.ReactElement[] = [];
  768. // Use 1 as the difference in the case that startTimestamp === endTimestamp
  769. const delta = Math.abs(transaction.timestamp - transaction.start_timestamp) || 1;
  770. for (let i = 0; i < transaction.performance_issues.length; i++) {
  771. const issue = transaction.performance_issues[i];
  772. const startPosition = Math.abs(issue.start - transaction.start_timestamp);
  773. const startPercentage = startPosition / delta;
  774. const duration = Math.abs(issue.end - issue.start);
  775. const widthPercentage = duration / delta;
  776. rows.push(
  777. <RowRectangle
  778. style={{
  779. backgroundColor: barColor,
  780. left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
  781. width: toPercent(widthPercentage || 0),
  782. }}
  783. spanBarType={SpanBarType.AFFECTED}
  784. />
  785. );
  786. }
  787. return rows;
  788. };
  789. const renderHeader = ({
  790. dividerHandlerChildrenProps,
  791. scrollbarManagerChildrenProps,
  792. }: {
  793. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps;
  794. scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps;
  795. }) => {
  796. const {hasGuideAnchor, index, transaction, onlyOrphanErrors = false} = props;
  797. const {dividerPosition} = dividerHandlerChildrenProps;
  798. const hideDurationRectangle = isTraceRoot(transaction) && onlyOrphanErrors;
  799. return (
  800. <RowCellContainer showDetail={isHighlighted}>
  801. <RowCell
  802. data-test-id="transaction-row-title"
  803. data-type="span-row-cell"
  804. style={{
  805. width: `calc(${toPercent(dividerPosition)} - 0.5px)`,
  806. paddingTop: 0,
  807. }}
  808. showDetail={isHighlighted}
  809. onClick={handleRowCellClick}
  810. ref={transactionTitleRef}
  811. >
  812. <GuideAnchor target="trace_view_guide_row" disabled={!hasGuideAnchor}>
  813. {renderTitle(scrollbarManagerChildrenProps)}
  814. </GuideAnchor>
  815. </RowCell>
  816. <DividerContainer>
  817. {renderDivider(dividerHandlerChildrenProps)}
  818. {!isTraceRoot(transaction) &&
  819. !isTraceError(transaction) &&
  820. renderEmbeddedTransactionsBadge()}
  821. </DividerContainer>
  822. <RowCell
  823. data-test-id="transaction-row-duration"
  824. data-type="span-row-cell"
  825. showStriping={index % 2 !== 0}
  826. style={{
  827. width: `calc(${toPercent(1 - dividerPosition)} - 0.5px)`,
  828. paddingTop: 0,
  829. overflow: 'visible',
  830. }}
  831. showDetail={isHighlighted}
  832. onClick={handleRowCellClick}
  833. >
  834. <RowReplayTimeIndicators />
  835. <GuideAnchor target="trace_view_guide_row_details" disabled={!hasGuideAnchor}>
  836. {!hideDurationRectangle && renderRectangle()}
  837. {renderMeasurements()}
  838. </GuideAnchor>
  839. </RowCell>
  840. {!isHighlighted && renderGhostDivider(dividerHandlerChildrenProps)}
  841. </RowCellContainer>
  842. );
  843. };
  844. const {isVisible, transaction} = props;
  845. return (
  846. <div>
  847. <StyledRow
  848. ref={transactionRowDOMRef}
  849. visible={isVisible}
  850. showBorder={isHighlighted}
  851. cursor={
  852. isTraceTransaction<TraceFullDetailed>(transaction) ? 'pointer' : 'default'
  853. }
  854. >
  855. <ScrollbarManager.Consumer>
  856. {scrollbarManagerChildrenProps => (
  857. <DividerHandlerManager.Consumer>
  858. {dividerHandlerChildrenProps =>
  859. renderHeader({
  860. dividerHandlerChildrenProps,
  861. scrollbarManagerChildrenProps,
  862. })
  863. }
  864. </DividerHandlerManager.Consumer>
  865. )}
  866. </ScrollbarManager.Consumer>
  867. </StyledRow>
  868. {renderEmbeddedChildrenState()}
  869. {renderEmbeddedChildren()}
  870. </div>
  871. );
  872. }
  873. function getOffset(generation) {
  874. return generation * (TOGGLE_BORDER_BOX / 2) + MARGIN_LEFT;
  875. }
  876. export default NewTraceDetailsTransactionBar;
  877. const StyledRow = styled(Row)`
  878. &,
  879. ${RowCellContainer} {
  880. overflow: visible;
  881. }
  882. `;
  883. const ErrorLink = styled(Link)`
  884. color: ${p => p.theme.error};
  885. `;
  886. const StyledRowRectangle = styled(RowRectangle)`
  887. display: flex;
  888. align-items: center;
  889. `;
  890. export const StyledZoomIcon = styled(IconZoom)`
  891. position: absolute;
  892. left: -20px;
  893. top: 4px;
  894. height: 16px;
  895. width: 18px;
  896. z-index: 1000;
  897. background: ${p => p.theme.background};
  898. padding: 1px;
  899. border: 1px solid ${p => p.theme.border};
  900. border-radius: ${space(0.5)};
  901. `;