useSpanSamples.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/useDiscoverSeries';
  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. | 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. query: extraQuery = [],
  49. additionalFields,
  50. } = options;
  51. const location = useLocation();
  52. const query = new MutableSearch([
  53. `${SPAN_GROUP}:${groupId}`,
  54. `transaction:"${transactionName}"`,
  55. ...extraQuery,
  56. ]);
  57. const filters: SpanMetricsQueryFilters = {
  58. transaction: transactionName,
  59. };
  60. if (transactionMethod) {
  61. query.addFilterValue('transaction.method', transactionMethod);
  62. filters['transaction.method'] = transactionMethod;
  63. }
  64. if (release) {
  65. query.addFilterValue('release', release);
  66. filters.release = release;
  67. }
  68. const dateCondtions = getDateConditions(pageFilter.selection);
  69. const {isLoading: isLoadingSeries, data: spanMetricsSeriesData} = useSpanMetricsSeries(
  70. {
  71. search: MutableSearch.fromQueryObject({'span.group': groupId, ...filters}),
  72. yAxis: [`avg(${SPAN_SELF_TIME})`],
  73. enabled: Object.values({'span.group': groupId, ...filters}).every(value =>
  74. Boolean(value)
  75. ),
  76. },
  77. 'api.starfish.sidebar-span-metrics'
  78. );
  79. const maxYValue = computeAxisMax([spanMetricsSeriesData?.[`avg(${SPAN_SELF_TIME})`]]);
  80. const enabled = Boolean(
  81. groupId && transactionName && !isLoadingSeries && pageFilter.isReady
  82. );
  83. const queryString = query.formatString();
  84. const result = useQuery<SpanSample[]>({
  85. queryKey: [
  86. 'span-samples',
  87. groupId,
  88. transactionName,
  89. dateCondtions.statsPeriod,
  90. dateCondtions.start,
  91. dateCondtions.end,
  92. queryString,
  93. additionalFields?.join(','),
  94. ],
  95. queryFn: async () => {
  96. const {data} = await api.requestPromise(
  97. `${url}?${qs.stringify({
  98. ...dateCondtions,
  99. ...{utc: location.query.utc},
  100. lowerBound: 0,
  101. firstBound: maxYValue * (1 / 3),
  102. secondBound: maxYValue * (2 / 3),
  103. upperBound: maxYValue,
  104. project: pageFilter.selection.projects,
  105. query: queryString,
  106. ...(additionalFields?.length ? {additionalFields} : {}),
  107. })}`
  108. );
  109. return data
  110. ?.map((d: SpanSample) => ({
  111. ...d,
  112. timestamp: moment(d.timestamp).format(DATE_FORMAT),
  113. }))
  114. .sort((a: SpanSample, b: SpanSample) => b[SPAN_SELF_TIME] - a[SPAN_SELF_TIME]);
  115. },
  116. refetchOnWindowFocus: false,
  117. enabled,
  118. initialData: [],
  119. });
  120. return {...result, isEnabled: enabled};
  121. };