useFullSpanFromTrace.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import type {Entry, EntrySpans} from 'sentry/types';
  2. import {EntryType} from 'sentry/types';
  3. import type {Sort} from 'sentry/utils/discover/fields';
  4. import {useEventDetails} from 'sentry/views/starfish/queries/useEventDetails';
  5. import {useIndexedSpans} from 'sentry/views/starfish/queries/useIndexedSpans';
  6. import {SpanIndexedField} from 'sentry/views/starfish/types';
  7. // NOTE: Fetching the top one is a bit naive, but works for now. A better
  8. // approach might be to fetch several at a time, and let the hook consumer
  9. // decide how to display them
  10. export function useFullSpanFromTrace(
  11. group?: string,
  12. sorts?: Sort[],
  13. enabled: boolean = true,
  14. extraFilters: Record<string, string> = {}
  15. ) {
  16. const filters = {...extraFilters};
  17. if (group) {
  18. filters[SpanIndexedField.SPAN_GROUP] = group;
  19. }
  20. const indexedSpansResponse = useIndexedSpans(filters, sorts, 1, enabled);
  21. const firstIndexedSpan = indexedSpansResponse.data?.[0];
  22. const eventDetailsResponse = useEventDetails({
  23. eventId: firstIndexedSpan?.[SpanIndexedField.TRANSACTION_ID],
  24. projectSlug: firstIndexedSpan?.[SpanIndexedField.PROJECT],
  25. });
  26. const spanEntry = eventDetailsResponse.data?.entries.find(
  27. (entry: Entry): entry is EntrySpans => {
  28. return entry.type === EntryType.SPANS;
  29. }
  30. );
  31. const fullSpan = spanEntry?.data?.find(
  32. span => span.span_id === firstIndexedSpan?.[SpanIndexedField.ID]
  33. );
  34. // N.B. There isn't a great pattern for us to merge the responses together,
  35. // so we're only merging the three most important properties
  36. return {
  37. isLoading: indexedSpansResponse.isLoading || eventDetailsResponse.isLoading,
  38. isFetching: indexedSpansResponse.isFetching || eventDetailsResponse.isFetching,
  39. isError: indexedSpansResponse.isError || eventDetailsResponse.isError,
  40. data: fullSpan,
  41. };
  42. }