useSpanTransactionMetrics.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 'resource.render_blocking_status': '' | 'non-blocking' | 'blocking';
  16. 'spm()': number;
  17. 'sum(span.self_time)': number;
  18. 'time_spent_percentage()': number;
  19. transaction: string;
  20. 'transaction.method': 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. 'http_error_count()',
  70. ...extraFields,
  71. ],
  72. orderby: '-time_spent_percentage',
  73. dataset: DiscoverDatasets.SPANS_METRICS,
  74. version: 2,
  75. },
  76. location
  77. );
  78. if (sorts) {
  79. eventView.sorts = sorts;
  80. }
  81. return eventView;
  82. }