eventCustomPerformanceMetrics.tsx 9.0 KB

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