useSpanTransactionMetrics.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import type {Location} from 'history';
  2. import {defined} from 'sentry/utils';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import type {Sort} from 'sentry/utils/discover/fields';
  5. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  6. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import type {MetricsFilters} from 'sentry/views/starfish/types';
  9. import {SpanMetricsField} from 'sentry/views/starfish/types';
  10. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  11. const {SPAN_SELF_TIME} = SpanMetricsField;
  12. export type SpanTransactionMetrics = {
  13. 'avg(http.response_content_length)': number;
  14. 'avg(span.self_time)': number;
  15. 'http_error_count()': number;
  16. 'resource.render_blocking_status': '' | 'non-blocking' | 'blocking';
  17. 'spm()': number;
  18. 'sum(span.self_time)': number;
  19. 'time_spent_percentage()': number;
  20. transaction: string;
  21. 'transaction.method': string;
  22. };
  23. export const useSpanTransactionMetrics = (
  24. filters: MetricsFilters,
  25. sorts?: Sort[],
  26. cursor?: string,
  27. extraFields?: string[],
  28. enabled: boolean = true,
  29. referrer = 'api.starfish.span-transaction-metrics'
  30. ) => {
  31. const location = useLocation();
  32. const eventView = getEventView(location, filters, sorts, extraFields);
  33. return useWrappedDiscoverQuery<SpanTransactionMetrics[]>({
  34. eventView,
  35. initialData: [],
  36. enabled,
  37. limit: 25,
  38. referrer,
  39. cursor,
  40. });
  41. };
  42. function getEventView(
  43. location: Location,
  44. filters: MetricsFilters = {},
  45. sorts?: Sort[],
  46. extraFields = [] as string[]
  47. ) {
  48. const search = new MutableSearch('');
  49. Object.entries(filters).forEach(([key, value]) => {
  50. if (!defined(value)) {
  51. return;
  52. }
  53. if (Array.isArray(value)) {
  54. search.addFilterValues(key, value);
  55. } else {
  56. search.addFilterValue(key, value);
  57. }
  58. });
  59. const eventView = EventView.fromNewQueryWithLocation(
  60. {
  61. name: '',
  62. query: search.formatString(),
  63. fields: [
  64. 'transaction',
  65. 'transaction.method',
  66. 'spm()',
  67. `sum(${SPAN_SELF_TIME})`,
  68. `avg(${SPAN_SELF_TIME})`,
  69. 'time_spent_percentage()',
  70. 'http_error_count()',
  71. ...extraFields,
  72. ],
  73. orderby: '-time_spent_percentage',
  74. dataset: DiscoverDatasets.SPANS_METRICS,
  75. version: 2,
  76. },
  77. location
  78. );
  79. if (sorts) {
  80. eventView.sorts = sorts;
  81. }
  82. return eventView;
  83. }