HighFrequencyDetector { public List<Metric> detect(final List<Metric> metrics) { List<Metric> newList = new ArrayList<>(); Map<Metric, Integer> groupedMetrics = metrics.stream() .sorted(Comparator.comparing(Metric::getTime)) .collect( Collectors.groupingBy( Function.identity(), Collectors.collectingAndThen( Collectors.toList(), this::computeTimeDelta))); updateHighFrequencyMetricsStats(groupedMetrics); if (dropHighFrequencyMetric && highFrequencyMetrics.get().size() > 0) { metrics.stream() .filter( metric -> highFrequencyMetrics.get().getOrDefault(metric, 0) < minNumberOfTriggers) .forEach(newList::add); statistics.reportHighFrequencyMetricsDropped(metrics.size() - newList.size()); return newList; } return metrics; } @Inject  HighFrequencyDetector(); List<Metric> detect(final List<Metric> metrics); static final int BURST_THRESHOLD; }
@Test public void testHighFrequencyDetection() { List<Metric> finalList = new ArrayList<>(); for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { List<Metric> list = new ArrayList<>(); for (int i = 0; i < 30; i++) { list.add( new Metric( "KEY" + y, 42.0 + i, new Date(), ImmutableSet.of(), Map.of("tag1", "value1", "what", "fun"), ImmutableMap.of(), null)); } finalList.addAll(detector.detect(list)); } } assertEquals(90, finalList.size()); }
@Test public void testLargeNumberOfHighFreqMetrics() { List<Metric> finalList = new ArrayList<>(); for (int y = 0; y < 1000; y++) { for (int x = 0; x < 3; x++) { List<Metric> list = new ArrayList<>(); for (int i = 0; i < 20; i++) { list.add( new Metric( "KEY" + y, 42.0 + i, new Date(), ImmutableSet.of(), Map.of("tag1", "value1", "what", "fun"), ImmutableMap.of(), null)); } finalList.addAll(detector.detect(list)); } } assertEquals(20000, finalList.size()); }
@Test public void testHighFreqStatsSwapping() { List<Metric> finalList = new ArrayList<>(); for (int y = 0; y < 5; y++) { for (int x = 0; x < 3; x++) { List<Metric> list = new ArrayList<>(); for (int i = 0; i < 20; i++) { list.add( new Metric( "KEY" + y, 42.0 + i, new Date(), ImmutableSet.of(), Map.of("tag1", "value1", "what", "fun"), ImmutableMap.of(), null)); } finalList.addAll(shortSwapDetector.detect(list)); try { Thread.sleep(300); } catch (InterruptedException e) { } } } assertEquals(280, finalList.size()); }