useExploreSpansTable.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {useMemo} from 'react';
  2. import type {NewQuery} from 'sentry/types/organization';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import {
  7. useExploreDataset,
  8. useExploreFields,
  9. useExploreSortBys,
  10. } from 'sentry/views/explore/contexts/pageParamsContext';
  11. import {useSpansQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
  12. interface UseExploreSpansTableOptions {
  13. enabled: boolean;
  14. limit: number;
  15. query: string;
  16. }
  17. export interface SpansTableResult {
  18. eventView: EventView;
  19. result: ReturnType<typeof useSpansQuery<any[]>>;
  20. }
  21. export function useExploreSpansTable({
  22. enabled,
  23. limit,
  24. query,
  25. }: UseExploreSpansTableOptions): SpansTableResult {
  26. const {selection} = usePageFilters();
  27. const dataset = useExploreDataset();
  28. const fields = useExploreFields();
  29. const sortBys = useExploreSortBys();
  30. const visibleFields = useMemo(
  31. () => (fields.includes('id') ? fields : ['id', ...fields]),
  32. [fields]
  33. );
  34. const eventView = useMemo(() => {
  35. const queryFields = [
  36. ...visibleFields,
  37. 'project',
  38. 'trace',
  39. 'transaction.span_id',
  40. 'id',
  41. 'timestamp',
  42. ];
  43. const search = new MutableSearch(query);
  44. // Filtering out all spans with op like 'ui.interaction*' which aren't
  45. // embedded under transactions. The trace view does not support rendering
  46. // such spans yet.
  47. search.addFilterValues('!transaction.span_id', ['00']);
  48. const discoverQuery: NewQuery = {
  49. id: undefined,
  50. name: 'Explore - Span Samples',
  51. fields: queryFields,
  52. orderby: sortBys.map(sort => `${sort.kind === 'desc' ? '-' : ''}${sort.field}`),
  53. query: search.formatString(),
  54. version: 2,
  55. dataset,
  56. };
  57. return EventView.fromNewQueryWithPageFilters(discoverQuery, selection);
  58. }, [dataset, sortBys, query, selection, visibleFields]);
  59. const result = useSpansQuery({
  60. enabled,
  61. eventView,
  62. initialData: [],
  63. limit,
  64. referrer: 'api.explore.spans-samples-table',
  65. allowAggregateConditions: false,
  66. trackResponseAnalytics: false,
  67. });
  68. return useMemo(() => {
  69. return {eventView, result};
  70. }, [eventView, result]);
  71. }