spanBar.tsx 29 KB

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