useSpanList.tsx 2.5 KB

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