useSpanSamples.tsx 4.1 KB

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