mri.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {MetricType, MRI} from 'sentry/types';
  2. import {ParsedMRI, UseCase} from 'sentry/types/metrics';
  3. import {getUseCaseFromMRI, parseField, parseMRI, toMRI} from 'sentry/utils/metrics/mri';
  4. describe('parseMRI', () => {
  5. it('should handle falsy values', () => {
  6. expect(parseMRI('')).toEqual(null);
  7. expect(parseMRI()).toEqual(null);
  8. expect(parseMRI(null)).toEqual(null);
  9. expect(parseMRI(undefined)).toEqual(null);
  10. });
  11. it.each(['c', 'd', 'e', 'g', 's'])(
  12. 'should correctly parse a valid MRI string - metric type %s',
  13. metricType => {
  14. const mri: MRI = `${metricType as MetricType}:custom/xyz@test`;
  15. const parsedMRI = {
  16. type: metricType,
  17. name: 'xyz',
  18. unit: 'test',
  19. useCase: 'custom',
  20. };
  21. expect(parseMRI(mri)).toEqual(parsedMRI);
  22. }
  23. );
  24. it.each(['sessions', 'transactions', 'custom'])(
  25. 'should correctly parse a valid MRI string - use case %s',
  26. useCase => {
  27. const mri: MRI = `c:${useCase as UseCase}/xyz@test`;
  28. const parsedMRI = {
  29. type: 'c',
  30. name: 'xyz',
  31. unit: 'test',
  32. useCase,
  33. };
  34. expect(parseMRI(mri)).toEqual(parsedMRI);
  35. }
  36. );
  37. it.each(['foo', 'foo_bar', 'foo_9-bar', '12-!foo][]312bar'])(
  38. 'should correctly parse a valid MRI string - name %s',
  39. name => {
  40. const mri: MRI = `c:custom/${name}@test`;
  41. const parsedMRI = {
  42. type: 'c',
  43. name,
  44. unit: 'test',
  45. useCase: 'custom',
  46. };
  47. expect(parseMRI(mri)).toEqual(parsedMRI);
  48. }
  49. );
  50. it.each(['ms', 'none', 'KiB'])(
  51. 'should correctly parse a valid MRI string - name %s',
  52. unit => {
  53. const mri: MRI = `c:custom/foo@${unit}`;
  54. const parsedMRI = {
  55. type: 'c',
  56. name: 'foo',
  57. unit,
  58. useCase: 'custom',
  59. };
  60. expect(parseMRI(mri)).toEqual(parsedMRI);
  61. }
  62. );
  63. });
  64. describe('getUseCaseFromMRI', () => {
  65. it('should return "custom" for mri containing "custom/"', () => {
  66. const mri = 'd:custom/sentry.events.symbolicator.query_task@second';
  67. const result = getUseCaseFromMRI(mri);
  68. expect(result).toBe('custom');
  69. });
  70. it('should return "transactions" for mri containing "transactions/"', () => {
  71. const mri = 'd:transactions/duration@second';
  72. const result = getUseCaseFromMRI(mri);
  73. expect(result).toBe('transactions');
  74. });
  75. it('should return undefined for invalid mris', () => {
  76. const mri = 'foobar';
  77. const result = getUseCaseFromMRI(mri);
  78. expect(result).toBeUndefined();
  79. });
  80. });
  81. describe('parseField', () => {
  82. it('should return the correct mri and op from field', () => {
  83. const field = 'op(c:test/project)';
  84. const result = parseField(field);
  85. expect(result).toEqual({
  86. mri: 'c:test/project',
  87. op: 'op',
  88. });
  89. });
  90. it('should do nothing for already formatted field', () => {
  91. const field = 'sum(my-metric)';
  92. const result = parseField(field);
  93. expect(result?.mri).toBe('my-metric');
  94. expect(result?.op).toBe('sum');
  95. });
  96. it('should return null mri invalid field', () => {
  97. const field = 'invalid-field';
  98. const result = parseField(field);
  99. expect(result).toBeNull();
  100. });
  101. });
  102. describe('toMRI', () => {
  103. it.each(['c', 'd', 'e', 'g', 's'])(
  104. 'should correctly parse a valid MRI string - metric type %s',
  105. metricType => {
  106. const mri = `${metricType as MetricType}:custom/xyz@test`;
  107. const parsedMRI: ParsedMRI = {
  108. type: metricType as MetricType,
  109. name: 'xyz',
  110. unit: 'test',
  111. useCase: 'custom',
  112. };
  113. expect(toMRI(parsedMRI)).toEqual(mri);
  114. }
  115. );
  116. it.each(['sessions', 'transactions', 'custom'])(
  117. 'should correctly parse a valid MRI string - use case %s',
  118. useCase => {
  119. const mri: MRI = `c:${useCase as UseCase}/xyz@test`;
  120. const parsedMRI: ParsedMRI = {
  121. type: 'c',
  122. name: 'xyz',
  123. unit: 'test',
  124. useCase: useCase as UseCase,
  125. };
  126. expect(toMRI(parsedMRI)).toEqual(mri);
  127. }
  128. );
  129. it.each(['foo', 'foo_bar', 'foo_9-bar', '12-!foo][]312bar'])(
  130. 'should correctly parse a valid MRI string - name %s',
  131. name => {
  132. const mri: MRI = `c:custom/${name}@test`;
  133. const parsedMRI: ParsedMRI = {
  134. type: 'c',
  135. name,
  136. unit: 'test',
  137. useCase: 'custom',
  138. };
  139. expect(toMRI(parsedMRI)).toEqual(mri);
  140. }
  141. );
  142. it.each(['ms', 'none', 'KiB'])(
  143. 'should correctly parse a valid MRI string - name %s',
  144. unit => {
  145. const mri: MRI = `c:custom/foo@${unit}`;
  146. const parsedMRI: ParsedMRI = {
  147. type: 'c',
  148. name: 'foo',
  149. unit,
  150. useCase: 'custom',
  151. };
  152. expect(toMRI(parsedMRI)).toEqual(mri);
  153. }
  154. );
  155. });