useSpanList.tsx 2.6 KB

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