spanBar.tsx 29 KB

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