eventMetas.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import Button from 'sentry/components/button';
  5. import Clipboard from 'sentry/components/clipboard';
  6. import DateTime from 'sentry/components/dateTime';
  7. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  8. import TimeSince from 'sentry/components/timeSince';
  9. import Tooltip from 'sentry/components/tooltip';
  10. import {frontend} from 'sentry/data/platformCategories';
  11. import {IconCopy, IconPlay} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import space from 'sentry/styles/space';
  14. import {AvatarProject, OrganizationSummary} from 'sentry/types';
  15. import {Event, EventTransaction} from 'sentry/types/event';
  16. import {getShortEventId} from 'sentry/utils/events';
  17. import {getDuration} from 'sentry/utils/formatters';
  18. import getDynamicText from 'sentry/utils/getDynamicText';
  19. import {
  20. QuickTraceQueryChildrenProps,
  21. TraceMeta,
  22. } from 'sentry/utils/performance/quickTrace/types';
  23. import {isTransaction} from 'sentry/utils/performance/quickTrace/utils';
  24. import Projects from 'sentry/utils/projects';
  25. import theme from 'sentry/utils/theme';
  26. import QuickTraceMeta from './quickTraceMeta';
  27. import {MetaData} from './styles';
  28. type Props = Pick<
  29. React.ComponentProps<typeof QuickTraceMeta>,
  30. 'errorDest' | 'transactionDest'
  31. > & {
  32. event: Event;
  33. location: Location;
  34. meta: TraceMeta | null;
  35. organization: OrganizationSummary;
  36. projectId: string;
  37. quickTrace: QuickTraceQueryChildrenProps | null;
  38. };
  39. type State = {
  40. isLargeScreen: boolean;
  41. };
  42. /**
  43. * This should match the breakpoint chosen for the `EventDetailHeader` below
  44. */
  45. const BREAKPOINT_MEDIA_QUERY = `(min-width: ${theme.breakpoints.large})`;
  46. class EventMetas extends Component<Props, State> {
  47. state: State = {
  48. isLargeScreen: window.matchMedia?.(BREAKPOINT_MEDIA_QUERY)?.matches,
  49. };
  50. componentDidMount() {
  51. if (this.mq) {
  52. this.mq.addEventListener('change', this.handleMediaQueryChange);
  53. }
  54. }
  55. componentWillUnmount() {
  56. if (this.mq) {
  57. this.mq.removeEventListener('change', this.handleMediaQueryChange);
  58. }
  59. }
  60. mq = window.matchMedia?.(BREAKPOINT_MEDIA_QUERY);
  61. handleMediaQueryChange = (changed: MediaQueryListEvent) => {
  62. this.setState({
  63. isLargeScreen: changed.matches,
  64. });
  65. };
  66. render() {
  67. const {
  68. event,
  69. organization,
  70. projectId,
  71. location,
  72. quickTrace,
  73. meta,
  74. errorDest,
  75. transactionDest,
  76. } = this.props;
  77. const {isLargeScreen} = this.state;
  78. // Replay preview gets rendered as part of the breadcrumb section. We need
  79. // to check for presence of both to show the replay link button here.
  80. const hasReplay =
  81. organization.features.includes('session-replay-ui') &&
  82. Boolean(event.entries.find(({type}) => type === 'breadcrumbs')) &&
  83. Boolean(event?.tags?.find(({key}) => key === 'replayId')?.value);
  84. const type = isTransaction(event) ? 'transaction' : 'event';
  85. const timestamp = (
  86. <TimeSince date={event.dateCreated || (event.endTimestamp || 0) * 1000} />
  87. );
  88. const httpStatus = <HttpStatus event={event} />;
  89. return (
  90. <Projects orgId={organization.slug} slugs={[projectId]}>
  91. {({projects}) => {
  92. const project = projects.find(p => p.slug === projectId);
  93. return (
  94. <EventDetailHeader type={type} hasReplay={hasReplay}>
  95. <MetaData
  96. headingText={t('Event ID')}
  97. tooltipText={t('The unique ID assigned to this %s.', type)}
  98. bodyText={<EventID event={event} />}
  99. subtext={
  100. <ProjectBadge
  101. project={project ? project : {slug: projectId}}
  102. avatarSize={16}
  103. />
  104. }
  105. />
  106. {isTransaction(event) ? (
  107. <MetaData
  108. headingText={t('Event Duration')}
  109. tooltipText={t(
  110. 'The time elapsed between the start and end of this transaction.'
  111. )}
  112. bodyText={getDuration(
  113. event.endTimestamp - event.startTimestamp,
  114. 2,
  115. true
  116. )}
  117. subtext={timestamp}
  118. />
  119. ) : (
  120. <MetaData
  121. headingText={t('Created')}
  122. tooltipText={t('The time at which this event was created.')}
  123. bodyText={timestamp}
  124. subtext={getDynamicText({
  125. value: <DateTime date={event.dateCreated} />,
  126. fixed: 'May 6, 2021 3:27:01 UTC',
  127. })}
  128. />
  129. )}
  130. {isTransaction(event) && (
  131. <MetaData
  132. headingText={t('Status')}
  133. tooltipText={t(
  134. 'The status of this transaction indicating if it succeeded or otherwise.'
  135. )}
  136. bodyText={getStatusBodyText(project, event, meta)}
  137. subtext={httpStatus}
  138. />
  139. )}
  140. {hasReplay && (
  141. <ReplayButtonContainer>
  142. <Button href="#breadcrumbs" size="sm" icon={<IconPlay size="xs" />}>
  143. {t('Replay')}
  144. </Button>
  145. </ReplayButtonContainer>
  146. )}
  147. <QuickTraceContainer>
  148. <QuickTraceMeta
  149. event={event}
  150. project={project}
  151. location={location}
  152. quickTrace={quickTrace}
  153. traceMeta={meta}
  154. anchor={isLargeScreen ? 'right' : 'left'}
  155. errorDest={errorDest}
  156. transactionDest={transactionDest}
  157. />
  158. </QuickTraceContainer>
  159. </EventDetailHeader>
  160. );
  161. }}
  162. </Projects>
  163. );
  164. }
  165. }
  166. type EventDetailHeaderProps = {
  167. hasReplay: boolean;
  168. type?: 'transaction' | 'event';
  169. };
  170. function getEventDetailHeaderCols({hasReplay, type}: EventDetailHeaderProps): string {
  171. if (type === 'transaction') {
  172. return hasReplay
  173. ? 'grid-template-columns: minmax(160px, 1fr) minmax(160px, 1fr) minmax(160px, 1fr) 5fr minmax(325px, 1fr);'
  174. : 'grid-template-columns: minmax(160px, 1fr) minmax(160px, 1fr) minmax(160px, 1fr) 6fr;';
  175. }
  176. return hasReplay
  177. ? 'grid-template-columns: minmax(160px, 1fr) minmax(200px, 1fr) 5fr minmax(325px, 1fr);'
  178. : 'grid-template-columns: minmax(160px, 1fr) minmax(200px, 1fr) 6fr;';
  179. }
  180. const EventDetailHeader = styled('div')<EventDetailHeaderProps>`
  181. display: grid;
  182. grid-template-columns: repeat(${p => (p.type === 'transaction' ? 3 : 2)}, 1fr);
  183. grid-template-rows: repeat(2, auto);
  184. gap: ${space(2)};
  185. margin-bottom: ${space(2)};
  186. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  187. margin-bottom: 0;
  188. }
  189. /* This should match the breakpoint chosen for BREAKPOINT_MEDIA_QUERY above. */
  190. @media (min-width: ${p => p.theme.breakpoints.large}) {
  191. ${p => getEventDetailHeaderCols(p)};
  192. grid-row-gap: 0;
  193. }
  194. `;
  195. const ReplayButtonContainer = styled('div')`
  196. order: 2;
  197. display: flex;
  198. justify-content: flex-end;
  199. align-items: flex-start;
  200. @media (min-width: ${p => p.theme.breakpoints.large}) {
  201. order: 4;
  202. }
  203. `;
  204. const QuickTraceContainer = styled('div')`
  205. grid-column: 1 / -2;
  206. order: 1;
  207. @media (min-width: ${p => p.theme.breakpoints.large}) {
  208. order: 5;
  209. justify-self: flex-end;
  210. min-width: 325px;
  211. grid-column: unset;
  212. }
  213. `;
  214. function EventID({event}: {event: Event}) {
  215. return (
  216. <Clipboard value={event.eventID}>
  217. <EventIDContainer>
  218. <EventIDWrapper>{getShortEventId(event.eventID)}</EventIDWrapper>
  219. <Tooltip title={event.eventID} position="top">
  220. <IconCopy color="subText" />
  221. </Tooltip>
  222. </EventIDContainer>
  223. </Clipboard>
  224. );
  225. }
  226. const EventIDContainer = styled('div')`
  227. display: flex;
  228. align-items: center;
  229. cursor: pointer;
  230. `;
  231. const EventIDWrapper = styled('span')`
  232. margin-right: ${space(1)};
  233. `;
  234. function HttpStatus({event}: {event: Event}) {
  235. const {tags} = event;
  236. const emptyStatus = <Fragment>{'\u2014'}</Fragment>;
  237. if (!Array.isArray(tags)) {
  238. return emptyStatus;
  239. }
  240. const tag = tags.find(tagObject => tagObject.key === 'http.status_code');
  241. if (!tag) {
  242. return emptyStatus;
  243. }
  244. return <Fragment>HTTP {tag.value}</Fragment>;
  245. }
  246. /*
  247. TODO: Ash
  248. I put this in place as a temporary patch to prevent successful frontend transactions from being set as 'unknown', which is what Relay sets by default
  249. if there is no status set by the SDK. In the future, the possible statuses will be revised and frontend transactions should properly have a status set.
  250. When that change is implemented, this function can simply be replaced with:
  251. event.contexts?.trace?.status ?? '\u2014';
  252. */
  253. function getStatusBodyText(
  254. project: AvatarProject | undefined,
  255. event: EventTransaction,
  256. meta: TraceMeta | null
  257. ): string {
  258. const isFrontendProject = frontend.some(val => val === project?.platform);
  259. if (
  260. isFrontendProject &&
  261. meta &&
  262. meta.errors === 0 &&
  263. event.contexts?.trace?.status === 'unknown'
  264. ) {
  265. return 'ok';
  266. }
  267. return event.contexts?.trace?.status ?? '\u2014';
  268. }
  269. export default EventMetas;