useFullSpanFromTrace.tsx 1.7 KB

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