buildEventViewQuery.tsx 2.1 KB

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