useSpanList.tsx 2.4 KB

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