fieldRenderers.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import {useState} from 'react';
  2. import {type Theme, useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {LinkButton} from 'sentry/components/button';
  5. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  6. import Link from 'sentry/components/links/link';
  7. import {RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
  8. import {pickBarColor} from 'sentry/components/performance/waterfall/utils';
  9. import PerformanceDuration from 'sentry/components/performanceDuration';
  10. import TimeSince from 'sentry/components/timeSince';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {IconIssues} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {DateString} from 'sentry/types/core';
  16. import {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
  17. import {getShortEventId} from 'sentry/utils/events';
  18. import Projects from 'sentry/utils/projects';
  19. import {useLocation} from 'sentry/utils/useLocation';
  20. import useOrganization from 'sentry/utils/useOrganization';
  21. import usePageFilters from 'sentry/utils/usePageFilters';
  22. import useProjects from 'sentry/utils/useProjects';
  23. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  24. import {getTraceDetailsUrl} from 'sentry/views/performance/traceDetails/utils';
  25. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  26. import type {TraceResult} from './content';
  27. import type {Field} from './data';
  28. import {getShortenedSdkName, getStylingSliceName} from './utils';
  29. interface ProjectRendererProps {
  30. projectSlug: string;
  31. hideName?: boolean;
  32. }
  33. export function ProjectRenderer({projectSlug, hideName}: ProjectRendererProps) {
  34. const organization = useOrganization();
  35. return (
  36. <Projects orgId={organization.slug} slugs={[projectSlug]}>
  37. {({projects}) => {
  38. const project = projects.find(p => p.slug === projectSlug);
  39. return (
  40. <ProjectBadge
  41. hideName={hideName}
  42. project={project ? project : {slug: projectSlug}}
  43. avatarSize={16}
  44. />
  45. );
  46. }}
  47. </Projects>
  48. );
  49. }
  50. export const TraceBreakdownContainer = styled('div')<{hoveredIndex?: number}>`
  51. position: relative;
  52. display: flex;
  53. min-width: 200px;
  54. height: 15px;
  55. background-color: ${p => p.theme.gray100};
  56. ${p => `--hoveredSlice-${p.hoveredIndex ?? -1}-translateY: translateY(-3px)`};
  57. `;
  58. const RectangleTraceBreakdown = styled(RowRectangle)<{
  59. sliceColor: string;
  60. sliceName: string | null;
  61. offset?: number;
  62. }>`
  63. background-color: ${p => p.sliceColor};
  64. position: relative;
  65. width: 100%;
  66. height: 15px;
  67. ${p => `
  68. filter: var(--highlightedSlice-${p.sliceName}-saturate, var(--defaultSlice-saturate));
  69. `}
  70. ${p => `
  71. opacity: var(--highlightedSlice-${p.sliceName ?? ''}-opacity, var(--defaultSlice-opacity, 1.0));
  72. `}
  73. ${p => `
  74. transform: var(--hoveredSlice-${p.offset}-translateY, var(--highlightedSlice-${p.sliceName ?? ''}-transform, var(--defaultSlice-transform, 1.0)));
  75. `}
  76. transition: filter,opacity,transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  77. `;
  78. export function TraceBreakdownRenderer({
  79. trace,
  80. setHighlightedSliceName,
  81. }: {
  82. setHighlightedSliceName: (sliceName: string) => void;
  83. trace: TraceResult<Field>;
  84. }) {
  85. const theme = useTheme();
  86. const [hoveredIndex, setHoveredIndex] = useState(-1);
  87. return (
  88. <TraceBreakdownContainer
  89. data-test-id="relative-ops-breakdown"
  90. hoveredIndex={hoveredIndex}
  91. onMouseLeave={() => setHoveredIndex(-1)}
  92. >
  93. {trace.breakdowns.map((breakdown, index) => {
  94. return (
  95. <SpanBreakdownSliceRenderer
  96. key={breakdown.start + (breakdown.project ?? t('missing instrumentation'))}
  97. sliceName={breakdown.project}
  98. sliceStart={breakdown.start}
  99. sliceEnd={breakdown.end}
  100. sliceDurationReal={breakdown.duration}
  101. sliceSecondaryName={breakdown.sdkName}
  102. trace={trace}
  103. theme={theme}
  104. offset={index}
  105. onMouseEnter={() => {
  106. setHoveredIndex(index);
  107. breakdown.project
  108. ? setHighlightedSliceName(
  109. getStylingSliceName(breakdown.project, breakdown.sdkName) ?? ''
  110. )
  111. : null;
  112. }}
  113. />
  114. );
  115. })}
  116. </TraceBreakdownContainer>
  117. );
  118. }
  119. const BREAKDOWN_BAR_SIZE = 200;
  120. const BREAKDOWN_QUANTIZE_STEP = 1;
  121. const BREAKDOWN_NUM_SLICES = BREAKDOWN_BAR_SIZE / BREAKDOWN_QUANTIZE_STEP; // 200
  122. export function SpanBreakdownSliceRenderer({
  123. trace,
  124. theme,
  125. sliceName,
  126. sliceStart,
  127. sliceEnd,
  128. sliceDurationReal,
  129. sliceSecondaryName,
  130. onMouseEnter,
  131. offset,
  132. }: {
  133. onMouseEnter: () => void;
  134. sliceEnd: number;
  135. sliceName: string | null;
  136. sliceSecondaryName: string | null;
  137. sliceStart: number;
  138. theme: Theme;
  139. trace: TraceResult<Field>;
  140. offset?: number;
  141. sliceDurationReal?: number;
  142. }) {
  143. const traceSliceSize = (trace.end - trace.start) / BREAKDOWN_NUM_SLICES;
  144. const traceDuration = BREAKDOWN_NUM_SLICES * traceSliceSize;
  145. const sliceDuration = sliceEnd - sliceStart;
  146. if (sliceDuration <= 0) {
  147. return null;
  148. }
  149. const stylingSliceName = getStylingSliceName(sliceName, sliceSecondaryName);
  150. const sliceColor = stylingSliceName ? pickBarColor(stylingSliceName) : theme.gray100;
  151. const sliceWidth =
  152. BREAKDOWN_QUANTIZE_STEP *
  153. Math.ceil(BREAKDOWN_NUM_SLICES * (sliceDuration / traceDuration));
  154. const relativeSliceStart = sliceStart - trace.start;
  155. const sliceOffset =
  156. BREAKDOWN_QUANTIZE_STEP *
  157. Math.floor((BREAKDOWN_NUM_SLICES * relativeSliceStart) / traceDuration);
  158. return (
  159. <BreakdownSlice
  160. sliceName={sliceName}
  161. sliceOffset={sliceOffset}
  162. sliceWidth={sliceWidth}
  163. onMouseEnter={onMouseEnter}
  164. >
  165. <Tooltip
  166. title={
  167. <div>
  168. <FlexContainer>
  169. {sliceName ? <ProjectRenderer projectSlug={sliceName} hideName /> : null}
  170. <strong>{sliceName}</strong>
  171. <Subtext>({getShortenedSdkName(sliceSecondaryName)})</Subtext>
  172. </FlexContainer>
  173. <div>
  174. <PerformanceDuration
  175. milliseconds={sliceDurationReal ?? sliceDuration}
  176. abbreviation
  177. />
  178. </div>
  179. </div>
  180. }
  181. containerDisplayMode="block"
  182. >
  183. <RectangleTraceBreakdown
  184. sliceColor={sliceColor}
  185. sliceName={stylingSliceName}
  186. offset={offset}
  187. />
  188. </Tooltip>
  189. </BreakdownSlice>
  190. );
  191. }
  192. const Subtext = styled('span')`
  193. font-weight: 400;
  194. color: ${p => p.theme.gray300};
  195. `;
  196. const FlexContainer = styled('div')`
  197. display: flex;
  198. flex-direction: row;
  199. align-items: center;
  200. gap: ${space(0.5)};
  201. padding-bottom: ${space(0.5)};
  202. `;
  203. const BreakdownSlice = styled('div')<{
  204. sliceName: string | null;
  205. sliceOffset: number;
  206. sliceWidth: number;
  207. }>`
  208. position: absolute;
  209. width: max(3px, ${p => p.sliceWidth}px);
  210. left: ${p => p.sliceOffset}px;
  211. ${p => (p.sliceName ? null : 'z-index: -1;')}
  212. `;
  213. interface SpanIdRendererProps {
  214. projectSlug: string;
  215. spanId: string;
  216. timestamp: string;
  217. traceId: string;
  218. transactionId: string;
  219. }
  220. export function SpanIdRenderer({
  221. projectSlug,
  222. spanId,
  223. timestamp,
  224. traceId,
  225. transactionId,
  226. }: SpanIdRendererProps) {
  227. const location = useLocation();
  228. const organization = useOrganization();
  229. const target = generateLinkToEventInTraceView({
  230. projectSlug,
  231. traceSlug: traceId,
  232. timestamp,
  233. eventId: transactionId,
  234. organization,
  235. location,
  236. spanId,
  237. });
  238. return <Link to={target}>{getShortEventId(spanId)}</Link>;
  239. }
  240. interface TraceIdRendererProps {
  241. traceId: string;
  242. timestamp?: DateString;
  243. transactionId?: string;
  244. }
  245. export function TraceIdRenderer({
  246. traceId,
  247. timestamp,
  248. transactionId,
  249. }: TraceIdRendererProps) {
  250. const organization = useOrganization();
  251. const {selection} = usePageFilters();
  252. const stringOrNumberTimestamp =
  253. timestamp instanceof Date ? timestamp.toISOString() : timestamp ?? '';
  254. const target = getTraceDetailsUrl(
  255. organization,
  256. traceId,
  257. {
  258. start: selection.datetime.start,
  259. end: selection.datetime.end,
  260. statsPeriod: selection.datetime.period,
  261. },
  262. stringOrNumberTimestamp,
  263. transactionId
  264. );
  265. return (
  266. <Link to={target} style={{minWidth: '66px', textAlign: 'right'}}>
  267. {getShortEventId(traceId)}
  268. </Link>
  269. );
  270. }
  271. interface TransactionRendererProps {
  272. projectSlug: string;
  273. transaction: string;
  274. }
  275. export function TransactionRenderer({
  276. projectSlug,
  277. transaction,
  278. }: TransactionRendererProps) {
  279. const location = useLocation();
  280. const organization = useOrganization();
  281. const {projects} = useProjects({slugs: [projectSlug]});
  282. const target = transactionSummaryRouteWithQuery({
  283. orgSlug: organization.slug,
  284. transaction,
  285. query: {
  286. ...location.query,
  287. query: undefined,
  288. },
  289. projectID: String(projects[0]?.id ?? ''),
  290. });
  291. return <Link to={target}>{transaction}</Link>;
  292. }
  293. export function TraceIssuesRenderer({trace}: {trace: TraceResult<Field>}) {
  294. const organization = useOrganization();
  295. const issueCount = trace.numErrors + trace.numOccurrences;
  296. const issueText = issueCount >= 100 ? '99+' : issueCount === 0 ? '\u2014' : issueCount;
  297. return (
  298. <LinkButton
  299. to={normalizeUrl({
  300. pathname: `/organizations/${organization.slug}/issues`,
  301. query: {
  302. query: `trace:"${trace.trace}"`,
  303. },
  304. })}
  305. size="xs"
  306. icon={<IconIssues size="xs" />}
  307. disabled={issueCount === 0}
  308. style={{minHeight: '24px', height: '24px', minWidth: '44px'}}
  309. >
  310. {issueText}
  311. </LinkButton>
  312. );
  313. }
  314. export function SpanTimeRenderer({
  315. timestamp,
  316. tooltipShowSeconds,
  317. }: {
  318. timestamp: number;
  319. tooltipShowSeconds?: boolean;
  320. }) {
  321. const date = new Date(timestamp);
  322. return (
  323. <TimeSince
  324. unitStyle="extraShort"
  325. date={date}
  326. tooltipShowSeconds={tooltipShowSeconds}
  327. />
  328. );
  329. }