traceTokenConverter.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // @TODO Add issue keys
  84. const TEXT_KEYS = new Set([
  85. ...withPrefixedPermutation('transaction', TRANSACTION_TEXT_KEYS),
  86. ...withPrefixedPermutation('span', SPAN_TEXT_KEYS),
  87. ]);
  88. const NUMERIC_KEYS = new Set([
  89. ...withPrefixedPermutation('transaction', TRANSACTION_NUMERIC_KEYS),
  90. ...withPrefixedPermutation('span', SPAN_NUMERIC_KEYS),
  91. ]);
  92. const DURATION_KEYS = new Set([
  93. ...withPrefixedPermutation('transaction', TRANSACTION_DURATION_KEYS),
  94. ...withPrefixedPermutation('transaction', TRANSACTION_DURATION_SYNTHETIC_KEYS),
  95. ...withPrefixedPermutation('span', SPAN_DURATION_KEYS),
  96. ...withPrefixedPermutation('span', SPAN_DURATION_SYNTHETIC_KEYS),
  97. ]);
  98. const DATE_KEYS = new Set([
  99. ...withPrefixedPermutation('transaction', TRANSACTION_DATE_KEYS),
  100. ...withPrefixedPermutation('span', SPAN_DATE_KEYS),
  101. ]);
  102. const BOOLEAN_KEYS = new Set([
  103. ...withPrefixedPermutation('transaction', TRANSACTION_BOOLEAN_KEYS),
  104. ...withPrefixedPermutation('span', SPAN_BOOLEAN_KEYS),
  105. ]);
  106. export const TRACE_SEARCH_CONFIG: SearchConfig = {
  107. ...defaultConfig,
  108. textOperatorKeys: TEXT_KEYS,
  109. durationKeys: DURATION_KEYS,
  110. percentageKeys: new Set(),
  111. numericKeys: NUMERIC_KEYS,
  112. dateKeys: DATE_KEYS,
  113. booleanKeys: BOOLEAN_KEYS,
  114. };
  115. export function parseTraceSearch(query: string) {
  116. return parseSearch(query, {...TRACE_SEARCH_CONFIG, parse: true});
  117. }