useSpanList.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import {SpanMetricsField, SpanMetricsQueryFilters} from 'sentry/views/starfish/types';
  9. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  10. import {EMPTY_OPTION_VALUE} from 'sentry/views/starfish/views/spans/selectors/emptyOption';
  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': Array<string>;
  19. 'span.group': string;
  20. 'span.op': string;
  21. 'spm()': number;
  22. 'sum(span.self_time)': number;
  23. 'time_spent_percentage()': number;
  24. };
  25. export const useSpanList = (
  26. filters: SpanMetricsQueryFilters,
  27. sorts?: Sort[],
  28. limit?: number,
  29. referrer = 'api.starfish.use-span-list',
  30. cursor?: string
  31. ) => {
  32. const location = useLocation();
  33. const eventView = getEventView(filters, location, sorts);
  34. // TODO: Add correct typing. The response should only include the fields
  35. // we're querying for
  36. const {isLoading, data, meta, pageLinks} = useWrappedDiscoverQuery<SpanMetrics[]>({
  37. eventView,
  38. initialData: [],
  39. limit,
  40. referrer,
  41. cursor,
  42. });
  43. return {isLoading, data, meta, pageLinks};
  44. };
  45. function getEventView(
  46. filters: SpanMetricsQueryFilters,
  47. location: Location,
  48. sorts?: Sort[]
  49. ) {
  50. const query = new MutableSearch('');
  51. Object.entries(filters).forEach(([key, value]) => {
  52. if (!value) {
  53. return;
  54. }
  55. if (value === EMPTY_OPTION_VALUE) {
  56. query.addFilterValue('!has', key);
  57. }
  58. query.addFilterValue(key, value, !ALLOWED_WILDCARD_FIELDS.includes(key));
  59. });
  60. query.addFilterValue('has', 'span.description');
  61. const fields = [
  62. PROJECT_ID,
  63. SPAN_OP,
  64. SPAN_GROUP,
  65. SPAN_DESCRIPTION,
  66. SPAN_DOMAIN,
  67. 'spm()',
  68. `sum(${SPAN_SELF_TIME})`,
  69. `avg(${SPAN_SELF_TIME})`,
  70. 'http_error_count()',
  71. 'time_spent_percentage()',
  72. ];
  73. const eventView = EventView.fromNewQueryWithLocation(
  74. {
  75. name: '',
  76. query: query.formatString(),
  77. fields,
  78. dataset: DiscoverDatasets.SPANS_METRICS,
  79. version: 2,
  80. },
  81. omit(location, 'span.category', 'http.method')
  82. );
  83. if (sorts) {
  84. eventView.sorts = sorts;
  85. }
  86. return eventView;
  87. }
  88. const ALLOWED_WILDCARD_FIELDS = ['span.description'];