traceFullQuery.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {Fragment} from 'react';
  2. import GenericDiscoverQuery, {
  3. DiscoverQueryProps,
  4. } from 'sentry/utils/discover/genericDiscoverQuery';
  5. import {
  6. BaseTraceChildrenProps,
  7. FullQuickTrace,
  8. TraceFull,
  9. TraceFullDetailed,
  10. TraceRequestProps,
  11. } from 'sentry/utils/performance/quickTrace/types';
  12. import {
  13. getTraceRequestPayload,
  14. makeEventView,
  15. } from 'sentry/utils/performance/quickTrace/utils';
  16. type AdditionalQueryProps = {
  17. detailed?: boolean;
  18. eventId?: string;
  19. };
  20. type TraceFullQueryChildrenProps<T> = BaseTraceChildrenProps &
  21. Omit<FullQuickTrace, 'trace'> & {
  22. /**
  23. * The `event-trace` endpoint returns a full trace with the parent-child
  24. * relationships. It can be flattened into a `QuickTraceEvent` if necessary.
  25. */
  26. traces: T | null;
  27. };
  28. type QueryProps<T> = Omit<TraceRequestProps, 'eventView'> &
  29. AdditionalQueryProps & {
  30. children: (props: TraceFullQueryChildrenProps<T>) => React.ReactNode;
  31. };
  32. function getTraceFullRequestPayload({
  33. detailed,
  34. eventId,
  35. ...props
  36. }: DiscoverQueryProps & AdditionalQueryProps) {
  37. const additionalApiPayload: any = getTraceRequestPayload(props);
  38. additionalApiPayload.detailed = detailed ? '1' : '0';
  39. if (eventId) {
  40. additionalApiPayload.event_id = eventId;
  41. }
  42. return additionalApiPayload;
  43. }
  44. function EmptyTrace<T>({children}: Pick<QueryProps<T>, 'children'>) {
  45. return (
  46. <Fragment>
  47. {children({
  48. isLoading: false,
  49. error: null,
  50. traces: null,
  51. type: 'full',
  52. })}
  53. </Fragment>
  54. );
  55. }
  56. function GenericTraceFullQuery<T>({
  57. traceId,
  58. start,
  59. end,
  60. statsPeriod,
  61. children,
  62. ...props
  63. }: QueryProps<T>) {
  64. if (!traceId) {
  65. return <EmptyTrace<T>>{children}</EmptyTrace>;
  66. }
  67. const eventView = makeEventView({start, end, statsPeriod});
  68. return (
  69. <GenericDiscoverQuery<T, AdditionalQueryProps>
  70. route={`events-trace/${traceId}`}
  71. getRequestPayload={getTraceFullRequestPayload}
  72. eventView={eventView}
  73. {...props}
  74. >
  75. {({tableData, ...rest}) =>
  76. children({
  77. // This is using '||` instead of '??` here because
  78. // the client returns a empty string when the response
  79. // is 204. And we want the empty string, undefined and
  80. // null to be converted to null.
  81. traces: tableData || null,
  82. type: 'full',
  83. ...rest,
  84. })
  85. }
  86. </GenericDiscoverQuery>
  87. );
  88. }
  89. export const TraceFullQuery = (props: Omit<QueryProps<TraceFull[]>, 'detailed'>) => (
  90. <GenericTraceFullQuery<TraceFull[]> {...props} detailed={false} />
  91. );
  92. export const TraceFullDetailedQuery = (
  93. props: Omit<QueryProps<TraceFullDetailed[]>, 'detailed'>
  94. ) => <GenericTraceFullQuery<TraceFullDetailed[]> {...props} detailed />;