From fad87fa92fc78b1b8df74b10fedce22eeb7e3ed4 Mon Sep 17 00:00:00 2001 From: Brian Harrington Date: Mon, 29 Jan 2024 11:45:59 -0600 Subject: [PATCH 1/2] fix some deprecation warnings --- .../spectator/api/CompositeTimerTest.java | 8 ++++---- .../netflix/spectator/api/NoopTimerTest.java | 4 ++-- .../netflix/spectator/api/RegistryTest.java | 2 +- .../placeholders/DefaultPlaceholderTimer.java | 19 +++++++++---------- .../DefaultPlaceholderTimerTest.java | 10 +++++----- .../spectator/atlas/AtlasRegistry.java | 4 ++-- .../spectator/atlas/AtlasTimerTest.java | 4 ++-- .../micrometer/MicrometerRegistryTest.java | 4 ++-- .../spectator/servo/ServoTimerTest.java | 8 ++++---- .../sidecar/SidecarRegistryTest.java | 4 ++-- .../spectator/stateless/StatelessTimer.java | 19 ------------------- 11 files changed, 33 insertions(+), 53 deletions(-) diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/CompositeTimerTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/CompositeTimerTest.java index 9e8561661..09c032b03 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/CompositeTimerTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/CompositeTimerTest.java @@ -91,7 +91,7 @@ public void testRecord() { public void testRecordCallable() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); - int v = t.record(() -> { + int v = t.recordCallable(() -> { clock.setMonotonicTime(500L); return 42; }); @@ -106,7 +106,7 @@ public void testRecordCallableException() throws Exception { clock.setMonotonicTime(100L); boolean seen = false; try { - t.record((Callable) () -> { + t.recordCallable((Callable) () -> { clock.setMonotonicTime(500L); throw new RuntimeException("foo"); }); @@ -122,7 +122,7 @@ public void testRecordCallableException() throws Exception { public void testRecordRunnable() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); - t.record(() -> clock.setMonotonicTime(500L)); + t.recordRunnable(() -> clock.setMonotonicTime(500L)); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); } @@ -133,7 +133,7 @@ public void testRecordRunnableException() throws Exception { clock.setMonotonicTime(100L); boolean seen = false; try { - t.record((Runnable) () -> { + t.recordRunnable(() -> { clock.setMonotonicTime(500L); throw new RuntimeException("foo"); }); diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/NoopTimerTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/NoopTimerTest.java index c7a31383e..79c5705bc 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/NoopTimerTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/NoopTimerTest.java @@ -41,7 +41,7 @@ public void testRecordCallableException() throws Exception { NoopTimer t = NoopTimer.INSTANCE; boolean seen = false; try { - t.record(() -> { + t.recordCallable(() -> { throw new Exception("foo"); }); } catch (Exception e) { @@ -55,7 +55,7 @@ public void testRecordRunnable() throws Exception { NoopTimer t = NoopTimer.INSTANCE; AtomicBoolean run = new AtomicBoolean(); - t.record(() -> run.set(true)); + t.recordRunnable(() -> run.set(true)); Assertions.assertTrue(run.get()); } diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/RegistryTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/RegistryTest.java index e71deb04c..80a677bfe 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/RegistryTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/RegistryTest.java @@ -519,7 +519,7 @@ public void resurrectUsingLambda() { ExpiringRegistry registry = new ExpiringRegistry(clock); Timer t = registry.timer("test"); - t.record(() -> { + t.recordRunnable(() -> { // Force expiration in the body of the lambda clock.setWallTime(60000 * 30); registry.removeExpiredMeters(); diff --git a/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java b/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java index 334f8d44f..627747176 100644 --- a/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java +++ b/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java @@ -15,10 +15,10 @@ */ package com.netflix.spectator.placeholders; +import com.netflix.spectator.api.Clock; import com.netflix.spectator.api.Registry; import com.netflix.spectator.api.Timer; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; /** @@ -27,6 +27,9 @@ * interface methods are called. */ class DefaultPlaceholderTimer extends AbstractDefaultPlaceholderMeter implements Timer { + + private final Clock clock; + /** * Constructs a new timer with the specified dynamic id. * @@ -35,21 +38,17 @@ class DefaultPlaceholderTimer extends AbstractDefaultPlaceholderMeter imp */ DefaultPlaceholderTimer(PlaceholderId id, Registry registry) { super(id, registry::timer); + this.clock = registry.clock(); } @Override - public void record(long amount, TimeUnit unit) { - resolveToCurrentMeter().record(amount, unit); + public Clock clock() { + return clock; } @Override - public T record(Callable f) throws Exception { - return resolveToCurrentMeter().record(f); - } - - @Override - public void record(Runnable f) { - resolveToCurrentMeter().record(f); + public void record(long amount, TimeUnit unit) { + resolveToCurrentMeter().record(amount, unit); } @Override diff --git a/spectator-ext-placeholders/src/test/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimerTest.java b/spectator-ext-placeholders/src/test/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimerTest.java index 8946f83f7..bc9673b0d 100644 --- a/spectator-ext-placeholders/src/test/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimerTest.java +++ b/spectator-ext-placeholders/src/test/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimerTest.java @@ -81,7 +81,7 @@ public void testRecordCallable() throws Exception { int expected = 42; Timer timer = factory.timer(factory.createId("testRecordCallable")); clock.setMonotonicTime(100L); - int actual = timer.record(() -> { + int actual = timer.recordCallable(() -> { clock.setMonotonicTime(500L); return expected; }); @@ -96,7 +96,7 @@ public void testRecordCallableException() throws Exception { clock.setMonotonicTime(100L); boolean seen = false; try { - timer.record(() -> { + timer.recordCallable(() -> { clock.setMonotonicTime(500L); throw new Exception("foo"); }); @@ -112,7 +112,7 @@ public void testRecordCallableException() throws Exception { public void testRecordRunnable() throws Exception { Timer timer = factory.timer(factory.createId("testRecordRunnable")); clock.setMonotonicTime(100L); - timer.record(() -> clock.setMonotonicTime(500L)); + timer.recordRunnable(() -> clock.setMonotonicTime(500L)); Assertions.assertEquals(1L, timer.count()); Assertions.assertEquals(timer.totalTime(), 400L); } @@ -121,10 +121,10 @@ public void testRecordRunnable() throws Exception { public void testRecordRunnableException() throws Exception { Timer timer = factory.timer(factory.createId("testRecordRunnableException")); clock.setMonotonicTime(100L); - Exception expectedExc = new RuntimeException("foo"); + RuntimeException expectedExc = new RuntimeException("foo"); Exception actualExc = null; try { - timer.record(() -> { + timer.recordRunnable(() -> { clock.setMonotonicTime(500L); throw expectedExc; }); diff --git a/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/AtlasRegistry.java b/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/AtlasRegistry.java index 6e19a1060..f957bab3b 100644 --- a/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/AtlasRegistry.java +++ b/spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/AtlasRegistry.java @@ -253,7 +253,7 @@ private void timePublishTask(String id, Runnable task) { } private void timePublishTask(String id, String lockName, Runnable task) { - publishTaskTimer(id).record(() -> { + publishTaskTimer(id).recordRunnable(() -> { Lock lock = publishTaskLocks.computeIfAbsent(lockName, n -> new ReentrantLock()); lock.lock(); try { @@ -341,7 +341,7 @@ void pollMeters(long t) { evaluator.update(id, timestamp, value); }; logger.debug("collecting measurements for time: {}", t); - publishTaskTimer("pollMeasurements").record(() -> StreamSupport + publishTaskTimer("pollMeasurements").recordRunnable(() -> StreamSupport .stream(spliterator(), parallelPolling) .forEach(meter -> ((AtlasMeter) meter).measure(t, consumer))); lastPollTimestamp = t; diff --git a/spectator-reg-atlas/src/test/java/com/netflix/spectator/atlas/AtlasTimerTest.java b/spectator-reg-atlas/src/test/java/com/netflix/spectator/atlas/AtlasTimerTest.java index a13274ec2..061ae9ddc 100644 --- a/spectator-reg-atlas/src/test/java/com/netflix/spectator/atlas/AtlasTimerTest.java +++ b/spectator-reg-atlas/src/test/java/com/netflix/spectator/atlas/AtlasTimerTest.java @@ -272,14 +272,14 @@ public void recordBatchPollsClockOnce() { @Test public void recordRunnable() { - dist.record(() -> clock.setMonotonicTime(clock.monotonicTime() + 2)); + dist.recordRunnable(() -> clock.setMonotonicTime(clock.monotonicTime() + 2)); clock.setWallTime(step + 1); checkValue(1, 2, 4, 2); } @Test public void recordCallable() throws Exception { - String s = dist.record(() -> { + String s = dist.recordCallable(() -> { clock.setMonotonicTime(clock.monotonicTime() + 2); return "foo"; }); diff --git a/spectator-reg-micrometer/src/test/java/com/netflix/spectator/micrometer/MicrometerRegistryTest.java b/spectator-reg-micrometer/src/test/java/com/netflix/spectator/micrometer/MicrometerRegistryTest.java index edaa8fd82..48f53f1e4 100644 --- a/spectator-reg-micrometer/src/test/java/com/netflix/spectator/micrometer/MicrometerRegistryTest.java +++ b/spectator-reg-micrometer/src/test/java/com/netflix/spectator/micrometer/MicrometerRegistryTest.java @@ -142,7 +142,7 @@ public void timerRecord() { @Test public void timerRecordRunnable() { Timer t = registry.timer("foo"); - t.record((Runnable) () -> clock.add(42, TimeUnit.SECONDS)); + t.recordRunnable(() -> clock.add(42, TimeUnit.SECONDS)); Assertions.assertEquals(1, t.count()); Assertions.assertEquals(TimeUnit.SECONDS.toNanos(42), t.totalTime()); } @@ -150,7 +150,7 @@ public void timerRecordRunnable() { @Test public void timerRecordCallable() throws Exception { Timer t = registry.timer("foo"); - t.record(() -> clock.add(42, TimeUnit.SECONDS)); + t.recordCallable(() -> clock.add(42, TimeUnit.SECONDS)); Assertions.assertEquals(1, t.count()); Assertions.assertEquals(TimeUnit.SECONDS.toNanos(42), t.totalTime()); } diff --git a/spectator-reg-servo/src/test/java/com/netflix/spectator/servo/ServoTimerTest.java b/spectator-reg-servo/src/test/java/com/netflix/spectator/servo/ServoTimerTest.java index 0e41eb07d..212d83827 100644 --- a/spectator-reg-servo/src/test/java/com/netflix/spectator/servo/ServoTimerTest.java +++ b/spectator-reg-servo/src/test/java/com/netflix/spectator/servo/ServoTimerTest.java @@ -79,7 +79,7 @@ public void testRecordZero() { public void testRecordCallable() throws Exception { Timer t = newTimer("foo"); clock.setMonotonicTime(100L); - int v = t.record(() -> { + int v = t.recordCallable(() -> { clock.setMonotonicTime(500L); return 42; }); @@ -94,7 +94,7 @@ public void testRecordCallableException() throws Exception { clock.setMonotonicTime(100L); boolean seen = false; try { - t.record((Callable) () -> { + t.recordCallable(() -> { clock.setMonotonicTime(500L); throw new RuntimeException("foo"); }); @@ -110,7 +110,7 @@ public void testRecordCallableException() throws Exception { public void testRecordRunnable() throws Exception { Timer t = newTimer("foo"); clock.setMonotonicTime(100L); - t.record(() -> clock.setMonotonicTime(500L)); + t.recordRunnable(() -> clock.setMonotonicTime(500L)); Assertions.assertEquals(t.count(), 1L); Assertions.assertEquals(t.totalTime(), 400L); } @@ -121,7 +121,7 @@ public void testRecordRunnableException() throws Exception { clock.setMonotonicTime(100L); boolean seen = false; try { - t.record((Runnable) () -> { + t.recordRunnable(() -> { clock.setMonotonicTime(500L); throw new RuntimeException("foo"); }); diff --git a/spectator-reg-sidecar/src/test/java/com/netflix/spectator/sidecar/SidecarRegistryTest.java b/spectator-reg-sidecar/src/test/java/com/netflix/spectator/sidecar/SidecarRegistryTest.java index 1d189f845..7191d719f 100644 --- a/spectator-reg-sidecar/src/test/java/com/netflix/spectator/sidecar/SidecarRegistryTest.java +++ b/spectator-reg-sidecar/src/test/java/com/netflix/spectator/sidecar/SidecarRegistryTest.java @@ -180,7 +180,7 @@ public void timerZero() { @Test public void timerRecordCallable() throws Exception { Timer t = registry.timer("test"); - long v = t.record(() -> { + long v = t.recordCallable(() -> { clock.setMonotonicTime(100_000_000); return registry.clock().monotonicTime(); }); @@ -191,7 +191,7 @@ public void timerRecordCallable() throws Exception { @Test public void timerRecordRunnable() { Timer t = registry.timer("test"); - t.record(() -> { + t.recordRunnable(() -> { clock.setMonotonicTime(100_000_000); }); assertSingleMessage("t:test:0.1"); diff --git a/spectator-reg-stateless/src/main/java/com/netflix/spectator/stateless/StatelessTimer.java b/spectator-reg-stateless/src/main/java/com/netflix/spectator/stateless/StatelessTimer.java index c634d78a6..67310a4c4 100644 --- a/spectator-reg-stateless/src/main/java/com/netflix/spectator/stateless/StatelessTimer.java +++ b/spectator-reg-stateless/src/main/java/com/netflix/spectator/stateless/StatelessTimer.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.function.LongToDoubleFunction; @@ -68,24 +67,6 @@ class StatelessTimer extends StatelessMeter implements Timer { } } - @Override public T record(Callable f) throws Exception { - final long start = clock.monotonicTime(); - try { - return f.call(); - } finally { - record(clock.monotonicTime() - start, TimeUnit.NANOSECONDS); - } - } - - @Override public void record(Runnable f) { - final long start = clock.monotonicTime(); - try { - f.run(); - } finally { - record(clock.monotonicTime() - start, TimeUnit.NANOSECONDS); - } - } - @Override public long count() { return count.get(); } From bfa1eead62e77aa6a420be0579eb79d77c3add0a Mon Sep 17 00:00:00 2001 From: Brian Harrington Date: Mon, 29 Jan 2024 11:47:50 -0600 Subject: [PATCH 2/2] use clock from underlying meter --- .../spectator/placeholders/DefaultPlaceholderTimer.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java b/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java index 627747176..783331c07 100644 --- a/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java +++ b/spectator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/DefaultPlaceholderTimer.java @@ -28,8 +28,6 @@ */ class DefaultPlaceholderTimer extends AbstractDefaultPlaceholderMeter implements Timer { - private final Clock clock; - /** * Constructs a new timer with the specified dynamic id. * @@ -38,12 +36,11 @@ class DefaultPlaceholderTimer extends AbstractDefaultPlaceholderMeter imp */ DefaultPlaceholderTimer(PlaceholderId id, Registry registry) { super(id, registry::timer); - this.clock = registry.clock(); } @Override public Clock clock() { - return clock; + return resolveToCurrentMeter().clock(); } @Override