buildEventViewQuery.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import type {Location} from 'history';
  2. import {t} from 'sentry/locale';
  3. import {defined} from 'sentry/utils';
  4. import {EMPTY_OPTION_VALUE} from 'sentry/utils/tokenizeSearch';
  5. import {type ModuleName, SpanMetricsField} from 'sentry/views/insights/types';
  6. const NULL_SPAN_CATEGORY = t('custom');
  7. const {SPAN_DESCRIPTION, SPAN_OP, SPAN_DOMAIN, SPAN_ACTION, SPAN_MODULE} =
  8. SpanMetricsField;
  9. const SPAN_FILTER_KEYS = [
  10. SPAN_OP,
  11. SPAN_DOMAIN,
  12. SPAN_ACTION,
  13. `!${SPAN_MODULE}`,
  14. '!span.category',
  15. ];
  16. export function buildEventViewQuery({
  17. moduleName,
  18. location,
  19. transaction,
  20. method,
  21. spanCategory,
  22. }: {
  23. location: Location;
  24. moduleName: ModuleName;
  25. method?: string;
  26. spanCategory?: string;
  27. transaction?: string;
  28. }) {
  29. const {query} = location;
  30. const result = Object.keys(query)
  31. .filter(key => SPAN_FILTER_KEYS.includes(key))
  32. .filter(key => Boolean(query[key]))
  33. .map(key => {
  34. const value = query[key];
  35. const isArray = Array.isArray(value);
  36. if (key === '!span.category' && isArray && value.includes('db')) {
  37. // When omitting database spans, explicitly allow `db.redis` spans, because
  38. // we're not including those spans in the database category
  39. const categoriesAsideFromDatabase = value.filter(v => v !== 'db');
  40. return `(!span.category:db OR ${SPAN_OP}:db.redis) !span.category:[${categoriesAsideFromDatabase.join(
  41. ','
  42. )}]`;
  43. }
  44. if (value === EMPTY_OPTION_VALUE) {
  45. return `!has:${key}`;
  46. }
  47. return `${key}:${isArray ? `[${value}]` : value}`;
  48. });
  49. result.push(`has:${SPAN_DESCRIPTION}`);
  50. result.push(`${SPAN_MODULE}:${moduleName}`);
  51. if (defined(spanCategory)) {
  52. if (spanCategory === NULL_SPAN_CATEGORY) {
  53. result.push(`!has:span.category`);
  54. } else if (spanCategory !== 'Other') {
  55. result.push(`span.category:${spanCategory}`);
  56. }
  57. }
  58. if (transaction) {
  59. result.push(`transaction:${transaction}`);
  60. }
  61. if (method) {
  62. result.push(`transaction.method:${method}`);
  63. }
  64. return result;
  65. }