buildEventViewQuery.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 {ModuleName, SpanMetricsField} from 'sentry/views/starfish/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. if (moduleName !== ModuleName.ALL) {
  51. result.push(`${SPAN_MODULE}:${moduleName}`);
  52. }
  53. if (defined(spanCategory)) {
  54. if (spanCategory === NULL_SPAN_CATEGORY) {
  55. result.push(`!has:span.category`);
  56. } else if (spanCategory !== 'Other') {
  57. result.push(`span.category:${spanCategory}`);
  58. }
  59. }
  60. if (transaction) {
  61. result.push(`transaction:${transaction}`);
  62. }
  63. if (method) {
  64. result.push(`transaction.method:${method}`);
  65. }
  66. return result;
  67. }