useFullSpanFromTrace.tsx 1.4 KB

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