From 6c60028919c4da9d3b5d8a458976227f00030fed Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Mon, 16 Sep 2024 07:18:52 -0700 Subject: [PATCH] Implement missing overrides in NoopDistributionSummary (#1159) Implement overrides for batchUpdater and record in NoopDistributionSummary that do nothing and avoid allocating. --- .../api/NoopDistributionSummary.java | 21 +++++++++++++++++++ .../api/NoopDistributionSummaryTest.java | 12 +++++++++++ 2 files changed, 33 insertions(+) diff --git a/spectator-api/src/main/java/com/netflix/spectator/api/NoopDistributionSummary.java b/spectator-api/src/main/java/com/netflix/spectator/api/NoopDistributionSummary.java index b15a7ee2c..5ef717cad 100644 --- a/spectator-api/src/main/java/com/netflix/spectator/api/NoopDistributionSummary.java +++ b/spectator-api/src/main/java/com/netflix/spectator/api/NoopDistributionSummary.java @@ -34,6 +34,9 @@ enum NoopDistributionSummary implements DistributionSummary { @Override public void record(long amount) { } + @Override public void record(long[] amounts, int n) { + } + @Override public Iterable measure() { return Collections.emptyList(); } @@ -45,4 +48,22 @@ enum NoopDistributionSummary implements DistributionSummary { @Override public long totalAmount() { return 0L; } + + @Override + public BatchUpdater batchUpdater(int batchSize) { + return NoopBatchUpdater.INSTANCE; + } + + private enum NoopBatchUpdater implements BatchUpdater { + INSTANCE; + + @Override public void flush() { + } + + @Override public void record(long amount) { + } + + @Override public void close() { + } + } } diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/NoopDistributionSummaryTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/NoopDistributionSummaryTest.java index be18b8972..3e38e93ef 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/NoopDistributionSummaryTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/NoopDistributionSummaryTest.java @@ -38,6 +38,18 @@ public void testMeasure() { NoopDistributionSummary t = NoopDistributionSummary.INSTANCE; t.record(42); Assertions.assertFalse(t.measure().iterator().hasNext()); + t.record(new long[] { 1L, 2L, 3L }, 3); + Assertions.assertFalse(t.measure().iterator().hasNext()); } + @Test + public void testBatchUpdate() throws Exception { + NoopDistributionSummary t = NoopDistributionSummary.INSTANCE; + try (DistributionSummary.BatchUpdater batchUpdater = t.batchUpdater(10)) { + for (long i = 0; i < 100; i++) { + batchUpdater.record(i); + } + } + Assertions.assertFalse(t.measure().iterator().hasNext()); + } }