useSpanSamples.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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. referrer: 'api.starfish.sidebar-span-metrics',
  75. });
  76. const maxYValue = computeAxisMax([spanMetricsSeriesData?.[`avg(${SPAN_SELF_TIME})`]]);
  77. const enabled = Boolean(
  78. groupId && transactionName && !isLoadingSeries && pageFilter.isReady
  79. );
  80. const queryString = query.formatString();
  81. const result = useQuery<SpanSample[]>({
  82. queryKey: [
  83. 'span-samples',
  84. groupId,
  85. transactionName,
  86. dateCondtions.statsPeriod,
  87. dateCondtions.start,
  88. dateCondtions.end,
  89. queryString,
  90. additionalFields?.join(','),
  91. ],
  92. queryFn: async () => {
  93. const {data} = await api.requestPromise(
  94. `${url}?${qs.stringify({
  95. ...dateCondtions,
  96. ...{utc: location.query.utc},
  97. lowerBound: 0,
  98. firstBound: maxYValue * (1 / 3),
  99. secondBound: maxYValue * (2 / 3),
  100. upperBound: maxYValue,
  101. project: pageFilter.selection.projects,
  102. query: queryString,
  103. ...(additionalFields?.length ? {additionalFields} : {}),
  104. })}`
  105. );
  106. return data
  107. ?.map((d: SpanSample) => ({
  108. ...d,
  109. timestamp: moment(d.timestamp).format(DATE_FORMAT),
  110. }))
  111. .sort((a: SpanSample, b: SpanSample) => b[SPAN_SELF_TIME] - a[SPAN_SELF_TIME]);
  112. },
  113. refetchOnWindowFocus: false,
  114. enabled,
  115. initialData: [],
  116. });
  117. return {...result, isEnabled: enabled};
  118. };