spanBar.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. import 'intersection-observer'; // this is a polyfill
  2. import * as React from 'react';
  3. import styled from '@emotion/styled';
  4. import Count from 'app/components/count';
  5. import FeatureBadge from 'app/components/featureBadge';
  6. import {ROW_HEIGHT} from 'app/components/performance/waterfall/constants';
  7. import {MessageRow} from 'app/components/performance/waterfall/messageRow';
  8. import {Row, RowCell, RowCellContainer} from 'app/components/performance/waterfall/row';
  9. import {DurationPill, RowRectangle} from 'app/components/performance/waterfall/rowBar';
  10. import {
  11. DividerContainer,
  12. DividerLine,
  13. DividerLineGhostContainer,
  14. EmbeddedTransactionBadge,
  15. ErrorBadge,
  16. } from 'app/components/performance/waterfall/rowDivider';
  17. import {
  18. RowTitle,
  19. RowTitleContainer,
  20. RowTitleContent,
  21. } from 'app/components/performance/waterfall/rowTitle';
  22. import {
  23. ConnectorBar,
  24. StyledIconChevron,
  25. TOGGLE_BORDER_BOX,
  26. TreeConnector,
  27. TreeToggle,
  28. TreeToggleContainer,
  29. } from 'app/components/performance/waterfall/treeConnector';
  30. import {
  31. getDurationDisplay,
  32. getHumanDuration,
  33. toPercent,
  34. } from 'app/components/performance/waterfall/utils';
  35. import Tooltip from 'app/components/tooltip';
  36. import {IconWarning} from 'app/icons';
  37. import {t} from 'app/locale';
  38. import space from 'app/styles/space';
  39. import {Organization} from 'app/types';
  40. import {EventTransaction} from 'app/types/event';
  41. import {defined} from 'app/utils';
  42. import {trackAnalyticsEvent} from 'app/utils/analytics';
  43. import {generateEventSlug} from 'app/utils/discover/urls';
  44. import * as QuickTraceContext from 'app/utils/performance/quickTrace/quickTraceContext';
  45. import {QuickTraceContextChildrenProps} from 'app/utils/performance/quickTrace/quickTraceContext';
  46. import {QuickTraceEvent, TraceError} from 'app/utils/performance/quickTrace/types';
  47. import {isTraceFull} from 'app/utils/performance/quickTrace/utils';
  48. import * as AnchorLinkManager from './anchorLinkManager';
  49. import {
  50. MINIMAP_CONTAINER_HEIGHT,
  51. MINIMAP_SPAN_BAR_HEIGHT,
  52. NUM_OF_SPANS_FIT_IN_MINI_MAP,
  53. } from './constants';
  54. import * as DividerHandlerManager from './dividerHandlerManager';
  55. import * as ScrollbarManager from './scrollbarManager';
  56. import SpanBarCursorGuide from './spanBarCursorGuide';
  57. import SpanDetail from './spanDetail';
  58. import {MeasurementMarker} from './styles';
  59. import {
  60. FetchEmbeddedChildrenState,
  61. ParsedTraceType,
  62. ProcessedSpanType,
  63. TreeDepthType,
  64. } from './types';
  65. import {
  66. durationlessBrowserOps,
  67. getMeasurementBounds,
  68. getMeasurements,
  69. getSpanID,
  70. getSpanOperation,
  71. isEventFromBrowserJavaScriptSDK,
  72. isGapSpan,
  73. isOrphanSpan,
  74. isOrphanTreeDepth,
  75. SpanBoundsType,
  76. SpanGeneratedBoundsType,
  77. SpanViewBoundsType,
  78. unwrapTreeDepth,
  79. } from './utils';
  80. // TODO: maybe use babel-plugin-preval
  81. // for (let i = 0; i <= 1.0; i += 0.01) {
  82. // INTERSECTION_THRESHOLDS.push(i);
  83. // }
  84. const INTERSECTION_THRESHOLDS: Array<number> = [
  85. 0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14,
  86. 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29,
  87. 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4, 0.41, 0.42, 0.43, 0.44,
  88. 0.45, 0.46, 0.47, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59,
  89. 0.6, 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72, 0.73, 0.74,
  90. 0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89,
  91. 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0,
  92. ];
  93. const MARGIN_LEFT = 0;
  94. type SpanBarProps = {
  95. event: Readonly<EventTransaction>;
  96. organization: Organization;
  97. trace: Readonly<ParsedTraceType>;
  98. span: Readonly<ProcessedSpanType>;
  99. spanBarColor?: string;
  100. spanBarHatch?: boolean;
  101. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  102. treeDepth: number;
  103. continuingTreeDepths: Array<TreeDepthType>;
  104. showSpanTree: boolean;
  105. numOfSpanChildren: number;
  106. spanNumber: number;
  107. isLast?: boolean;
  108. isRoot?: boolean;
  109. toggleSpanTree: () => void;
  110. showEmbeddedChildren: boolean;
  111. toggleEmbeddedChildren:
  112. | ((props: {orgSlug: string; eventSlug: string}) => void)
  113. | undefined;
  114. fetchEmbeddedChildrenState: FetchEmbeddedChildrenState;
  115. toggleSpanGroup: (() => void) | undefined;
  116. numOfSpans: number;
  117. };
  118. type SpanBarState = {
  119. showDetail: boolean;
  120. };
  121. class SpanBar extends React.Component<SpanBarProps, SpanBarState> {
  122. state: SpanBarState = {
  123. showDetail: false,
  124. };
  125. componentDidMount() {
  126. this._mounted = true;
  127. if (this.spanRowDOMRef.current) {
  128. this.connectObservers();
  129. }
  130. }
  131. componentWillUnmount() {
  132. this._mounted = false;
  133. this.disconnectObservers();
  134. }
  135. spanRowDOMRef = React.createRef<HTMLDivElement>();
  136. intersectionObserver?: IntersectionObserver = void 0;
  137. zoomLevel: number = 1; // assume initial zoomLevel is 100%
  138. _mounted: boolean = false;
  139. toggleDisplayDetail = () => {
  140. this.setState(state => ({
  141. showDetail: !state.showDetail,
  142. }));
  143. };
  144. scrollIntoView = () => {
  145. const element = this.spanRowDOMRef.current;
  146. if (!element) {
  147. return;
  148. }
  149. const boundingRect = element.getBoundingClientRect();
  150. const offset = boundingRect.top + window.scrollY - MINIMAP_CONTAINER_HEIGHT;
  151. this.setState({showDetail: true}, () => window.scrollTo(0, offset));
  152. };
  153. renderDetail({
  154. isVisible,
  155. transactions,
  156. errors,
  157. }: {
  158. isVisible: boolean;
  159. transactions: QuickTraceEvent[] | null;
  160. errors: TraceError[] | null;
  161. }) {
  162. const {span, organization, isRoot, trace, event} = this.props;
  163. return (
  164. <AnchorLinkManager.Consumer>
  165. {({registerScrollFn, scrollToHash}) => {
  166. if (!isGapSpan(span)) {
  167. registerScrollFn(`#span-${span.span_id}`, this.scrollIntoView);
  168. }
  169. if (!this.state.showDetail || !isVisible) {
  170. return null;
  171. }
  172. return (
  173. <SpanDetail
  174. span={span}
  175. organization={organization}
  176. event={event}
  177. isRoot={!!isRoot}
  178. trace={trace}
  179. childTransactions={transactions}
  180. relatedErrors={errors}
  181. scrollToHash={scrollToHash}
  182. />
  183. );
  184. }}
  185. </AnchorLinkManager.Consumer>
  186. );
  187. }
  188. getBounds(): SpanViewBoundsType {
  189. const {event, span, generateBounds} = this.props;
  190. const bounds = generateBounds({
  191. startTimestamp: span.start_timestamp,
  192. endTimestamp: span.timestamp,
  193. });
  194. const shouldHideSpanWarnings = isEventFromBrowserJavaScriptSDK(event);
  195. switch (bounds.type) {
  196. case 'TRACE_TIMESTAMPS_EQUAL': {
  197. return {
  198. warning: t('Trace times are equal'),
  199. left: void 0,
  200. width: void 0,
  201. isSpanVisibleInView: bounds.isSpanVisibleInView,
  202. };
  203. }
  204. case 'INVALID_VIEW_WINDOW': {
  205. return {
  206. warning: t('Invalid view window'),
  207. left: void 0,
  208. width: void 0,
  209. isSpanVisibleInView: bounds.isSpanVisibleInView,
  210. };
  211. }
  212. case 'TIMESTAMPS_EQUAL': {
  213. const warning =
  214. shouldHideSpanWarnings &&
  215. 'op' in span &&
  216. span.op &&
  217. durationlessBrowserOps.includes(span.op)
  218. ? void 0
  219. : t('Equal start and end times');
  220. return {
  221. warning,
  222. left: bounds.start,
  223. width: 0.00001,
  224. isSpanVisibleInView: bounds.isSpanVisibleInView,
  225. };
  226. }
  227. case 'TIMESTAMPS_REVERSED': {
  228. return {
  229. warning: t('Reversed start and end times'),
  230. left: bounds.start,
  231. width: bounds.end - bounds.start,
  232. isSpanVisibleInView: bounds.isSpanVisibleInView,
  233. };
  234. }
  235. case 'TIMESTAMPS_STABLE': {
  236. return {
  237. warning: void 0,
  238. left: bounds.start,
  239. width: bounds.end - bounds.start,
  240. isSpanVisibleInView: bounds.isSpanVisibleInView,
  241. };
  242. }
  243. default: {
  244. const _exhaustiveCheck: never = bounds;
  245. return _exhaustiveCheck;
  246. }
  247. }
  248. }
  249. renderMeasurements() {
  250. const {event, generateBounds} = this.props;
  251. if (this.state.showDetail) {
  252. return null;
  253. }
  254. const measurements = getMeasurements(event);
  255. return (
  256. <React.Fragment>
  257. {Array.from(measurements).map(([timestamp, verticalMark]) => {
  258. const bounds = getMeasurementBounds(timestamp, generateBounds);
  259. const shouldDisplay = defined(bounds.left) && defined(bounds.width);
  260. if (!shouldDisplay || !bounds.isSpanVisibleInView) {
  261. return null;
  262. }
  263. return (
  264. <MeasurementMarker
  265. key={String(timestamp)}
  266. style={{
  267. left: `clamp(0%, ${toPercent(bounds.left || 0)}, calc(100% - 1px))`,
  268. }}
  269. failedThreshold={verticalMark.failedThreshold}
  270. />
  271. );
  272. })}
  273. </React.Fragment>
  274. );
  275. }
  276. renderSpanTreeConnector({hasToggler}: {hasToggler: boolean}) {
  277. const {
  278. isLast,
  279. isRoot,
  280. treeDepth: spanTreeDepth,
  281. continuingTreeDepths,
  282. span,
  283. showSpanTree,
  284. } = this.props;
  285. const spanID = getSpanID(span);
  286. if (isRoot) {
  287. if (hasToggler) {
  288. return (
  289. <ConnectorBar
  290. style={{right: '16px', height: '10px', bottom: '-5px', top: 'auto'}}
  291. key={`${spanID}-last`}
  292. orphanBranch={false}
  293. />
  294. );
  295. }
  296. return null;
  297. }
  298. const connectorBars: Array<React.ReactNode> = continuingTreeDepths.map(treeDepth => {
  299. const depth: number = unwrapTreeDepth(treeDepth);
  300. if (depth === 0) {
  301. // do not render a connector bar at depth 0,
  302. // if we did render a connector bar, this bar would be placed at depth -1
  303. // which does not exist.
  304. return null;
  305. }
  306. const left = ((spanTreeDepth - depth) * (TOGGLE_BORDER_BOX / 2) + 1) * -1;
  307. return (
  308. <ConnectorBar
  309. style={{left}}
  310. key={`${spanID}-${depth}`}
  311. orphanBranch={isOrphanTreeDepth(treeDepth)}
  312. />
  313. );
  314. });
  315. if (hasToggler && showSpanTree) {
  316. // if there is a toggle button, we add a connector bar to create an attachment
  317. // between the toggle button and any connector bars below the toggle button
  318. connectorBars.push(
  319. <ConnectorBar
  320. style={{
  321. right: '16px',
  322. height: `${ROW_HEIGHT / 2}px`,
  323. bottom: isLast ? `-${ROW_HEIGHT / 2}px` : '0',
  324. top: 'auto',
  325. }}
  326. key={`${spanID}-last-bottom`}
  327. orphanBranch={false}
  328. />
  329. );
  330. }
  331. return (
  332. <TreeConnector
  333. isLast={isLast}
  334. hasToggler={hasToggler}
  335. orphanBranch={isOrphanSpan(span)}
  336. >
  337. {connectorBars}
  338. </TreeConnector>
  339. );
  340. }
  341. renderSpanTreeToggler({left, errored}: {left: number; errored: boolean}) {
  342. const {numOfSpanChildren, isRoot, showSpanTree} = this.props;
  343. const chevron = <StyledIconChevron direction={showSpanTree ? 'up' : 'down'} />;
  344. if (numOfSpanChildren <= 0) {
  345. return (
  346. <TreeToggleContainer style={{left: `${left}px`}}>
  347. {this.renderSpanTreeConnector({hasToggler: false})}
  348. </TreeToggleContainer>
  349. );
  350. }
  351. const chevronElement = !isRoot ? <div>{chevron}</div> : null;
  352. return (
  353. <TreeToggleContainer style={{left: `${left}px`}} hasToggler>
  354. {this.renderSpanTreeConnector({hasToggler: true})}
  355. <TreeToggle
  356. disabled={!!isRoot}
  357. isExpanded={showSpanTree}
  358. errored={errored}
  359. onClick={event => {
  360. event.stopPropagation();
  361. if (isRoot) {
  362. return;
  363. }
  364. this.props.toggleSpanTree();
  365. }}
  366. >
  367. <Count value={numOfSpanChildren} />
  368. {chevronElement}
  369. </TreeToggle>
  370. </TreeToggleContainer>
  371. );
  372. }
  373. renderTitle(
  374. scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps,
  375. errors: TraceError[] | null
  376. ) {
  377. const {generateContentSpanBarRef} = scrollbarManagerChildrenProps;
  378. const {span, treeDepth, toggleSpanGroup} = this.props;
  379. let titleFragments: React.ReactNode[] = [];
  380. if (typeof toggleSpanGroup === 'function') {
  381. titleFragments.push(
  382. <Regroup
  383. onClick={event => {
  384. event.stopPropagation();
  385. event.preventDefault();
  386. toggleSpanGroup();
  387. }}
  388. >
  389. <a
  390. href="#regroup"
  391. onClick={event => {
  392. event.preventDefault();
  393. }}
  394. >
  395. {t('Regroup')}
  396. </a>
  397. </Regroup>
  398. );
  399. }
  400. const spanOperationName = getSpanOperation(span);
  401. if (spanOperationName) {
  402. titleFragments.push(spanOperationName);
  403. }
  404. titleFragments = titleFragments.flatMap(current => [current, ' \u2014 ']);
  405. const description = span?.description ?? getSpanID(span);
  406. const left = treeDepth * (TOGGLE_BORDER_BOX / 2) + MARGIN_LEFT;
  407. const errored = Boolean(errors && errors.length > 0);
  408. return (
  409. <RowTitleContainer
  410. data-debug-id="SpanBarTitleContainer"
  411. ref={generateContentSpanBarRef()}
  412. >
  413. {this.renderSpanTreeToggler({left, errored})}
  414. <RowTitle
  415. style={{
  416. left: `${left}px`,
  417. width: '100%',
  418. }}
  419. >
  420. <RowTitleContent errored={errored}>
  421. <strong>{titleFragments}</strong>
  422. {description}
  423. </RowTitleContent>
  424. </RowTitle>
  425. </RowTitleContainer>
  426. );
  427. }
  428. connectObservers() {
  429. if (!this.spanRowDOMRef.current) {
  430. return;
  431. }
  432. this.disconnectObservers();
  433. /**
  434. We track intersections events between the span bar's DOM element
  435. and the viewport's (root) intersection area. the intersection area is sized to
  436. exclude the minimap. See below.
  437. By default, the intersection observer's root intersection is the viewport.
  438. We adjust the margins of this root intersection area to exclude the minimap's
  439. height. The minimap's height is always fixed.
  440. VIEWPORT (ancestor element used for the intersection events)
  441. +--+-------------------------+--+
  442. | | | |
  443. | | MINIMAP | |
  444. | | | |
  445. | +-------------------------+ | ^
  446. | | | | |
  447. | | SPANS | | | ROOT
  448. | | | | | INTERSECTION
  449. | | | | | OBSERVER
  450. | | | | | HEIGHT
  451. | | | | |
  452. | | | | |
  453. | | | | |
  454. | +-------------------------+ | |
  455. | | |
  456. +-------------------------------+ v
  457. */
  458. this.intersectionObserver = new IntersectionObserver(
  459. entries => {
  460. entries.forEach(entry => {
  461. if (!this._mounted) {
  462. return;
  463. }
  464. const shouldMoveMinimap = this.props.numOfSpans > NUM_OF_SPANS_FIT_IN_MINI_MAP;
  465. if (!shouldMoveMinimap) {
  466. return;
  467. }
  468. const spanNumber = this.props.spanNumber;
  469. const minimapSlider = document.getElementById('minimap-background-slider');
  470. if (!minimapSlider) {
  471. return;
  472. }
  473. // NOTE: THIS IS HACKY.
  474. //
  475. // IntersectionObserver.rootMargin is un-affected by the browser's zoom level.
  476. // The margins of the intersection area needs to be adjusted.
  477. // Thus, IntersectionObserverEntry.rootBounds may not be what we expect.
  478. //
  479. // We address this below.
  480. //
  481. // Note that this function was called whenever an intersection event occurred wrt
  482. // the thresholds.
  483. //
  484. if (entry.rootBounds) {
  485. // After we create the IntersectionObserver instance with rootMargin set as:
  486. // -${MINIMAP_CONTAINER_HEIGHT * this.zoomLevel}px 0px 0px 0px
  487. //
  488. // we can introspect the rootBounds to infer the zoomlevel.
  489. //
  490. // we always expect entry.rootBounds.top to equal MINIMAP_CONTAINER_HEIGHT
  491. const actualRootTop = Math.ceil(entry.rootBounds.top);
  492. if (actualRootTop !== MINIMAP_CONTAINER_HEIGHT && actualRootTop > 0) {
  493. // we revert the actualRootTop value by the current zoomLevel factor
  494. const normalizedActualTop = actualRootTop / this.zoomLevel;
  495. const zoomLevel = MINIMAP_CONTAINER_HEIGHT / normalizedActualTop;
  496. this.zoomLevel = zoomLevel;
  497. // we reconnect the observers; the callback functions may be invoked
  498. this.connectObservers();
  499. // NOTE: since we cannot guarantee that the callback function is invoked on
  500. // the newly connected observers, we continue running this function.
  501. }
  502. }
  503. // root refers to the root intersection rectangle used for the IntersectionObserver
  504. const rectRelativeToRoot = entry.boundingClientRect as DOMRect;
  505. const bottomYCoord = rectRelativeToRoot.y + rectRelativeToRoot.height;
  506. // refers to if the rect is out of view from the viewport
  507. const isOutOfViewAbove = rectRelativeToRoot.y < 0 && bottomYCoord < 0;
  508. if (isOutOfViewAbove) {
  509. return;
  510. }
  511. const relativeToMinimap = {
  512. top: rectRelativeToRoot.y - MINIMAP_CONTAINER_HEIGHT,
  513. bottom: bottomYCoord - MINIMAP_CONTAINER_HEIGHT,
  514. };
  515. const rectBelowMinimap =
  516. relativeToMinimap.top > 0 && relativeToMinimap.bottom > 0;
  517. if (rectBelowMinimap) {
  518. // if the first span is below the minimap, we scroll the minimap
  519. // to the top. this addresses spurious scrolling to the top of the page
  520. if (spanNumber <= 1) {
  521. minimapSlider.style.top = '0px';
  522. return;
  523. }
  524. return;
  525. }
  526. const inAndAboveMinimap = relativeToMinimap.bottom <= 0;
  527. if (inAndAboveMinimap) {
  528. return;
  529. }
  530. // invariant: spanNumber >= 1
  531. const numberOfMovedSpans = spanNumber - 1;
  532. const totalHeightOfHiddenSpans = numberOfMovedSpans * MINIMAP_SPAN_BAR_HEIGHT;
  533. const currentSpanHiddenRatio = 1 - entry.intersectionRatio;
  534. const panYPixels =
  535. totalHeightOfHiddenSpans + currentSpanHiddenRatio * MINIMAP_SPAN_BAR_HEIGHT;
  536. // invariant: this.props.numOfSpans - spanNumberToStopMoving + 1 = NUM_OF_SPANS_FIT_IN_MINI_MAP
  537. const spanNumberToStopMoving =
  538. this.props.numOfSpans + 1 - NUM_OF_SPANS_FIT_IN_MINI_MAP;
  539. if (spanNumber > spanNumberToStopMoving) {
  540. // if the last span bar appears on the minimap, we do not want the minimap
  541. // to keep panning upwards
  542. minimapSlider.style.top = `-${
  543. spanNumberToStopMoving * MINIMAP_SPAN_BAR_HEIGHT
  544. }px`;
  545. return;
  546. }
  547. minimapSlider.style.top = `-${panYPixels}px`;
  548. });
  549. },
  550. {
  551. threshold: INTERSECTION_THRESHOLDS,
  552. rootMargin: `-${MINIMAP_CONTAINER_HEIGHT * this.zoomLevel}px 0px 0px 0px`,
  553. }
  554. );
  555. this.intersectionObserver.observe(this.spanRowDOMRef.current);
  556. }
  557. disconnectObservers() {
  558. if (this.intersectionObserver) {
  559. this.intersectionObserver.disconnect();
  560. }
  561. }
  562. renderDivider(
  563. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  564. ) {
  565. if (this.state.showDetail) {
  566. // Mock component to preserve layout spacing
  567. return (
  568. <DividerLine
  569. showDetail
  570. style={{
  571. position: 'absolute',
  572. }}
  573. />
  574. );
  575. }
  576. const {addDividerLineRef} = dividerHandlerChildrenProps;
  577. return (
  578. <DividerLine
  579. ref={addDividerLineRef()}
  580. style={{
  581. position: 'absolute',
  582. }}
  583. onMouseEnter={() => {
  584. dividerHandlerChildrenProps.setHover(true);
  585. }}
  586. onMouseLeave={() => {
  587. dividerHandlerChildrenProps.setHover(false);
  588. }}
  589. onMouseOver={() => {
  590. dividerHandlerChildrenProps.setHover(true);
  591. }}
  592. onMouseDown={dividerHandlerChildrenProps.onDragStart}
  593. onClick={event => {
  594. // we prevent the propagation of the clicks from this component to prevent
  595. // the span detail from being opened.
  596. event.stopPropagation();
  597. }}
  598. />
  599. );
  600. }
  601. getRelatedErrors(quickTrace: QuickTraceContextChildrenProps): TraceError[] | null {
  602. if (!quickTrace) {
  603. return null;
  604. }
  605. const {span} = this.props;
  606. const {currentEvent} = quickTrace;
  607. if (isGapSpan(span) || !currentEvent || !isTraceFull(currentEvent)) {
  608. return null;
  609. }
  610. return currentEvent.errors.filter(error => error.span === span.span_id);
  611. }
  612. getChildTransactions(
  613. quickTrace: QuickTraceContextChildrenProps
  614. ): QuickTraceEvent[] | null {
  615. if (!quickTrace) {
  616. return null;
  617. }
  618. const {span} = this.props;
  619. const {trace} = quickTrace;
  620. if (isGapSpan(span) || !trace) {
  621. return null;
  622. }
  623. return trace.filter(({parent_span_id}) => parent_span_id === span.span_id);
  624. }
  625. renderErrorBadge(errors: TraceError[] | null): React.ReactNode {
  626. return errors?.length ? <ErrorBadge /> : null;
  627. }
  628. renderEmbeddedTransactionsBadge(
  629. transactions: QuickTraceEvent[] | null
  630. ): React.ReactNode {
  631. const {toggleEmbeddedChildren, organization, showEmbeddedChildren} = this.props;
  632. if (!organization.features.includes('unified-span-view')) {
  633. return null;
  634. }
  635. if (transactions && transactions.length === 1) {
  636. const transaction = transactions[0];
  637. return (
  638. <Tooltip
  639. title={
  640. <span>
  641. {showEmbeddedChildren
  642. ? t('This span is showing a direct child. Remove transaction to hide')
  643. : t('This span has a direct child. Add transaction to view')}
  644. <FeatureBadge type="beta" noTooltip />
  645. </span>
  646. }
  647. position="top"
  648. containerDisplayMode="block"
  649. >
  650. <EmbeddedTransactionBadge
  651. expanded={showEmbeddedChildren}
  652. onClick={() => {
  653. if (toggleEmbeddedChildren) {
  654. if (showEmbeddedChildren) {
  655. trackAnalyticsEvent({
  656. eventKey: 'span_view.embedded_child.hide',
  657. eventName: 'Span View: Hide Embedded Transaction',
  658. organization_id: parseInt(organization.id, 10),
  659. });
  660. } else {
  661. trackAnalyticsEvent({
  662. eventKey: 'span_view.embedded_child.show',
  663. eventName: 'Span View: Show Embedded Transaction',
  664. organization_id: parseInt(organization.id, 10),
  665. });
  666. }
  667. toggleEmbeddedChildren({
  668. orgSlug: organization.slug,
  669. eventSlug: generateEventSlug({
  670. id: transaction.event_id,
  671. project: transaction.project_slug,
  672. }),
  673. });
  674. }
  675. }}
  676. />
  677. </Tooltip>
  678. );
  679. }
  680. return null;
  681. }
  682. renderWarningText({warningText}: {warningText?: string} = {}) {
  683. if (!warningText) {
  684. return null;
  685. }
  686. return (
  687. <Tooltip containerDisplayMode="flex" title={warningText}>
  688. <StyledIconWarning size="xs" />
  689. </Tooltip>
  690. );
  691. }
  692. renderHeader({
  693. scrollbarManagerChildrenProps,
  694. dividerHandlerChildrenProps,
  695. errors,
  696. transactions,
  697. }: {
  698. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps;
  699. scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps;
  700. errors: TraceError[] | null;
  701. transactions: QuickTraceEvent[] | null;
  702. }) {
  703. const {span, spanBarColor, spanBarHatch, spanNumber} = this.props;
  704. const startTimestamp: number = span.start_timestamp;
  705. const endTimestamp: number = span.timestamp;
  706. const duration = Math.abs(endTimestamp - startTimestamp);
  707. const durationString = getHumanDuration(duration);
  708. const bounds = this.getBounds();
  709. const {dividerPosition, addGhostDividerLineRef} = dividerHandlerChildrenProps;
  710. const displaySpanBar = defined(bounds.left) && defined(bounds.width);
  711. const durationDisplay = getDurationDisplay(bounds);
  712. return (
  713. <RowCellContainer showDetail={this.state.showDetail}>
  714. <RowCell
  715. data-type="span-row-cell"
  716. showDetail={this.state.showDetail}
  717. style={{
  718. width: `calc(${toPercent(dividerPosition)} - 0.5px)`,
  719. paddingTop: 0,
  720. }}
  721. onClick={() => {
  722. this.toggleDisplayDetail();
  723. }}
  724. >
  725. {this.renderTitle(scrollbarManagerChildrenProps, errors)}
  726. </RowCell>
  727. <DividerContainer>
  728. {this.renderDivider(dividerHandlerChildrenProps)}
  729. {this.renderErrorBadge(errors)}
  730. {this.renderEmbeddedTransactionsBadge(transactions)}
  731. </DividerContainer>
  732. <RowCell
  733. data-type="span-row-cell"
  734. showDetail={this.state.showDetail}
  735. showStriping={spanNumber % 2 !== 0}
  736. style={{
  737. width: `calc(${toPercent(1 - dividerPosition)} - 0.5px)`,
  738. }}
  739. onClick={() => {
  740. this.toggleDisplayDetail();
  741. }}
  742. >
  743. {displaySpanBar && (
  744. <RowRectangle
  745. spanBarHatch={!!spanBarHatch}
  746. style={{
  747. backgroundColor: spanBarColor,
  748. left: `min(${toPercent(bounds.left || 0)}, calc(100% - 1px))`,
  749. width: toPercent(bounds.width || 0),
  750. }}
  751. >
  752. <DurationPill
  753. durationDisplay={durationDisplay}
  754. showDetail={this.state.showDetail}
  755. spanBarHatch={!!spanBarHatch}
  756. >
  757. {durationString}
  758. {this.renderWarningText({warningText: bounds.warning})}
  759. </DurationPill>
  760. </RowRectangle>
  761. )}
  762. {this.renderMeasurements()}
  763. <SpanBarCursorGuide />
  764. </RowCell>
  765. {!this.state.showDetail && (
  766. <DividerLineGhostContainer
  767. style={{
  768. width: `calc(${toPercent(dividerPosition)} + 0.5px)`,
  769. display: 'none',
  770. }}
  771. >
  772. <DividerLine
  773. ref={addGhostDividerLineRef()}
  774. style={{
  775. right: 0,
  776. }}
  777. className="hovering"
  778. onClick={event => {
  779. // the ghost divider line should not be interactive.
  780. // we prevent the propagation of the clicks from this component to prevent
  781. // the span detail from being opened.
  782. event.stopPropagation();
  783. }}
  784. />
  785. </DividerLineGhostContainer>
  786. )}
  787. </RowCellContainer>
  788. );
  789. }
  790. renderEmbeddedChildrenState() {
  791. const {fetchEmbeddedChildrenState} = this.props;
  792. switch (fetchEmbeddedChildrenState) {
  793. case 'loading_embedded_transactions': {
  794. return (
  795. <MessageRow>
  796. <span>{t('Loading embedded transaction')}</span>
  797. </MessageRow>
  798. );
  799. }
  800. case 'error_fetching_embedded_transactions': {
  801. return (
  802. <MessageRow>
  803. <span>{t('Error loading embedded transaction')}</span>
  804. </MessageRow>
  805. );
  806. }
  807. default:
  808. return null;
  809. }
  810. }
  811. render() {
  812. const bounds = this.getBounds();
  813. const {isSpanVisibleInView} = bounds;
  814. return (
  815. <React.Fragment>
  816. <Row
  817. ref={this.spanRowDOMRef}
  818. visible={isSpanVisibleInView}
  819. showBorder={this.state.showDetail}
  820. data-test-id="span-row"
  821. >
  822. <QuickTraceContext.Consumer>
  823. {quickTrace => {
  824. const errors = this.getRelatedErrors(quickTrace);
  825. const transactions = this.getChildTransactions(quickTrace);
  826. return (
  827. <React.Fragment>
  828. <ScrollbarManager.Consumer>
  829. {scrollbarManagerChildrenProps => (
  830. <DividerHandlerManager.Consumer>
  831. {(
  832. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  833. ) =>
  834. this.renderHeader({
  835. dividerHandlerChildrenProps,
  836. scrollbarManagerChildrenProps,
  837. errors,
  838. transactions,
  839. })
  840. }
  841. </DividerHandlerManager.Consumer>
  842. )}
  843. </ScrollbarManager.Consumer>
  844. {this.renderDetail({
  845. isVisible: isSpanVisibleInView,
  846. transactions,
  847. errors,
  848. })}
  849. </React.Fragment>
  850. );
  851. }}
  852. </QuickTraceContext.Consumer>
  853. </Row>
  854. {this.renderEmbeddedChildrenState()}
  855. </React.Fragment>
  856. );
  857. }
  858. }
  859. const StyledIconWarning = styled(IconWarning)`
  860. margin-left: ${space(0.25)};
  861. margin-bottom: ${space(0.25)};
  862. `;
  863. const Regroup = styled('span')``;
  864. export default SpanBar;