From b3c93e987d7985662bbda660974a3ed5077865cc Mon Sep 17 00:00:00 2001 From: Nikita Sapozhnikov Date: Fri, 14 Jul 2017 14:43:12 +0100 Subject: [PATCH] InstantGauge to use with datadog --- .../server/spring/metrics/InstantGauge.java | 24 +++++++++++++++++++ .../spring/metrics/InstantGaugeTest.java | 19 +++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 micro-metrics/src/main/java/com/aol/micro/server/spring/metrics/InstantGauge.java create mode 100644 micro-metrics/src/test/java/com/aol/micro/server/spring/metrics/InstantGaugeTest.java diff --git a/micro-metrics/src/main/java/com/aol/micro/server/spring/metrics/InstantGauge.java b/micro-metrics/src/main/java/com/aol/micro/server/spring/metrics/InstantGauge.java new file mode 100644 index 000000000..94f2f0192 --- /dev/null +++ b/micro-metrics/src/main/java/com/aol/micro/server/spring/metrics/InstantGauge.java @@ -0,0 +1,24 @@ +package com.aol.micro.server.spring.metrics; + +import java.util.concurrent.atomic.AtomicLong; + +import com.codahale.metrics.Gauge; + + +public class InstantGauge implements Gauge { + + private final AtomicLong counter = new AtomicLong(0l); + + @Override + public Long getValue() { + return counter.getAndSet(0l); + } + + public void increment() { + counter.incrementAndGet(); + } + + public void increase(long value) { + counter.addAndGet(value); + } +} diff --git a/micro-metrics/src/test/java/com/aol/micro/server/spring/metrics/InstantGaugeTest.java b/micro-metrics/src/test/java/com/aol/micro/server/spring/metrics/InstantGaugeTest.java new file mode 100644 index 000000000..32be828d0 --- /dev/null +++ b/micro-metrics/src/test/java/com/aol/micro/server/spring/metrics/InstantGaugeTest.java @@ -0,0 +1,19 @@ +package com.aol.micro.server.spring.metrics; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class InstantGaugeTest { + + @Test + public void instantGauge() { + InstantGauge gauge = new InstantGauge(); + gauge.increment(); + gauge.increase(3); + + assertEquals(4l ,gauge.getValue().longValue()); + assertEquals(0l ,gauge.getValue().longValue()); + + } + +}