useSpanSamples.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. | 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. search: MutableSearch.fromQueryObject({'span.group': groupId, ...filters}),
  71. yAxis: [`avg(${SPAN_SELF_TIME})`],
  72. enabled: Object.values({'span.group': groupId, ...filters}).every(value =>
  73. Boolean(value)
  74. ),
  75. referrer: '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. };