utils.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import type {Location, Query} from 'history';
  2. import pick from 'lodash/pick';
  3. import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
  4. import {t} from 'sentry/locale';
  5. import {defined} from 'sentry/utils';
  6. import EventView from 'sentry/utils/discover/eventView';
  7. import {isAggregateField} from 'sentry/utils/discover/fields';
  8. import type {SpanSlug} from 'sentry/utils/performance/suspectSpans/types';
  9. import {decodeScalar} from 'sentry/utils/queryString';
  10. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  11. import type {SpanSort, SpanSortOption} from './types';
  12. import {SpanSortOthers, SpanSortPercentiles} from './types';
  13. export function generateSpansRoute({orgSlug}: {orgSlug: string}): string {
  14. return `/organizations/${orgSlug}/performance/summary/spans/`;
  15. }
  16. export function spansRouteWithQuery({
  17. orgSlug,
  18. transaction,
  19. projectID,
  20. query,
  21. }: {
  22. orgSlug: string;
  23. query: Query;
  24. transaction: string;
  25. projectID?: string | string[];
  26. }) {
  27. const pathname = generateSpansRoute({
  28. orgSlug,
  29. });
  30. return {
  31. pathname,
  32. query: {
  33. transaction,
  34. project: projectID,
  35. environment: query.environment,
  36. statsPeriod: query.statsPeriod,
  37. start: query.start,
  38. end: query.end,
  39. query: query.query,
  40. },
  41. };
  42. }
  43. export const SPAN_RETENTION_DAYS = 30;
  44. export const SPAN_RELATIVE_PERIODS = pick(DEFAULT_RELATIVE_PERIODS, [
  45. '1h',
  46. '24h',
  47. '7d',
  48. '14d',
  49. '30d',
  50. ]);
  51. export const SPAN_SORT_OPTIONS: SpanSortOption[] = [
  52. {
  53. prefix: t('Sort By'),
  54. label: t('Total Self Time'),
  55. field: SpanSortOthers.SUM_EXCLUSIVE_TIME,
  56. },
  57. {
  58. prefix: t('Sort By'),
  59. label: t('Average Count'),
  60. field: SpanSortOthers.AVG_OCCURRENCE,
  61. },
  62. {
  63. prefix: t('Sort By'),
  64. label: t('p50 Self Time'),
  65. field: SpanSortPercentiles.P50_EXCLUSIVE_TIME,
  66. },
  67. {
  68. prefix: t('Sort By'),
  69. label: t('p75 Self Time'),
  70. field: SpanSortPercentiles.P75_EXCLUSIVE_TIME,
  71. },
  72. {
  73. prefix: t('Sort By'),
  74. label: t('p95 Self Time'),
  75. field: SpanSortPercentiles.P95_EXCLUSIVE_TIME,
  76. },
  77. {
  78. prefix: t('Sort By'),
  79. label: t('p99 Self Time'),
  80. field: SpanSortPercentiles.P99_EXCLUSIVE_TIME,
  81. },
  82. ];
  83. const DEFAULT_SORT = SpanSortOthers.SUM_EXCLUSIVE_TIME;
  84. function getSuspectSpanSort(sort: string): SpanSortOption {
  85. const selected = SPAN_SORT_OPTIONS.find(option => option.field === sort);
  86. if (selected) {
  87. return selected;
  88. }
  89. return SPAN_SORT_OPTIONS.find(option => option.field === DEFAULT_SORT)!;
  90. }
  91. export function getSuspectSpanSortFromLocation(
  92. location: Location,
  93. sortKey: string = 'sort'
  94. ): SpanSortOption {
  95. const sort = decodeScalar(location?.query?.[sortKey]) ?? DEFAULT_SORT;
  96. return getSuspectSpanSort(sort);
  97. }
  98. export function getSuspectSpanSortFromEventView(eventView: EventView): SpanSortOption {
  99. const sort = eventView.sorts.length ? eventView.sorts[0].field : DEFAULT_SORT;
  100. return getSuspectSpanSort(sort);
  101. }
  102. export function parseSpanSlug(spanSlug: string | undefined): SpanSlug | undefined {
  103. if (!defined(spanSlug)) {
  104. return undefined;
  105. }
  106. const delimiterPos = spanSlug.lastIndexOf(':');
  107. if (delimiterPos < 0) {
  108. return undefined;
  109. }
  110. const op = spanSlug.slice(0, delimiterPos);
  111. const group = spanSlug.slice(delimiterPos + 1);
  112. return {op, group};
  113. }
  114. export function generateSpansEventView({
  115. location,
  116. transactionName,
  117. }: {
  118. location: Location;
  119. transactionName: string;
  120. }): EventView {
  121. const query = decodeScalar(location.query.query, '');
  122. const conditions = new MutableSearch(query);
  123. conditions.setFilterValues('event.type', ['transaction']);
  124. conditions.setFilterValues('transaction', [transactionName]);
  125. Object.keys(conditions.filters).forEach(field => {
  126. if (isAggregateField(field)) {
  127. conditions.removeFilter(field);
  128. }
  129. });
  130. const eventView = EventView.fromNewQueryWithLocation(
  131. {
  132. id: undefined,
  133. version: 2,
  134. name: transactionName,
  135. fields: [...Object.values(SpanSortOthers), ...Object.values(SpanSortPercentiles)],
  136. query: conditions.formatString(),
  137. projects: [],
  138. },
  139. location
  140. );
  141. const sort = getSuspectSpanSortFromLocation(location);
  142. return eventView.withSorts([{field: sort.field, kind: 'desc'}]);
  143. }
  144. /**
  145. * For the totals view, we want to get some transaction level stats like
  146. * the number of transactions and the sum of the transaction duration.
  147. * This requires the removal of any aggregate conditions as they can result
  148. * in unexpected empty responses.
  149. */
  150. export function getTotalsView(eventView: EventView): EventView {
  151. const totalsView = eventView.withColumns([
  152. {kind: 'function', function: ['count', '', undefined, undefined]},
  153. {kind: 'function', function: ['sum', 'transaction.duration', undefined, undefined]},
  154. ]);
  155. const conditions = new MutableSearch(eventView.query);
  156. // filter out any aggregate conditions
  157. Object.keys(conditions.filters).forEach(field => {
  158. if (isAggregateField(field)) {
  159. conditions.removeFilter(field);
  160. }
  161. });
  162. totalsView.query = conditions.formatString();
  163. return totalsView;
  164. }
  165. export const SPAN_SORT_TO_FIELDS: Record<SpanSort, string[]> = {
  166. [SpanSortOthers.SUM_EXCLUSIVE_TIME]: [
  167. 'percentileArray(spans_exclusive_time, 0.75)',
  168. 'count()',
  169. 'count_unique(id)',
  170. 'sumArray(spans_exclusive_time)',
  171. ],
  172. [SpanSortOthers.AVG_OCCURRENCE]: [
  173. 'percentileArray(spans_exclusive_time, 0.75)',
  174. 'count()',
  175. 'count_unique(id)',
  176. 'equation|count() / count_unique(id)',
  177. 'sumArray(spans_exclusive_time)',
  178. ],
  179. [SpanSortPercentiles.P50_EXCLUSIVE_TIME]: [
  180. 'percentileArray(spans_exclusive_time, 0.5)',
  181. 'count()',
  182. 'count_unique(id)',
  183. 'sumArray(spans_exclusive_time)',
  184. ],
  185. [SpanSortPercentiles.P75_EXCLUSIVE_TIME]: [
  186. 'percentileArray(spans_exclusive_time, 0.75)',
  187. 'count()',
  188. 'count_unique(id)',
  189. 'sumArray(spans_exclusive_time)',
  190. ],
  191. [SpanSortPercentiles.P95_EXCLUSIVE_TIME]: [
  192. 'percentileArray(spans_exclusive_time, 0.95)',
  193. 'count()',
  194. 'count_unique(id)',
  195. 'sumArray(spans_exclusive_time)',
  196. ],
  197. [SpanSortPercentiles.P99_EXCLUSIVE_TIME]: [
  198. 'percentileArray(spans_exclusive_time, 0.99)',
  199. 'count()',
  200. 'count_unique(id)',
  201. 'sumArray(spans_exclusive_time)',
  202. ],
  203. };
  204. export function getExclusiveTimeDisplayedValue(value: string): string {
  205. return value.replace('exclusive', 'self');
  206. }