useSpanSamples.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import moment from 'moment';
  2. import * as qs from 'query-string';
  3. import {useQuery} from 'sentry/utils/queryClient';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import useApi from 'sentry/utils/useApi';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {computeAxisMax} from 'sentry/views/starfish/components/chart';
  10. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  11. import type {
  12. SpanIndexedFieldTypes,
  13. SpanMetricsQueryFilters,
  14. } from 'sentry/views/starfish/types';
  15. import {SpanIndexedField} from 'sentry/views/starfish/types';
  16. import {getDateConditions} from 'sentry/views/starfish/utils/getDateConditions';
  17. import {DATE_FORMAT} from 'sentry/views/starfish/utils/useSpansQuery';
  18. const {SPAN_SELF_TIME, SPAN_GROUP} = SpanIndexedField;
  19. type Options = {
  20. groupId: string;
  21. transactionName: string;
  22. additionalFields?: string[];
  23. query?: string[];
  24. release?: string;
  25. transactionMethod?: string;
  26. };
  27. export type SpanSample = Pick<
  28. SpanIndexedFieldTypes,
  29. | SpanIndexedField.SPAN_SELF_TIME
  30. | SpanIndexedField.TRANSACTION_ID
  31. | SpanIndexedField.PROJECT
  32. | SpanIndexedField.TIMESTAMP
  33. | SpanIndexedField.ID
  34. | SpanIndexedField.PROFILE_ID
  35. | SpanIndexedField.HTTP_RESPONSE_CONTENT_LENGTH
  36. >;
  37. export const useSpanSamples = (options: Options) => {
  38. const organization = useOrganization();
  39. const url = `/api/0/organizations/${organization.slug}/spans-samples/`;
  40. const api = useApi();
  41. const pageFilter = usePageFilters();
  42. const {
  43. groupId,
  44. transactionName,
  45. transactionMethod,
  46. release,
  47. query: extraQuery = [],
  48. additionalFields,
  49. } = options;
  50. const location = useLocation();
  51. const query = new MutableSearch([
  52. `${SPAN_GROUP}:${groupId}`,
  53. `transaction:"${transactionName}"`,
  54. ...extraQuery,
  55. ]);
  56. const filters: SpanMetricsQueryFilters = {
  57. transaction: transactionName,
  58. };
  59. if (transactionMethod) {
  60. query.addFilterValue('transaction.method', transactionMethod);
  61. filters['transaction.method'] = transactionMethod;
  62. }
  63. if (release) {
  64. query.addFilterValue('release', release);
  65. filters.release = release;
  66. }
  67. const dateCondtions = getDateConditions(pageFilter.selection);
  68. const {isLoading: isLoadingSeries, data: spanMetricsSeriesData} = useSpanMetricsSeries({
  69. filters: {'span.group': groupId, ...filters},
  70. yAxis: [`avg(${SPAN_SELF_TIME})`],
  71. referrer: 'api.starfish.sidebar-span-metrics',
  72. });
  73. const maxYValue = computeAxisMax([spanMetricsSeriesData?.[`avg(${SPAN_SELF_TIME})`]]);
  74. const enabled = Boolean(
  75. groupId && transactionName && !isLoadingSeries && pageFilter.isReady
  76. );
  77. const queryString = query.formatString();
  78. const result = useQuery<SpanSample[]>({
  79. queryKey: [
  80. 'span-samples',
  81. groupId,
  82. transactionName,
  83. dateCondtions.statsPeriod,
  84. dateCondtions.start,
  85. dateCondtions.end,
  86. queryString,
  87. additionalFields?.join(','),
  88. ],
  89. queryFn: async () => {
  90. const {data} = await api.requestPromise(
  91. `${url}?${qs.stringify({
  92. ...dateCondtions,
  93. ...{utc: location.query.utc},
  94. lowerBound: 0,
  95. firstBound: maxYValue * (1 / 3),
  96. secondBound: maxYValue * (2 / 3),
  97. upperBound: maxYValue,
  98. project: pageFilter.selection.projects,
  99. query: queryString,
  100. ...(additionalFields?.length ? {additionalFields} : {}),
  101. })}`
  102. );
  103. return data
  104. ?.map((d: SpanSample) => ({
  105. ...d,
  106. timestamp: moment(d.timestamp).format(DATE_FORMAT),
  107. }))
  108. .sort((a: SpanSample, b: SpanSample) => b[SPAN_SELF_TIME] - a[SPAN_SELF_TIME]);
  109. },
  110. refetchOnWindowFocus: false,
  111. enabled,
  112. initialData: [],
  113. });
  114. return {...result, isEnabled: enabled};
  115. };