useSpanList.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, SpanMetricsField} 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 {
  12. SPAN_SELF_TIME,
  13. SPAN_DESCRIPTION,
  14. SPAN_GROUP,
  15. SPAN_OP,
  16. SPAN_DOMAIN,
  17. SPAN_DOMAIN_ARRAY,
  18. PROJECT_ID,
  19. } = SpanMetricsField;
  20. export type SpanMetrics = {
  21. 'avg(span.self_time)': number;
  22. 'http_error_count()': number;
  23. 'project.id': number;
  24. 'span.description': string;
  25. 'span.domain': string;
  26. 'span.domain_array': Array<string>;
  27. 'span.group': string;
  28. 'span.op': string;
  29. 'spm()': number;
  30. 'sum(span.self_time)': number;
  31. 'time_spent_percentage()': number;
  32. 'time_spent_percentage(local)': number;
  33. };
  34. export const useSpanList = (
  35. moduleName: ModuleName,
  36. transaction?: string,
  37. method?: string,
  38. spanCategory?: string,
  39. sorts?: Sort[],
  40. limit?: number,
  41. referrer = 'api.starfish.use-span-list',
  42. cursor?: string
  43. ) => {
  44. const location = useLocation();
  45. const eventView = getEventView(
  46. moduleName,
  47. location,
  48. transaction,
  49. method,
  50. spanCategory,
  51. sorts
  52. );
  53. const {isLoading, data, meta, pageLinks} = useWrappedDiscoverQuery<SpanMetrics[]>({
  54. eventView,
  55. initialData: [],
  56. limit,
  57. referrer,
  58. cursor,
  59. });
  60. return {isLoading, data, meta, pageLinks};
  61. };
  62. function getEventView(
  63. moduleName: ModuleName,
  64. location: Location,
  65. transaction?: string,
  66. method?: string,
  67. spanCategory?: string,
  68. sorts?: Sort[]
  69. ) {
  70. const query = [
  71. ...buildEventViewQuery({
  72. moduleName,
  73. location,
  74. transaction,
  75. method,
  76. spanCategory,
  77. }),
  78. 'transaction.op:http.server',
  79. ].join(' ');
  80. const fields = [
  81. PROJECT_ID,
  82. SPAN_OP,
  83. SPAN_GROUP,
  84. SPAN_DESCRIPTION,
  85. SPAN_DOMAIN,
  86. SPAN_DOMAIN_ARRAY,
  87. 'spm()',
  88. `sum(${SPAN_SELF_TIME})`,
  89. `avg(${SPAN_SELF_TIME})`,
  90. 'http_error_count()',
  91. ];
  92. if (defined(transaction)) {
  93. fields.push('time_spent_percentage(local)');
  94. } else {
  95. fields.push('time_spent_percentage()');
  96. }
  97. const eventView = EventView.fromNewQueryWithLocation(
  98. {
  99. name: '',
  100. query,
  101. fields,
  102. dataset: DiscoverDatasets.SPANS_METRICS,
  103. version: 2,
  104. },
  105. omit(location, 'span.category', 'http.method')
  106. );
  107. if (sorts) {
  108. eventView.sorts = sorts;
  109. }
  110. return eventView;
  111. }