useFullSpanFromTrace.tsx 1.6 KB

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