logs_model.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import {
  2. calculateFieldStats,
  3. calculateLogsLabelStats,
  4. dedupLogRows,
  5. getParser,
  6. LogsDedupStrategy,
  7. LogsModel,
  8. LogsParsers,
  9. } from '../logs_model';
  10. describe('dedupLogRows()', () => {
  11. test('should return rows as is when dedup is set to none', () => {
  12. const logs = {
  13. rows: [
  14. {
  15. entry: 'WARN test 1.23 on [xxx]',
  16. },
  17. {
  18. entry: 'WARN test 1.23 on [xxx]',
  19. },
  20. ],
  21. };
  22. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.none).rows).toMatchObject(logs.rows);
  23. });
  24. test('should dedup on exact matches', () => {
  25. const logs = {
  26. rows: [
  27. {
  28. entry: 'WARN test 1.23 on [xxx]',
  29. },
  30. {
  31. entry: 'WARN test 1.23 on [xxx]',
  32. },
  33. {
  34. entry: 'INFO test 2.44 on [xxx]',
  35. },
  36. {
  37. entry: 'WARN test 1.23 on [xxx]',
  38. },
  39. ],
  40. };
  41. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.exact).rows).toEqual([
  42. {
  43. duplicates: 1,
  44. entry: 'WARN test 1.23 on [xxx]',
  45. },
  46. {
  47. duplicates: 0,
  48. entry: 'INFO test 2.44 on [xxx]',
  49. },
  50. {
  51. duplicates: 0,
  52. entry: 'WARN test 1.23 on [xxx]',
  53. },
  54. ]);
  55. });
  56. test('should dedup on number matches', () => {
  57. const logs = {
  58. rows: [
  59. {
  60. entry: 'WARN test 1.2323423 on [xxx]',
  61. },
  62. {
  63. entry: 'WARN test 1.23 on [xxx]',
  64. },
  65. {
  66. entry: 'INFO test 2.44 on [xxx]',
  67. },
  68. {
  69. entry: 'WARN test 1.23 on [xxx]',
  70. },
  71. ],
  72. };
  73. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.numbers).rows).toEqual([
  74. {
  75. duplicates: 1,
  76. entry: 'WARN test 1.2323423 on [xxx]',
  77. },
  78. {
  79. duplicates: 0,
  80. entry: 'INFO test 2.44 on [xxx]',
  81. },
  82. {
  83. duplicates: 0,
  84. entry: 'WARN test 1.23 on [xxx]',
  85. },
  86. ]);
  87. });
  88. test('should dedup on signature matches', () => {
  89. const logs = {
  90. rows: [
  91. {
  92. entry: 'WARN test 1.2323423 on [xxx]',
  93. },
  94. {
  95. entry: 'WARN test 1.23 on [xxx]',
  96. },
  97. {
  98. entry: 'INFO test 2.44 on [xxx]',
  99. },
  100. {
  101. entry: 'WARN test 1.23 on [xxx]',
  102. },
  103. ],
  104. };
  105. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.signature).rows).toEqual([
  106. {
  107. duplicates: 3,
  108. entry: 'WARN test 1.2323423 on [xxx]',
  109. },
  110. ]);
  111. });
  112. test('should return to non-deduped state on same log result', () => {
  113. const logs = {
  114. rows: [
  115. {
  116. entry: 'INFO 123',
  117. },
  118. {
  119. entry: 'WARN 123',
  120. },
  121. {
  122. entry: 'WARN 123',
  123. },
  124. ],
  125. };
  126. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.exact).rows).toEqual([
  127. {
  128. duplicates: 0,
  129. entry: 'INFO 123',
  130. },
  131. {
  132. duplicates: 1,
  133. entry: 'WARN 123',
  134. },
  135. ]);
  136. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.none).rows).toEqual(logs.rows);
  137. });
  138. });
  139. describe('calculateFieldStats()', () => {
  140. test('should return no stats for empty rows', () => {
  141. expect(calculateFieldStats([], /foo=(.*)/)).toEqual([]);
  142. });
  143. test('should return no stats if extractor does not match', () => {
  144. const rows = [
  145. {
  146. entry: 'foo=bar',
  147. },
  148. ];
  149. expect(calculateFieldStats(rows as any, /baz=(.*)/)).toEqual([]);
  150. });
  151. test('should return stats for found field', () => {
  152. const rows = [
  153. {
  154. entry: 'foo="42 + 1"',
  155. },
  156. {
  157. entry: 'foo=503 baz=foo',
  158. },
  159. {
  160. entry: 'foo="42 + 1"',
  161. },
  162. {
  163. entry: 't=2018-12-05T07:44:59+0000 foo=503',
  164. },
  165. ];
  166. expect(calculateFieldStats(rows as any, /foo=("[^"]*"|\S+)/)).toMatchObject([
  167. {
  168. value: '"42 + 1"',
  169. count: 2,
  170. },
  171. {
  172. value: '503',
  173. count: 2,
  174. },
  175. ]);
  176. });
  177. });
  178. describe('calculateLogsLabelStats()', () => {
  179. test('should return no stats for empty rows', () => {
  180. expect(calculateLogsLabelStats([], '')).toEqual([]);
  181. });
  182. test('should return no stats of label is not found', () => {
  183. const rows = [
  184. {
  185. entry: 'foo 1',
  186. labels: {
  187. foo: 'bar',
  188. },
  189. },
  190. ];
  191. expect(calculateLogsLabelStats(rows as any, 'baz')).toEqual([]);
  192. });
  193. test('should return stats for found labels', () => {
  194. const rows = [
  195. {
  196. entry: 'foo 1',
  197. labels: {
  198. foo: 'bar',
  199. },
  200. },
  201. {
  202. entry: 'foo 0',
  203. labels: {
  204. foo: 'xxx',
  205. },
  206. },
  207. {
  208. entry: 'foo 2',
  209. labels: {
  210. foo: 'bar',
  211. },
  212. },
  213. ];
  214. expect(calculateLogsLabelStats(rows as any, 'foo')).toMatchObject([
  215. {
  216. value: 'bar',
  217. count: 2,
  218. },
  219. {
  220. value: 'xxx',
  221. count: 1,
  222. },
  223. ]);
  224. });
  225. });
  226. describe('getParser()', () => {
  227. test('should return no parser on empty line', () => {
  228. expect(getParser('')).toBeUndefined();
  229. });
  230. test('should return no parser on unknown line pattern', () => {
  231. expect(getParser('To Be or not to be')).toBeUndefined();
  232. });
  233. test('should return logfmt parser on key value patterns', () => {
  234. expect(getParser('foo=bar baz="41 + 1')).toEqual(LogsParsers.logfmt);
  235. });
  236. test('should return JSON parser on JSON log lines', () => {
  237. // TODO implement other JSON value types than string
  238. expect(getParser('{"foo": "bar", "baz": "41 + 1"}')).toEqual(LogsParsers.JSON);
  239. });
  240. });
  241. describe('LogsParsers', () => {
  242. describe('logfmt', () => {
  243. const parser = LogsParsers.logfmt;
  244. test('should detect format', () => {
  245. expect(parser.test('foo')).toBeFalsy();
  246. expect(parser.test('foo=bar')).toBeTruthy();
  247. });
  248. test('should return parsed fields', () => {
  249. expect(parser.getFields('foo=bar baz="42 + 1"')).toEqual(['foo=bar', 'baz="42 + 1"']);
  250. });
  251. test('should return label for field', () => {
  252. expect(parser.getLabelFromField('foo=bar')).toBe('foo');
  253. });
  254. test('should return value for field', () => {
  255. expect(parser.getValueFromField('foo=bar')).toBe('bar');
  256. });
  257. test('should build a valid value matcher', () => {
  258. const matcher = parser.buildMatcher('foo');
  259. const match = 'foo=bar'.match(matcher);
  260. expect(match).toBeDefined();
  261. expect(match[1]).toBe('bar');
  262. });
  263. });
  264. describe('JSON', () => {
  265. const parser = LogsParsers.JSON;
  266. test('should detect format', () => {
  267. expect(parser.test('foo')).toBeFalsy();
  268. expect(parser.test('{"foo":"bar"}')).toBeTruthy();
  269. });
  270. test('should return parsed fields', () => {
  271. expect(parser.getFields('{ "foo" : "bar", "baz" : 42 }')).toEqual(['"foo" : "bar"', '"baz" : 42']);
  272. });
  273. test('should return parsed fields for nested quotes', () => {
  274. expect(parser.getFields(`{"foo":"bar: '[value=\\"42\\"]'"}`)).toEqual([`"foo":"bar: '[value=\\"42\\"]'"`]);
  275. });
  276. test('should return label for field', () => {
  277. expect(parser.getLabelFromField('"foo" : "bar"')).toBe('foo');
  278. });
  279. test('should return value for field', () => {
  280. expect(parser.getValueFromField('"foo" : "bar"')).toBe('"bar"');
  281. expect(parser.getValueFromField('"foo" : 42')).toBe('42');
  282. expect(parser.getValueFromField('"foo" : 42.1')).toBe('42.1');
  283. });
  284. test('should build a valid value matcher for strings', () => {
  285. const matcher = parser.buildMatcher('foo');
  286. const match = '{"foo":"bar"}'.match(matcher);
  287. expect(match).toBeDefined();
  288. expect(match[1]).toBe('bar');
  289. });
  290. test('should build a valid value matcher for integers', () => {
  291. const matcher = parser.buildMatcher('foo');
  292. const match = '{"foo":42.1}'.match(matcher);
  293. expect(match).toBeDefined();
  294. expect(match[1]).toBe('42.1');
  295. });
  296. });
  297. });