eventMetas.tsx 9.7 KB

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