eventCustomPerformanceMetrics.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import styled from '@emotion/styled';
  2. import type {Location} from 'history';
  3. import {SectionHeading} from 'sentry/components/charts/styles';
  4. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  5. import Panel from 'sentry/components/panels/panel';
  6. import {IconEllipsis} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {Event} from 'sentry/types/event';
  10. import type {Organization} from 'sentry/types/organization';
  11. import EventView from 'sentry/utils/discover/eventView';
  12. import {
  13. DURATION_UNITS,
  14. FIELD_FORMATTERS,
  15. PERCENTAGE_UNITS,
  16. SIZE_UNITS,
  17. } from 'sentry/utils/discover/fieldRenderers';
  18. import {isCustomMeasurement} from 'sentry/views/dashboards/utils';
  19. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  20. import {Tooltip} from '../tooltip';
  21. export enum EventDetailPageSource {
  22. PERFORMANCE = 'performance',
  23. DISCOVER = 'discover',
  24. }
  25. type Props = {
  26. event: Event;
  27. location: Location;
  28. organization: Organization;
  29. isHomepage?: boolean;
  30. source?: EventDetailPageSource;
  31. };
  32. export function isNotMarkMeasurement(field: string) {
  33. return !field.startsWith('mark.');
  34. }
  35. export function isNotPerformanceScoreMeasurement(field: string) {
  36. return !field.startsWith('score.');
  37. }
  38. export default function EventCustomPerformanceMetrics({
  39. event,
  40. location,
  41. organization,
  42. source,
  43. isHomepage,
  44. }: Props) {
  45. const measurementNames = Object.keys(event.measurements ?? {})
  46. .filter(name => isCustomMeasurement(`measurements.${name}`))
  47. .filter(isNotMarkMeasurement)
  48. .filter(isNotPerformanceScoreMeasurement)
  49. .sort();
  50. if (measurementNames.length === 0) {
  51. return null;
  52. }
  53. return (
  54. <Container>
  55. <SectionHeading>{t('Custom Performance Metrics')}</SectionHeading>
  56. <Measurements>
  57. {measurementNames.map(name => {
  58. return (
  59. <EventCustomPerformanceMetric
  60. key={name}
  61. event={event}
  62. name={name}
  63. location={location}
  64. organization={organization}
  65. source={source}
  66. isHomepage={isHomepage}
  67. />
  68. );
  69. })}
  70. </Measurements>
  71. </Container>
  72. );
  73. }
  74. type EventCustomPerformanceMetricProps = Props & {
  75. name: string;
  76. };
  77. export function getFieldTypeFromUnit(unit) {
  78. if (unit) {
  79. if (DURATION_UNITS[unit]) {
  80. return 'duration';
  81. }
  82. if (SIZE_UNITS[unit]) {
  83. return 'size';
  84. }
  85. if (PERCENTAGE_UNITS.includes(unit)) {
  86. return 'percentage';
  87. }
  88. if (unit === 'none') {
  89. return 'integer';
  90. }
  91. return 'string';
  92. }
  93. return 'number';
  94. }
  95. export function EventCustomPerformanceMetric({
  96. event,
  97. name,
  98. location,
  99. organization,
  100. source,
  101. isHomepage,
  102. }: EventCustomPerformanceMetricProps) {
  103. const {value, unit} = event.measurements?.[name] ?? {};
  104. if (value === null) {
  105. return null;
  106. }
  107. const fieldType = getFieldTypeFromUnit(unit);
  108. const renderValue = fieldType === 'string' ? `${value} ${unit}` : value;
  109. const rendered = fieldType
  110. ? FIELD_FORMATTERS[fieldType].renderFunc(
  111. name,
  112. {[name]: renderValue},
  113. {location, organization, unit}
  114. )
  115. : renderValue;
  116. function generateLinkWithQuery(query: string) {
  117. const eventView = EventView.fromLocation(location);
  118. eventView.query = query;
  119. switch (source) {
  120. case EventDetailPageSource.PERFORMANCE:
  121. return transactionSummaryRouteWithQuery({
  122. orgSlug: organization.slug,
  123. transaction: event.title,
  124. projectID: event.projectID,
  125. query: {query},
  126. });
  127. case EventDetailPageSource.DISCOVER:
  128. default:
  129. return eventView.getResultsViewUrlTarget(organization.slug, isHomepage);
  130. }
  131. }
  132. // Some custom perf metrics have units.
  133. // These custom perf metrics need to be adjusted to the correct value.
  134. let customMetricValue = value;
  135. if (typeof value === 'number' && unit && customMetricValue) {
  136. if (Object.keys(SIZE_UNITS).includes(unit)) {
  137. customMetricValue *= SIZE_UNITS[unit];
  138. } else if (Object.keys(DURATION_UNITS).includes(unit)) {
  139. customMetricValue *= DURATION_UNITS[unit];
  140. }
  141. }
  142. return (
  143. <StyledPanel>
  144. <div>
  145. <div>{name}</div>
  146. <ValueRow>
  147. <Value>{rendered}</Value>
  148. </ValueRow>
  149. </div>
  150. <StyledDropdownMenuControl
  151. items={[
  152. {
  153. key: 'includeEvents',
  154. label: t('Show events with this value'),
  155. to: generateLinkWithQuery(`measurements.${name}:${customMetricValue}`),
  156. },
  157. {
  158. key: 'excludeEvents',
  159. label: t('Hide events with this value'),
  160. to: generateLinkWithQuery(`!measurements.${name}:${customMetricValue}`),
  161. },
  162. {
  163. key: 'includeGreaterThanEvents',
  164. label: t('Show events with values greater than'),
  165. to: generateLinkWithQuery(`measurements.${name}:>${customMetricValue}`),
  166. },
  167. {
  168. key: 'includeLessThanEvents',
  169. label: t('Show events with values less than'),
  170. to: generateLinkWithQuery(`measurements.${name}:<${customMetricValue}`),
  171. },
  172. ]}
  173. triggerProps={{
  174. 'aria-label': t('Widget actions'),
  175. size: 'xs',
  176. borderless: true,
  177. showChevron: false,
  178. icon: <IconEllipsis direction="down" size="sm" />,
  179. }}
  180. position="bottom-end"
  181. />
  182. </StyledPanel>
  183. );
  184. }
  185. export function TraceEventCustomPerformanceMetric({
  186. event,
  187. name,
  188. location,
  189. organization,
  190. source,
  191. isHomepage,
  192. }: EventCustomPerformanceMetricProps) {
  193. const {value, unit} = event.measurements?.[name] ?? {};
  194. if (value === null) {
  195. return null;
  196. }
  197. const fieldType = getFieldTypeFromUnit(unit);
  198. const renderValue = fieldType === 'string' ? `${value} ${unit}` : value;
  199. const rendered = fieldType
  200. ? FIELD_FORMATTERS[fieldType].renderFunc(
  201. name,
  202. {[name]: renderValue},
  203. {location, organization, unit}
  204. )
  205. : renderValue;
  206. function generateLinkWithQuery(query: string) {
  207. const eventView = EventView.fromLocation(location);
  208. eventView.query = query;
  209. switch (source) {
  210. case EventDetailPageSource.PERFORMANCE:
  211. return transactionSummaryRouteWithQuery({
  212. orgSlug: organization.slug,
  213. transaction: event.title,
  214. projectID: event.projectID,
  215. query: {query},
  216. });
  217. case EventDetailPageSource.DISCOVER:
  218. default:
  219. return eventView.getResultsViewUrlTarget(organization.slug, isHomepage);
  220. }
  221. }
  222. // Some custom perf metrics have units.
  223. // These custom perf metrics need to be adjusted to the correct value.
  224. let customMetricValue = value;
  225. if (typeof value === 'number' && unit && customMetricValue) {
  226. if (Object.keys(SIZE_UNITS).includes(unit)) {
  227. customMetricValue *= SIZE_UNITS[unit];
  228. } else if (Object.keys(DURATION_UNITS).includes(unit)) {
  229. customMetricValue *= DURATION_UNITS[unit];
  230. }
  231. }
  232. return (
  233. <TraceStyledPanel>
  234. <Tooltip title={name} showOnlyOnOverflow>
  235. <StyledMeasurementsName>{name}</StyledMeasurementsName>
  236. </Tooltip>
  237. <div>{rendered}</div>
  238. <div>
  239. <StyledDropdownMenuControl
  240. size="xs"
  241. items={[
  242. {
  243. key: 'includeEvents',
  244. label: t('Show events with this value'),
  245. to: generateLinkWithQuery(`measurements.${name}:${customMetricValue}`),
  246. },
  247. {
  248. key: 'excludeEvents',
  249. label: t('Hide events with this value'),
  250. to: generateLinkWithQuery(`!measurements.${name}:${customMetricValue}`),
  251. },
  252. {
  253. key: 'includeGreaterThanEvents',
  254. label: t('Show events with values greater than'),
  255. to: generateLinkWithQuery(`measurements.${name}:>${customMetricValue}`),
  256. },
  257. {
  258. key: 'includeLessThanEvents',
  259. label: t('Show events with values less than'),
  260. to: generateLinkWithQuery(`measurements.${name}:<${customMetricValue}`),
  261. },
  262. ]}
  263. triggerProps={{
  264. 'aria-label': t('Widget actions'),
  265. size: 'xs',
  266. borderless: true,
  267. showChevron: false,
  268. icon: <IconEllipsis direction="down" size="sm" />,
  269. }}
  270. position="bottom-end"
  271. />
  272. </div>
  273. </TraceStyledPanel>
  274. );
  275. }
  276. const Measurements = styled('div')`
  277. display: grid;
  278. grid-column-gap: ${space(1)};
  279. `;
  280. const Container = styled('div')`
  281. font-size: ${p => p.theme.fontSizeMedium};
  282. margin-bottom: ${space(4)};
  283. `;
  284. const TraceStyledPanel = styled(Panel)`
  285. margin-bottom: 0;
  286. display: flex;
  287. align-items: center;
  288. max-width: fit-content;
  289. font-size: ${p => p.theme.fontSizeSmall};
  290. gap: ${space(0.5)};
  291. > :not(:last-child) {
  292. padding: 0 ${space(1)};
  293. }
  294. `;
  295. const ValueRow = styled('div')`
  296. display: flex;
  297. align-items: center;
  298. `;
  299. const Value = styled('span')`
  300. font-size: ${p => p.theme.fontSizeExtraLarge};
  301. `;
  302. const StyledPanel = styled(Panel)`
  303. padding: ${space(1)} ${space(1.5)};
  304. margin-bottom: ${space(1)};
  305. display: flex;
  306. `;
  307. const StyledDropdownMenuControl = styled(DropdownMenu)`
  308. display: block;
  309. margin-left: auto;
  310. `;
  311. const StyledMeasurementsName = styled('div')`
  312. max-width: 200px;
  313. ${p => p.theme.overflowEllipsis};
  314. `;