useSpanTransactionMetrics.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {Location} from 'history';
  2. import moment, {Moment} from 'moment';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import type {IndexedSpan} from 'sentry/views/starfish/queries/types';
  8. import {getDateFilters} from 'sentry/views/starfish/utils/dates';
  9. import {getDateQueryFilter} from 'sentry/views/starfish/utils/getDateQueryFilter';
  10. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  11. export type SpanTransactionMetrics = {
  12. 'p50(span.duration)': number;
  13. 'p95(span.duration)': number;
  14. 'spm()': number;
  15. 'sum(span.self_time)': number;
  16. 'time_spent_percentage(local)': number;
  17. transaction: string;
  18. };
  19. export const useSpanTransactionMetrics = (
  20. span?: Pick<IndexedSpan, 'group'>,
  21. transactions?: string[],
  22. _referrer = 'span-transaction-metrics'
  23. ) => {
  24. const location = useLocation();
  25. const pageFilters = usePageFilters();
  26. const {startTime, endTime} = getDateFilters(pageFilters);
  27. const dateFilters = getDateQueryFilter(startTime, endTime);
  28. const query = span
  29. ? getQuery(span, startTime, endTime, dateFilters, transactions ?? [])
  30. : '';
  31. const eventView = span ? getEventView(span, location, transactions ?? []) : undefined;
  32. const {isLoading, data} = useSpansQuery<SpanTransactionMetrics[]>({
  33. eventView,
  34. queryString: query,
  35. initialData: [],
  36. enabled: Boolean(query),
  37. });
  38. return {isLoading, data};
  39. };
  40. function getQuery(
  41. span: {group: string},
  42. startTime: Moment,
  43. endTime: Moment,
  44. dateFilters: string,
  45. transactions: string[]
  46. ) {
  47. return `
  48. SELECT
  49. transaction,
  50. quantile(0.5)(exclusive_time) as "p50(span.duration)",
  51. quantile(0.95)(exclusive_time) as "p95(span.duration)",
  52. sum(exclusive_time) as "sum(span.self_time)",
  53. divide(count(), ${
  54. moment(endTime ?? undefined).unix() - moment(startTime).unix()
  55. }) as "spm()"
  56. FROM spans_experimental_starfish
  57. WHERE group_id = '${span.group}'
  58. ${dateFilters}
  59. AND transaction IN ('${transactions.join("','")}')
  60. GROUP BY transaction
  61. `;
  62. }
  63. function getEventView(span: {group: string}, location: Location, transactions: string[]) {
  64. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  65. return EventView.fromNewQueryWithLocation(
  66. {
  67. name: '',
  68. query: `span.group:${cleanGroupId}${
  69. transactions.length > 0 ? ` transaction:[${transactions.join(',')}]` : ''
  70. }`,
  71. fields: [
  72. 'transaction',
  73. 'spm()',
  74. 'sum(span.duration)',
  75. 'p95(span.duration)',
  76. 'time_spent_percentage(local)',
  77. ],
  78. orderby: '-time_spent_percentage_local',
  79. dataset: DiscoverDatasets.SPANS_METRICS,
  80. projects: [1],
  81. version: 2,
  82. },
  83. location
  84. );
  85. }