mri.spec.tsx 5.3 KB

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