traceTokenConverter.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {
  2. defaultConfig,
  3. parseSearch,
  4. type SearchConfig,
  5. } from 'sentry/components/searchSyntax/parser';
  6. import type {TraceTree} from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  7. // Span keys
  8. type TransactionPrefix = 'Transaction';
  9. // The keys can be prefixed by the entity type they belong to, this ensures that
  10. // conflicting keys on different entities are resolved to the correct entity.
  11. type TransactionKey =
  12. | `${TransactionPrefix}.${keyof TraceTree.Transaction}`
  13. | keyof TraceTree.Transaction;
  14. // Transaction keys
  15. const TRANSACTION_TEXT_KEYS: TransactionKey[] = [
  16. 'event_id',
  17. 'project_slug',
  18. 'parent_event_id',
  19. 'parent_span_id',
  20. 'span_id',
  21. 'transaction',
  22. 'transaction.op',
  23. 'transaction.status',
  24. ];
  25. const TRANSACTION_NUMERIC_KEYS: TransactionKey[] = [
  26. 'project_id',
  27. 'start_timestamp',
  28. 'timestamp',
  29. ];
  30. const TRANSACTION_DURATION_KEYS: TransactionKey[] = ['transaction.duration'];
  31. const TRANSACTION_DURATION_SYNTHETIC_KEYS: TransactionKey[] = [
  32. // @ts-expect-error
  33. 'duration',
  34. // @ts-expect-error
  35. 'total_time',
  36. ];
  37. // @TODO the current date parsing does not support timestamps, so we
  38. // exclude these keys for now and parse them as numeric keys
  39. const TRANSACTION_DATE_KEYS: TransactionKey[] = [
  40. // 'start_timestamp',
  41. // 'timestamp',
  42. ];
  43. const TRANSACTION_BOOLEAN_KEYS: TransactionKey[] = [];
  44. // Span keys
  45. type SpanPrefix = 'span';
  46. // The keys can be prefixed by the entity type they belong to, this ensures that
  47. // conflicting keys on different entities are resolved to the correct entity.
  48. type SpanKey = `${SpanPrefix}.${keyof TraceTree.Span}` | keyof TraceTree.Span;
  49. const SPAN_TEXT_KEYS: SpanKey[] = [
  50. 'hash',
  51. 'description',
  52. 'op',
  53. 'origin',
  54. 'parent_span_id',
  55. 'span_id',
  56. 'trace_id',
  57. 'status',
  58. ];
  59. const SPAN_NUMERIC_KEYS: SpanKey[] = ['timestamp', 'start_timestamp'];
  60. const SPAN_DURATION_KEYS: SpanKey[] = ['exclusive_time'];
  61. // The keys below are not real keys returned by the API, but are instead
  62. // mapped by the frontend to the correct keys for convenience and UX reasons
  63. const SPAN_DURATION_SYNTHETIC_KEYS: SpanKey[] = [
  64. // @ts-expect-error
  65. 'duration',
  66. // @ts-expect-error
  67. 'total_time',
  68. // @ts-expect-error
  69. 'self_time',
  70. ];
  71. // @TODO the current date parsing does not support timestamps, so we
  72. // exclude these keys for now and parse them as numeric keys
  73. const SPAN_DATE_KEYS: SpanKey[] = [
  74. // 'timestamp', 'start_timestamp'
  75. ];
  76. const SPAN_BOOLEAN_KEYS: SpanKey[] = ['same_process_as_parent'];
  77. function withPrefixedPermutation(
  78. prefix: 'span' | 'transaction',
  79. keys: string[]
  80. ): string[] {
  81. return [...keys, ...keys.map(key => `${prefix}.${key}`)];
  82. }
  83. // Keys that do not belong to a particular entity, and can be inferred from the context
  84. const SYNTHETIC_KEYS = new Set(['has']);
  85. // @TODO Add issue keys
  86. const TEXT_KEYS = new Set([
  87. ...SYNTHETIC_KEYS,
  88. ...withPrefixedPermutation('transaction', TRANSACTION_TEXT_KEYS),
  89. ...withPrefixedPermutation('span', SPAN_TEXT_KEYS),
  90. ]);
  91. const NUMERIC_KEYS = new Set([
  92. ...withPrefixedPermutation('transaction', TRANSACTION_NUMERIC_KEYS),
  93. ...withPrefixedPermutation('span', SPAN_NUMERIC_KEYS),
  94. ]);
  95. const DURATION_KEYS = new Set([
  96. ...withPrefixedPermutation('transaction', TRANSACTION_DURATION_KEYS),
  97. ...withPrefixedPermutation('transaction', TRANSACTION_DURATION_SYNTHETIC_KEYS),
  98. ...withPrefixedPermutation('span', SPAN_DURATION_KEYS),
  99. ...withPrefixedPermutation('span', SPAN_DURATION_SYNTHETIC_KEYS),
  100. ]);
  101. const DATE_KEYS = new Set([
  102. ...withPrefixedPermutation('transaction', TRANSACTION_DATE_KEYS),
  103. ...withPrefixedPermutation('span', SPAN_DATE_KEYS),
  104. ]);
  105. const BOOLEAN_KEYS = new Set([
  106. ...withPrefixedPermutation('transaction', TRANSACTION_BOOLEAN_KEYS),
  107. ...withPrefixedPermutation('span', SPAN_BOOLEAN_KEYS),
  108. ]);
  109. export const TRACE_SEARCH_CONFIG: SearchConfig = {
  110. ...defaultConfig,
  111. textOperatorKeys: TEXT_KEYS,
  112. durationKeys: DURATION_KEYS,
  113. percentageKeys: new Set(),
  114. numericKeys: NUMERIC_KEYS,
  115. dateKeys: DATE_KEYS,
  116. booleanKeys: BOOLEAN_KEYS,
  117. };
  118. export function parseTraceSearch(query: string) {
  119. return parseSearch(query, {...TRACE_SEARCH_CONFIG, parse: true});
  120. }