getStatsGroups.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { getStatsGroups } from './getStatsGroups';
  2. describe('GroupListener', () => {
  3. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-examples.html
  4. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Container-Insights-view-metrics.html
  5. it('should correctly parse groups in stats query', () => {
  6. const testQueries = [
  7. {
  8. query:
  9. 'filter @message like /Exception/ | stats count(*) as exceptionCount by bin(1h) | sort exceptionCount desc',
  10. expected: ['bin(1h)'],
  11. },
  12. {
  13. query: `filter @type = "REPORT"
  14. | stats max(@memorySize / 1024 / 1024) as provisonedMemoryMB,
  15. min(@maxMemoryUsed / 1024 / 1024) as smallestMemoryRequestMB,
  16. avg(@maxMemoryUsed / 1024 / 1024) as avgMemoryUsedMB,
  17. max(@maxMemoryUsed / 1024 / 1024) as maxMemoryUsedMB,
  18. provisonedMemoryMB - maxMemoryUsedMB as overProvisionedMB`,
  19. expected: [],
  20. },
  21. {
  22. query: `stats count(@message) by bin(1h), @log, @logStream as fieldAlias`,
  23. expected: ['bin(1h)', '@log', 'fieldAlias'],
  24. },
  25. {
  26. query: `stats sum(packets) as packetsTransferred by srcAddr, dstAddr
  27. | sort packetsTransferred desc
  28. | limit 15`,
  29. expected: ['srcAddr', 'dstAddr'],
  30. },
  31. {
  32. query: `filter isIpv4InSubnet(srcAddr, "192.0.2.0/24")
  33. | stats sum(bytes) as bytesTransferred by dstAddr
  34. | sort bytesTransferred desc
  35. | limit 15`,
  36. expected: ['dstAddr'],
  37. },
  38. {
  39. query: `filter logStatus="SKIPDATA"
  40. | stats count(*) by bin(1h) as t
  41. | sort t
  42. `,
  43. expected: ['t'],
  44. },
  45. {
  46. query: `stats count(*) by queryType, bin(1h)`,
  47. expected: ['queryType', 'bin(1h)'],
  48. },
  49. {
  50. query: `parse @message "user=*, method:*, latency := *" as @user,
  51. @method, @latency | stats avg(@latency) by @method,
  52. @user`,
  53. expected: ['@method', '@user'],
  54. },
  55. {
  56. query: 'fields @timestamp, @message | sort @timestamp desc | limit 25',
  57. expected: [],
  58. },
  59. {
  60. query: `stats count(*)`,
  61. expected: [],
  62. },
  63. {
  64. query: `filter responseCode="SERVFAIL" | stats count(*) by queryName.0.1 as f0, bin(5m)`,
  65. expected: ['f0', 'bin(5m)'],
  66. },
  67. {
  68. query: `fields @timestamp, @message
  69. | filter @message like /dial tcp /
  70. | parse log /dial tcp (?<ip>[\d\.]+)\:(?<port>\d+)\: (?<reason>[^\\\"]+)/
  71. | stats count() by bin($__interval), reason`,
  72. expected: ['bin($__interval)', 'reason'],
  73. },
  74. ];
  75. for (const { query, expected } of testQueries) {
  76. expect(getStatsGroups(query)).toStrictEqual(expected);
  77. }
  78. });
  79. });