useSpanTransactionMetrics.tsx 2.3 KB

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