useSpanList.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {Location} from 'history';
  2. import omit from 'lodash/omit';
  3. import {defined} from 'sentry/utils';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import type {Sort} from 'sentry/utils/discover/fields';
  6. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import {ModuleName, SpanMetricsFields} from 'sentry/views/starfish/types';
  9. import {buildEventViewQuery} from 'sentry/views/starfish/utils/buildEventViewQuery';
  10. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  11. const {SPAN_SELF_TIME, SPAN_DESCRIPTION, SPAN_GROUP, SPAN_OP, SPAN_DOMAIN} =
  12. SpanMetricsFields;
  13. export type SpanMetrics = {
  14. 'http_error_count()': number;
  15. 'http_error_count_percent_change()': number;
  16. 'p95(span.self_time)': number;
  17. 'percentile_percent_change(span.self_time, 0.95)': number;
  18. 'span.description': string;
  19. 'span.domain': string;
  20. 'span.group': string;
  21. 'span.op': string;
  22. 'sps()': number;
  23. 'sps_percent_change()': number;
  24. 'sum(span.self_time)': number;
  25. 'time_spent_percentage()': number;
  26. 'time_spent_percentage(local)': number;
  27. };
  28. export const useSpanList = (
  29. moduleName: ModuleName,
  30. transaction?: string,
  31. method?: string,
  32. spanCategory?: string,
  33. sorts?: Sort[],
  34. limit?: number,
  35. referrer = 'api.starfish.use-span-list',
  36. cursor?: string
  37. ) => {
  38. const location = useLocation();
  39. const eventView = getEventView(
  40. moduleName,
  41. location,
  42. transaction,
  43. method,
  44. spanCategory,
  45. sorts
  46. );
  47. const {isLoading, data, meta, pageLinks} = useWrappedDiscoverQuery<SpanMetrics[]>({
  48. eventView,
  49. initialData: [],
  50. limit,
  51. referrer,
  52. cursor,
  53. });
  54. return {isLoading, data, meta, pageLinks};
  55. };
  56. function getEventView(
  57. moduleName: ModuleName,
  58. location: Location,
  59. transaction?: string,
  60. method?: string,
  61. spanCategory?: string,
  62. sorts?: Sort[]
  63. ) {
  64. const query = [
  65. ...buildEventViewQuery({
  66. moduleName,
  67. location,
  68. transaction,
  69. method,
  70. spanCategory,
  71. }),
  72. 'transaction.op:http.server',
  73. ].join(' ');
  74. const fields = [
  75. SPAN_OP,
  76. SPAN_GROUP,
  77. SPAN_DESCRIPTION,
  78. SPAN_DOMAIN,
  79. 'sps()',
  80. `sum(${SPAN_SELF_TIME})`,
  81. `p95(${SPAN_SELF_TIME})`,
  82. 'http_error_count()',
  83. ];
  84. if (defined(transaction)) {
  85. fields.push('time_spent_percentage(local)');
  86. } else {
  87. fields.push('time_spent_percentage()');
  88. }
  89. const eventView = EventView.fromNewQueryWithLocation(
  90. {
  91. name: '',
  92. query,
  93. fields,
  94. dataset: DiscoverDatasets.SPANS_METRICS,
  95. version: 2,
  96. },
  97. omit(location, 'span.category', 'http.method')
  98. );
  99. if (sorts) {
  100. eventView.sorts = sorts;
  101. }
  102. return eventView;
  103. }