From d58eb2438d3b97741c32dccb39d2760c76011b1f Mon Sep 17 00:00:00 2001 From: hari9973 Date: Thu, 21 Mar 2019 10:39:22 +0530 Subject: [PATCH 1/5] modified project structure --- client/pom.xml | 72 ++++ .../metrics/client/ApptuitPutClient.java | 0 .../ai/apptuit/metrics/client/DataPoint.java | 0 .../ai/apptuit/metrics/client/Sanitizer.java | 0 .../metrics/client/XCollectorForwarder.java | 2 + .../metrics/client/ApptuitPutClientTest.java | 13 +- .../apptuit/metrics/client/DataPointTest.java | 354 ++++++++++++++++++ .../java/ai/apptuit/metrics/client/Util.java | 0 .../client/XCollectorForwarderTest.java | 40 +- dropwizard/pom.xml | 86 +++++ .../metrics/dropwizard/ApptuitReporter.java | 0 .../dropwizard/ApptuitReporterFactory.java | 4 - .../dropwizard/TagEncodedMetricName.java | 0 .../dropwizard/ApptuitReporterTest.java | 0 .../metrics/dropwizard/BaseMockClient.java | 0 .../dropwizard/MockApptuitPutClient.java | 0 .../dropwizard/MockXCollectorForwarder.java | 0 .../dropwizard/TagEncodedMetricNameTest.java | 0 micrometer-registry-apptuit/pom.xml | 119 ++++++ .../ApptuitConfig.java | 36 ++ .../ApptuitMeterRegistry.java | 247 ++++++++++++ ...ApptuitMetricsExportAutoConfiguration.java | 58 +++ .../ApptuitProperties.java | 35 ++ .../ApptuitPropertiesConfigAdapter.java | 34 ++ .../main/resources/META-INF/spring.factories | 13 + .../ApptuitMeterRegistryTest.java | 176 +++++++++ ...itMetricsExportAutoConfigurationTests.java | 49 +++ .../ApptuitPropertiesConfigAdapterTests.java | 32 ++ .../ApptuitPropertiesTests.java | 31 ++ pom.xml | 83 +--- .../apptuit/metrics/client/DataPointTest.java | 345 ----------------- 31 files changed, 1381 insertions(+), 448 deletions(-) create mode 100644 client/pom.xml rename {src => client/src}/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java (100%) rename {src => client/src}/main/java/ai/apptuit/metrics/client/DataPoint.java (100%) rename {src => client/src}/main/java/ai/apptuit/metrics/client/Sanitizer.java (100%) rename {src => client/src}/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java (99%) rename {src => client/src}/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java (96%) create mode 100644 client/src/test/java/ai/apptuit/metrics/client/DataPointTest.java rename {src => client/src}/test/java/ai/apptuit/metrics/client/Util.java (100%) rename {src => client/src}/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java (82%) create mode 100644 dropwizard/pom.xml rename {src => dropwizard/src}/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java (100%) rename {src => dropwizard/src}/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java (99%) rename {src => dropwizard/src}/main/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricName.java (100%) rename {src => dropwizard/src}/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java (100%) rename {src => dropwizard/src}/test/java/ai/apptuit/metrics/dropwizard/BaseMockClient.java (100%) rename {src => dropwizard/src}/test/java/ai/apptuit/metrics/dropwizard/MockApptuitPutClient.java (100%) rename {src => dropwizard/src}/test/java/ai/apptuit/metrics/dropwizard/MockXCollectorForwarder.java (100%) rename {src => dropwizard/src}/test/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricNameTest.java (100%) create mode 100644 micrometer-registry-apptuit/pom.xml create mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java create mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java create mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java create mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java create mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java create mode 100644 micrometer-registry-apptuit/src/main/resources/META-INF/spring.factories create mode 100644 micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java create mode 100644 micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java create mode 100644 micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java create mode 100644 micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java delete mode 100644 src/test/java/ai/apptuit/metrics/client/DataPointTest.java diff --git a/client/pom.xml b/client/pom.xml new file mode 100644 index 0000000..934713f --- /dev/null +++ b/client/pom.xml @@ -0,0 +1,72 @@ + + + + 4.0.0 + + ai.apptuit + metrics-apptuit + 0.0.1-SNAPSHOT + + metrics-apptuit-client + 0.0.1-SNAPSHOT + jar + metrics-apptuit-client + + + + junit + junit + ${junit.version} + test + + + org.mockito + mockito-core + 1.10.19 + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.jacoco + org.jacoco.agent + runtime + ${jacoco.version} + test + + + org.awaitility + awaitility + 3.1.1 + test + + + com.googlecode.json-simple + json-simple + 1.1.1 + test + + + diff --git a/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java b/client/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java similarity index 100% rename from src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java rename to client/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java diff --git a/src/main/java/ai/apptuit/metrics/client/DataPoint.java b/client/src/main/java/ai/apptuit/metrics/client/DataPoint.java similarity index 100% rename from src/main/java/ai/apptuit/metrics/client/DataPoint.java rename to client/src/main/java/ai/apptuit/metrics/client/DataPoint.java diff --git a/src/main/java/ai/apptuit/metrics/client/Sanitizer.java b/client/src/main/java/ai/apptuit/metrics/client/Sanitizer.java similarity index 100% rename from src/main/java/ai/apptuit/metrics/client/Sanitizer.java rename to client/src/main/java/ai/apptuit/metrics/client/Sanitizer.java diff --git a/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java b/client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java similarity index 99% rename from src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java rename to client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java index 23ea267..6e04e33 100644 --- a/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java +++ b/client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java @@ -1,3 +1,5 @@ + + /* * Copyright 2017 Agilx, Inc. * diff --git a/src/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java b/client/src/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java similarity index 96% rename from src/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java rename to client/src/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java index 60206ce..28c7e7a 100644 --- a/src/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java +++ b/client/src/test/java/ai/apptuit/metrics/client/ApptuitPutClientTest.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertEquals; import ai.apptuit.metrics.client.ApptuitPutClient.DatapointsHttpEntity; -import ai.apptuit.metrics.dropwizard.TagEncodedMetricName; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; @@ -56,8 +55,9 @@ public class ApptuitPutClientTest { private static MockServer httpServer; - private TagEncodedMetricName tagEncodedMetricName; private HashMap globalTags; + private String metricName; + private HashMap tags; private static String streamToString(InputStream inputStream) throws IOException { return new Scanner(new GZIPInputStream(inputStream)).useDelimiter("\0").next(); @@ -76,8 +76,9 @@ public static void tearDownClass() throws Exception { @Before public void setUp() throws Exception { - tagEncodedMetricName = TagEncodedMetricName.decode("proc.stat.cpu") - .withTags("type", "idle"); + metricName = "proc.stat.cpu"; + tags = new HashMap<>(); + tags.put("type", "idle"); globalTags = new HashMap<>(); globalTags.put("host", "rajiv"); @@ -173,8 +174,8 @@ private ArrayList createDataPoints(int numDataPoints) { for (int i = 0; i < numDataPoints; i++) { long value = 99 + i; long epoch = System.currentTimeMillis() / 1000 - value; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); + DataPoint dataPoint = new DataPoint(metricName, epoch, value, + tags); dataPoints.add(dataPoint); } return dataPoints; diff --git a/client/src/test/java/ai/apptuit/metrics/client/DataPointTest.java b/client/src/test/java/ai/apptuit/metrics/client/DataPointTest.java new file mode 100644 index 0000000..477cbc3 --- /dev/null +++ b/client/src/test/java/ai/apptuit/metrics/client/DataPointTest.java @@ -0,0 +1,354 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.client; + +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * @author Rajiv Shivane + */ + +public class DataPointTest { + + private String metricName; + private Map tags; + + @Before + public void setUp() throws Exception { + metricName = "proc.stat.cpu"; + tags = new HashMap<>(); + tags.put("host", "myhost"); + tags.put("type", "idle"); + } + + @Test + public void testNotEqualsNull() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + assertNotEquals(dp, null); + } + + @Test + public void testNotEqualsString() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + assertNotEquals(dp, "Text"); + } + + @Test + public void testEqualsSelf() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + assertEquals(dp, dp); + } + + @Test + public void testEqualsNoTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp1 = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + DataPoint dp2 = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + assertEquals(dp1, dp2); + assertEquals(dp1.hashCode(), dp2.hashCode()); + } + + + @Test + public void testEqualsWithTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp1 = new DataPoint(metricName, epoch, value, + tags); + DataPoint dp2 = new DataPoint(metricName, epoch, value, + tags); + assertEquals(dp1, dp2); + assertEquals(dp1.hashCode(), dp2.hashCode()); + } + + @Test + public void testNotEqualsName() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp1 = new DataPoint(metricName, epoch, value, + tags); + DataPoint dp2 = new DataPoint(metricName + "x", epoch, value, + tags); + assertNotEquals(dp1, dp2); + assertNotEquals(dp1.hashCode(), dp2.hashCode()); + } + + @Test + public void testNotEqualsTime() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp1 = new DataPoint(metricName, epoch, value, + tags); + DataPoint dp2 = new DataPoint(metricName, epoch + 1, value, + tags); + assertNotEquals(dp1, dp2); + assertNotEquals(dp1.hashCode(), dp2.hashCode()); + } + + @Test + public void testNotEqualsValue() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dp1 = new DataPoint(metricName, epoch, value, + tags); + DataPoint dp2 = new DataPoint(metricName, epoch, value + 1, + tags); + assertNotEquals(dp1, dp2); + assertNotEquals(dp1.hashCode(), dp2.hashCode()); + } + + @Test + public void testNotEqualsTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + Map tags = this.tags; + DataPoint dp1 = new DataPoint(metricName, epoch, value, tags); + Map newtags = new HashMap<>(tags); + newtags.put("key", "value"); + DataPoint dp2 = new DataPoint(metricName, epoch, value, newtags); + assertNotEquals(dp1, dp2); + assertNotEquals(dp1.hashCode(), dp2.hashCode()); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullTags() throws Exception { + new DataPoint(metricName, System.currentTimeMillis(), 1515, null); + } + + @Test + public void testToStringNoTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + + assertEquals("proc.stat.cpu " + epoch + " " + value, dataPoint.toString()); + } + + @Test + public void testToStringWithTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, + epoch, value, tags); + + assertEquals("proc.stat.cpu " + epoch + " " + value + " host=myhost type=idle", + dataPoint.toString()); + } + + @Test + public void testToTextNoTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + dataPoint.toTextLine(out, null, Sanitizer.NO_OP_SANITIZER); + + assertEquals("proc.stat.cpu " + epoch + " " + value + "\n", out.toString()); + } + + @Test + public void testToTextWithTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + dataPoint.toTextLine(out, null, Sanitizer.NO_OP_SANITIZER); + + assertEquals("proc.stat.cpu " + epoch + " " + value + " host=myhost type=idle\n", + out.toString()); + } + + @Test + public void testToTextWithGlobalTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, + epoch, value, Collections.emptyMap()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + dataPoint.toTextLine(out, tags, Sanitizer.NO_OP_SANITIZER); + + assertEquals("proc.stat.cpu " + epoch + " " + value + " host=myhost type=idle\n", + out.toString()); + } + + @Test + public void testToJsonNoTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, epoch, value, + Collections.emptyMap()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + dataPoint.toJson(new PrintStream(out), null, Sanitizer.NO_OP_SANITIZER); + String jsonTxt = out.toString(); + + DataPoint dp = Util.jsonToDataPoint(jsonTxt); + assertEquals(dataPoint, dp); + } + + @Test + public void testToJsonWithTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + dataPoint.toJson(new PrintStream(out), null, Sanitizer.NO_OP_SANITIZER); + String jsonTxt = out.toString(); + + DataPoint dp = Util.jsonToDataPoint(jsonTxt); + assertEquals(dataPoint, dp); + } + + @Test + public void testToJsonWithGlobalTags() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + DataPoint dataPoint = new DataPoint(metricName, + epoch, value, Collections.emptyMap()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + dataPoint.toJson(new PrintStream(out), tags, Sanitizer.NO_OP_SANITIZER); + String jsonTxt = out.toString(); + + DataPoint dp = Util.jsonToDataPoint(jsonTxt); + DataPoint expectedDataPoint = new DataPoint(metricName, + epoch, value, tags); + assertEquals(expectedDataPoint, dp); + } + + @Test + public void testToPromSanitization() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + metricName = "1proc.stat$cpu"; + tags = getTags("3host", "2myhost", "type__4", "idle"); + DataPoint dataPoint1 = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out1 = new ByteArrayOutputStream(); + dataPoint1.toTextLine(out1, Collections.emptyMap(), Sanitizer.PROMETHEUS_SANITIZER); + + assertEquals("_1proc_stat_cpu " + epoch + " " + value + " _3host=2myhost type_4=idle\n", + out1.toString()); + + metricName = "1proc.stat_わcpu"; + tags = getTags("3host", "2myhost_わ", "type__4", "idle"); + DataPoint dataPoint2 = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out2 = new ByteArrayOutputStream(); + dataPoint2.toTextLine(out2, Collections.emptyMap(), Sanitizer.PROMETHEUS_SANITIZER); + + assertEquals("_1proc_stat_cpu " + epoch + " " + value + " _3host=2myhost_わ type_4=idle\n", + out2.toString()); + } + + @Test + public void testToApptuitSanitization() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + metricName = "1proc.stat$cpu_わ"; + tags = getTags("3host", "2-myhost", "type__4", "idle"); + + DataPoint dataPoint1 = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out1 = new ByteArrayOutputStream(); + dataPoint1.toTextLine(out1, Collections.emptyMap(), Sanitizer.APPTUIT_SANITIZER); + + assertEquals("1proc.stat_cpu_わ " + epoch + " " + value + " 3host=2-myhost type_4=idle\n", + out1.toString()); + + + metricName = "1proc.stat$cpu"; + tags = getTags("3.host", "2-myhost", "type_/4", "idle"); + DataPoint dataPoint2 = new DataPoint(metricName, + epoch, value, Collections.emptyMap()); + + ByteArrayOutputStream out2 = new ByteArrayOutputStream(); + dataPoint2.toTextLine(out2, tags, Sanitizer.APPTUIT_SANITIZER); + + assertEquals("1proc.stat_cpu " + epoch + " " + value + " 3.host=2-myhost type_/4=idle\n", + out2.toString()); + } + + private Map getTags(String k1, String v1, String k2, String v2) { + Map tags = new HashMap<>(); + tags.put(k1, v1); + tags.put(k2, v2); + return tags; + } + + @Test + public void testToNoOpSanitization() throws Exception { + long epoch = System.currentTimeMillis(); + long value = 1515; + metricName = "1proc.stat$cpu_わ"; + tags = getTags("3host", "2-myhost", "type__4", "idle"); + DataPoint dataPoint1 = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out1 = new ByteArrayOutputStream(); + dataPoint1.toTextLine(out1, Collections.emptyMap(), Sanitizer.NO_OP_SANITIZER); + + assertEquals("1proc.stat$cpu_わ " + epoch + " " + value + " 3host=2-myhost type__4=idle\n", + out1.toString()); + + + metricName = "1proc.stat$cpu"; + tags = getTags("3.host", "2-myhost", "type_/4", "idle"); + DataPoint dataPoint2 = new DataPoint(metricName, + epoch, value, tags); + + ByteArrayOutputStream out2 = new ByteArrayOutputStream(); + dataPoint2.toTextLine(out2, Collections.emptyMap(), Sanitizer.NO_OP_SANITIZER); + + assertEquals("1proc.stat$cpu " + epoch + " " + value + " 3.host=2-myhost type_/4=idle\n", + out2.toString()); + } + +} diff --git a/src/test/java/ai/apptuit/metrics/client/Util.java b/client/src/test/java/ai/apptuit/metrics/client/Util.java similarity index 100% rename from src/test/java/ai/apptuit/metrics/client/Util.java rename to client/src/test/java/ai/apptuit/metrics/client/Util.java diff --git a/src/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java b/client/src/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java similarity index 82% rename from src/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java rename to client/src/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java index 2bd8d67..2a1095e 100644 --- a/src/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java +++ b/client/src/test/java/ai/apptuit/metrics/client/XCollectorForwarderTest.java @@ -19,7 +19,6 @@ import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; -import ai.apptuit.metrics.dropwizard.TagEncodedMetricName; import java.io.IOException; import java.net.DatagramPacket; @@ -47,8 +46,9 @@ public class XCollectorForwarderTest { private static final int UDP_PORT = 8953; private static MockServer mockServer; - private TagEncodedMetricName tagEncodedMetricName; private HashMap globalTags; + private String metricName; + private HashMap tags; @BeforeClass public static void setUpClass() throws Exception { @@ -65,8 +65,9 @@ public static void tearDownClass() throws Exception { @Before public void setUp() throws Exception { - tagEncodedMetricName = TagEncodedMetricName.decode("proc.stat.cpu") - .withTags("type", "idle"); + metricName = "proc.stat.cpu"; + tags = new HashMap<>(); + tags.put("type", "idle"); globalTags = new HashMap<>(); globalTags.put("host", "rajiv"); @@ -81,36 +82,28 @@ public void tearDown() throws Exception { @Test public void testSinglePacket() throws Exception { - testForward(10,Sanitizer.NO_OP_SANITIZER); + testForward(10); } - @Test - public void testMultiPacketDefaultSanitizer() throws Exception { - testForward(250, null); - } @Test public void testMultiPacket() throws Exception { - testForward(250, Sanitizer.NO_OP_SANITIZER); + testForward(250); } - private void testForward(int numDataPoints, Sanitizer sanitizer) throws SocketException { + private void testForward(int numDataPoints) throws SocketException { ArrayList dataPoints = createDataPoints(numDataPoints); XCollectorForwarder forwarder = new XCollectorForwarder(globalTags, new InetSocketAddress("127.0.0.1", UDP_PORT)); - if (sanitizer != null) { - forwarder.forward(dataPoints, sanitizer); - } else { - forwarder.forward(dataPoints); - } + forwarder.forward(dataPoints, Sanitizer.NO_OP_SANITIZER); await().atMost(5, TimeUnit.SECONDS).until(() -> mockServer.countReceivedDPs() == numDataPoints); DataPoint[] receivedDPs = mockServer.getReceivedDPs(); assertEquals(numDataPoints, receivedDPs.length); for (int i = 0; i < numDataPoints; i++) { - assertEquals(getExpectedDataPoint(dataPoints.get(i), globalTags, sanitizer), receivedDPs[i]); + assertEquals(getExpectedDataPoint(dataPoints.get(i), globalTags), receivedDPs[i]); } } @@ -119,20 +112,17 @@ private ArrayList createDataPoints(int numDataPoints) { for (int i = 0; i < numDataPoints; i++) { long value = 99 + i; long epoch = System.currentTimeMillis() / 1000 - value; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); + DataPoint dataPoint = new DataPoint(metricName, epoch, value, + tags); dataPoints.add(dataPoint); } return dataPoints; } - private DataPoint getExpectedDataPoint(DataPoint dataPoint, HashMap globalTags, Sanitizer sanitizer) { + private DataPoint getExpectedDataPoint(DataPoint dataPoint, HashMap globalTags) { Map tags = new HashMap<>(dataPoint.getTags()); tags.putAll(globalTags); - if (sanitizer == null) { - sanitizer = Sanitizer.DEFAULT_SANITIZER; - } - return new DataPoint(sanitizer.sanitizer(dataPoint.getMetric()), dataPoint.getTimestamp(), dataPoint.getValue(), + return new DataPoint(dataPoint.getMetric(), dataPoint.getTimestamp(), dataPoint.getValue(), tags); } @@ -206,4 +196,4 @@ private Map getTags(Scanner fields) { } } -} \ No newline at end of file +} diff --git a/dropwizard/pom.xml b/dropwizard/pom.xml new file mode 100644 index 0000000..acead93 --- /dev/null +++ b/dropwizard/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + ai.apptuit + metrics-apptuit + 0.0.1-SNAPSHOT + + metrics-apptuit-dropwizard + 0.0.1-SNAPSHOT + jar + metrics-apptuit-dropwizard + + + + junit + junit + ${junit.version} + test + + + org.mockito + mockito-core + 1.10.19 + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.jacoco + org.jacoco.agent + runtime + ${jacoco.version} + test + + + org.awaitility + awaitility + 3.1.1 + test + + + com.googlecode.json-simple + json-simple + 1.1.1 + test + + + org.slf4j + slf4j-simple + 1.7.26 + test + + + ai.apptuit + metrics-apptuit-client + 0.0.1-SNAPSHOT + + + io.dropwizard.metrics + metrics-core + ${metrics.version} + + + diff --git a/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java similarity index 100% rename from src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java rename to dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java diff --git a/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java similarity index 99% rename from src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java rename to dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java index 76e5502..866c2ae 100644 --- a/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java @@ -163,9 +163,7 @@ public boolean containsMatch(Set matchExpressions, String metricName) { private static class RegexStringMatchingStrategy extends DefaultStringMatchingStrategy { //TODO /* - private final LoadingCache patternCache; - private RegexStringMatchingStrategy() { patternCache = CacheBuilder.newBuilder() .expireAfterWrite(1, TimeUnit.HOURS) @@ -176,7 +174,6 @@ public Pattern load(String regex) throws Exception { } }); } - @Override public boolean containsMatch(Set matchExpressions, String metricName) { for (String regexExpression : matchExpressions) { @@ -185,7 +182,6 @@ public boolean containsMatch(Set matchExpressions, String metricName) { return true; } } - return false; } */ diff --git a/src/main/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricName.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricName.java similarity index 100% rename from src/main/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricName.java rename to dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricName.java diff --git a/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java similarity index 100% rename from src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java rename to dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java diff --git a/src/test/java/ai/apptuit/metrics/dropwizard/BaseMockClient.java b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/BaseMockClient.java similarity index 100% rename from src/test/java/ai/apptuit/metrics/dropwizard/BaseMockClient.java rename to dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/BaseMockClient.java diff --git a/src/test/java/ai/apptuit/metrics/dropwizard/MockApptuitPutClient.java b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/MockApptuitPutClient.java similarity index 100% rename from src/test/java/ai/apptuit/metrics/dropwizard/MockApptuitPutClient.java rename to dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/MockApptuitPutClient.java diff --git a/src/test/java/ai/apptuit/metrics/dropwizard/MockXCollectorForwarder.java b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/MockXCollectorForwarder.java similarity index 100% rename from src/test/java/ai/apptuit/metrics/dropwizard/MockXCollectorForwarder.java rename to dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/MockXCollectorForwarder.java diff --git a/src/test/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricNameTest.java b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricNameTest.java similarity index 100% rename from src/test/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricNameTest.java rename to dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/TagEncodedMetricNameTest.java diff --git a/micrometer-registry-apptuit/pom.xml b/micrometer-registry-apptuit/pom.xml new file mode 100644 index 0000000..2cb2a4d --- /dev/null +++ b/micrometer-registry-apptuit/pom.xml @@ -0,0 +1,119 @@ + + + 4.0.0 + + ai.apptuit + metrics-apptuit + 0.0.1-SNAPSHOT + + micrometer-registry-apptuit + 0.0.1-SNAPSHOT + jar + micrometer-registry-apptuit + + + + io.micrometer + micrometer-core + 1.1.3 + compile + + + ai.apptuit + metrics-apptuit-client + 0.0.1-SNAPSHOT + + + ai.apptuit + metrics-apptuit-dropwizard + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-actuator + 2.1.3.RELEASE + + + org.springframework.boot + spring-boot-autoconfigure + 2.1.3.RELEASE + + + org.springframework + spring-context + 4.2.5.RELEASE + + + org.springframework.boot + spring-boot-starter-test + 2.1.3.RELEASE + test + + + org.springframework + spring-beans + 5.1.5.RELEASE + test + + + org.springframework + spring-core + 5.1.5.RELEASE + test + + + junit + junit + ${junit.version} + test + + + org.mockito + mockito-core + 1.10.19 + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.jacoco + org.jacoco.agent + runtime + ${jacoco.version} + test + + + org.awaitility + awaitility + 3.1.1 + test + + + com.googlecode.json-simple + json-simple + 1.1.1 + test + + + \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java new file mode 100644 index 0000000..c125f2f --- /dev/null +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java @@ -0,0 +1,36 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import io.micrometer.core.instrument.step.StepRegistryConfig; + + +public interface ApptuitConfig extends StepRegistryConfig { + + ApptuitConfig DEFAULT = k -> null; + + String get(String key); + + default String prefix() { + return "apptuit"; + } + + default String token() { + String v = get(prefix() + ".token"); + return v == null ? "" : v; + } +} diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java new file mode 100644 index 0000000..ba7af87 --- /dev/null +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java @@ -0,0 +1,247 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ai.apptuit.metrics.client.ApptuitPutClient; +import ai.apptuit.metrics.client.DataPoint; +import ai.apptuit.metrics.client.Sanitizer; +import ai.apptuit.metrics.dropwizard.TagEncodedMetricName; +import io.micrometer.core.instrument.Clock; +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.DistributionSummary; +import io.micrometer.core.instrument.FunctionCounter; +import io.micrometer.core.instrument.FunctionTimer; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.LongTaskTimer; +import io.micrometer.core.instrument.Measurement; +import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Timer; +import io.micrometer.core.instrument.distribution.HistogramSnapshot; +import io.micrometer.core.instrument.distribution.ValueAtPercentile; +import io.micrometer.core.instrument.step.StepMeterRegistry; +import io.micrometer.core.instrument.util.MeterPartition; +import io.micrometer.core.instrument.util.NamedThreadFactory; +import io.micrometer.core.instrument.util.TimeUtils; +import io.micrometer.core.lang.NonNull; + +public class ApptuitMeterRegistry extends StepMeterRegistry { + private static final ThreadFactory DEFAULT_THREAD_FACTORY = new NamedThreadFactory("apptuit-metrics-publisher"); + + private final Logger logger = LoggerFactory.getLogger(ApptuitMeterRegistry.class); + + private final ApptuitConfig config; + + private final Map GLOBAL_TAGS_MAP = new HashMap(); + + private ApptuitPutClient putClient; + + public DataPointCollector testing = new DataPointCollector(System.currentTimeMillis() / 1000); + + @SuppressWarnings("deprecation") + public ApptuitMeterRegistry(ApptuitConfig config, Clock clock) { + this(config, clock, DEFAULT_THREAD_FACTORY); + } + + private ApptuitMeterRegistry(ApptuitConfig config, Clock clock, ThreadFactory threadFactory) { + super(config, clock); + this.config = config; + putClient = getClient(); + GLOBAL_TAGS_MAP.put("process", "spring-boot-application"); + start(threadFactory); + } + + public static Builder builder(ApptuitConfig config) { + return new Builder(config); + } + + private final ApptuitPutClient getClient(){ + return new ApptuitPutClient(config.token(), GLOBAL_TAGS_MAP); + } + + @Override + public void start(ThreadFactory threadFactory) { + if (config.enabled()) { + logger.info("publishing metrics to apptuit every " + TimeUtils.format(config.step())); + } + super.start(threadFactory); + } + + @Override + protected void publish() { + for (List batch : MeterPartition.partition(this, config.batchSize())) { + DataPointCollector collector = new DataPointCollector(System.currentTimeMillis() / 1000); + for(int i=0;i dataPoints; + + DataPointCollector(long epoch) { + this.epoch = epoch; + this.dataPoints = new LinkedList<>(); + } + + private String addBaseUnitToName(String name,Meter m) { + if(m.getId().getBaseUnit() != null) { + return name+"."+m.getId().getBaseUnit(); + } + return name; + } + + protected void collectGauge(String name, Gauge gauge) { + Double value = gauge.value(); + if (Double.isFinite(value)) { + addDataPoint(addBaseUnitToName(name,gauge), ((Number) value).doubleValue(),gauge.getId().getTags()); + } + } + + protected void collectCounter(String name, Counter counter) { + addDataPoint(name+".total", counter.count(),counter.getId().getTags()); + } + + protected void collectFunctionCounter(String name,FunctionCounter counter) { + addDataPoint(name+".total", counter.count(),counter.getId().getTags()); + } + + protected void collectHistogram(String name, DistributionSummary histogram) { + TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); + reportSnapshot(rootMetric, histogram.takeSnapshot(),histogram.getId().getTags()); + } + + protected void collectTimer(String name, Timer timer) { + TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); + reportSnapshot(rootMetric, timer.takeSnapshot(),timer.getId().getTags()); + } + + protected void collectLongTaskTimer(String name, LongTaskTimer longTimer) { + addDataPoint(name+".activeTasks",(double)longTimer.activeTasks(),longTimer.getId().getTags()); + addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".duration",longTimer.duration(getBaseTimeUnit()),longTimer.getId().getTags()); + } + protected void collectFunctionTimer(String name,FunctionTimer funcTimer) { + addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".count",funcTimer.count(),funcTimer.getId().getTags()); + addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".sum",funcTimer.totalTime(getBaseTimeUnit()),funcTimer.getId().getTags()); + addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".mean",funcTimer.mean(getBaseTimeUnit()),funcTimer.getId().getTags()); + } + + protected void collectMeter(String name,Meter meter) { + for (Measurement measurement : meter.measure()) { + this.addDataPoint(name+"."+measurement.getStatistic().toString().toLowerCase(), measurement.getValue(), meter.getId().getTags()); + } + } + + private void reportSnapshot(TagEncodedMetricName metric, HistogramSnapshot snapshot,List tags) { + addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration.max"), snapshot.max(getBaseTimeUnit()),tags); + addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration.mean"), snapshot.mean(getBaseTimeUnit()),tags); + addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration.count"), snapshot.total(),tags); + ValueAtPercentile[] percentValues = snapshot.percentileValues(); + for(ValueAtPercentile obj: percentValues){ + addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration").withTags("quantile", Double.toString(obj.percentile())), + obj.value(),tags); + } + } + + private void addDataPoint(String name, double value,List tags) { + addDataPoint(TagEncodedMetricName.decode(name), value,tags); + } + + private void addDataPoint(TagEncodedMetricName name, Number value,List tags) { + Map tag = new HashMap(); + for(Tag obj:tags) { + tag.put(obj.getKey(), obj.getValue()); + } + DataPoint dataPoint; + if(tag.isEmpty()) { + dataPoint = new DataPoint(name.getMetricName(), epoch, value, name.getTags()); + } else { + dataPoint = new DataPoint(name.getMetricName(), epoch, value, tag); + } + dataPoints.add(dataPoint); + //logger.error(dataPoint.getMetric()+dataPoint.getTags()+dataPoint.getValue()); + } + } + + @Override + @NonNull + protected TimeUnit getBaseTimeUnit() { + return TimeUnit.MILLISECONDS; + } + + public static class Builder { + private final ApptuitConfig config; + + private Clock clock = Clock.SYSTEM; + private ThreadFactory threadFactory = DEFAULT_THREAD_FACTORY; + + @SuppressWarnings("deprecation") + Builder(ApptuitConfig config) { + this.config = config; + } + + public Builder clock(Clock clock) { + this.clock = clock; + return this; + } + + public Builder threadFactory(ThreadFactory threadFactory) { + this.threadFactory = threadFactory; + return this; + } + + public ApptuitMeterRegistry build() { + return new ApptuitMeterRegistry(config, clock, threadFactory); + } + } +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java new file mode 100644 index 0000000..947b125 --- /dev/null +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java @@ -0,0 +1,58 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import io.micrometer.core.instrument.Clock; + +import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, + SimpleMetricsExportAutoConfiguration.class }) +@AutoConfigureAfter(MetricsAutoConfiguration.class) +@ConditionalOnBean(Clock.class) +@ConditionalOnClass(ApptuitMeterRegistry.class) +@ConditionalOnProperty(prefix = "management.metrics.export.apptuit", name = "enabled", havingValue = "true", matchIfMissing = true) +@EnableConfigurationProperties(ApptuitProperties.class) +public class ApptuitMetricsExportAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public ApptuitConfig apptuitConfig(ApptuitProperties apptuitProperties) { + return new ApptuitPropertiesConfigAdapter(apptuitProperties); + } + + @Bean + @ConditionalOnMissingBean + public ApptuitMeterRegistry apptuitMeterRegistry(ApptuitConfig apptuitConfig, + Clock clock) { + return new ApptuitMeterRegistry(apptuitConfig, clock); + } + +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java new file mode 100644 index 0000000..e73f6d9 --- /dev/null +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "management.metrics.export.apptuit") +public class ApptuitProperties extends StepRegistryProperties { + + private String token = ""; + + public String gettoken() { + return this.token; + } + + public void settoken(String token) { + this.token = token; + } + +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java new file mode 100644 index 0000000..3a9acd1 --- /dev/null +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter; + + +class ApptuitPropertiesConfigAdapter extends + StepRegistryPropertiesConfigAdapter implements ApptuitConfig { + + ApptuitPropertiesConfigAdapter(ApptuitProperties properties) { + super(properties); + } + + @Override + public String token() { + return get(ApptuitProperties::gettoken, ApptuitConfig.super::token); + } + +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/resources/META-INF/spring.factories b/micrometer-registry-apptuit/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..26dcd42 --- /dev/null +++ b/micrometer-registry-apptuit/src/main/resources/META-INF/spring.factories @@ -0,0 +1,13 @@ +# Copyright 2017 Agilx, Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +ai.apptuit.metrics.micrometer_registry_apptuit.ApptuitMetricsExportAutoConfiguration \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java new file mode 100644 index 0000000..de8594b --- /dev/null +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java @@ -0,0 +1,176 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.Random; +import java.util.concurrent.ThreadFactory; + +import io.micrometer.core.instrument.util.NamedThreadFactory; +import org.junit.Test; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.DistributionSummary; +import io.micrometer.core.instrument.FunctionCounter; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.LongTaskTimer; +import io.micrometer.core.instrument.Measurement; +import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.MockClock; +import io.micrometer.core.instrument.Statistic; +import io.micrometer.core.instrument.Timer; + +public class ApptuitMeterRegistryTest { + private MockClock clock = new MockClock(); + private ApptuitConfig config = new ApptuitConfig() { + @Override + public String get(String key) { + return null; + } + + @Override + public boolean enabled() { + return true; + } + }; + private static final ThreadFactory DEFAULT_THREAD_FACTORY = new NamedThreadFactory("apptuit-metrics-publisher"); + private ApptuitMeterRegistry registry = ApptuitMeterRegistry.builder(config).clock(clock).build(); + ApptuitMeterRegistry.DataPointCollector collector = registry.new DataPointCollector(System.currentTimeMillis() / 1000); + private Random r = new Random(); + private int rangeMin = 1; + private int rangeMax = 1000; + + @Test + public void objectCreationUsingBuilder(){ + ApptuitMeterRegistry register = ApptuitMeterRegistry.builder(config).clock(clock).threadFactory(DEFAULT_THREAD_FACTORY).build(); + ApptuitMeterRegistry.DataPointCollector collect = register.new DataPointCollector(System.currentTimeMillis() / 1000); + Counter count = Counter.builder("demo.counter").register(register); + collect.collectCounter(count.getId().getName(),count); + assertEquals(0.0,collect.dataPoints.get(0).getValue()); + assertEquals("demo.counter.total",collect.dataPoints.get(0).getMetric()); + collect.dataPoints.clear(); + } + + @Test + public void testCounter() { + Counter counter = Counter.builder("my.Counter").baseUnit("seconds").tag("type","testing").register(registry); + counter.increment(); + counter.increment(); + //FunctionTimer t = FunctionTimer.builder("my.FunctionTimer").register(registry); + clock.add(config.step()); + collector.collectCounter(counter.getId().getName(),counter); + registry.publish(); + assertEquals(1,collector.dataPoints.size()); + assertEquals(2.0,collector.dataPoints.get(0).getValue()); + assertEquals("my.Counter.total",collector.dataPoints.get(0).getMetric()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + Counter counter1 = Counter.builder("my.Counter").register(registry); + collector.collectCounter(counter1.getId().getName(),counter1); + assertEquals(1,collector.dataPoints.size()); + assertEquals(0.0,collector.dataPoints.get(0).getValue()); + assertEquals("my.Counter.total",collector.dataPoints.get(0).getMetric()); + collector.dataPoints.clear(); + } + + @Test + public void testGuage() { + double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble(); + Gauge gauge = Gauge.builder("my.Gauge", randomValue, Number::doubleValue).baseUnit("bytes").tag("type","testing").register(registry); + collector.collectGauge(gauge.getId().getName(),gauge); + registry.publish(); + assertEquals(1,collector.dataPoints.size()); + assertEquals(randomValue,collector.dataPoints.get(0).getValue()); + assertEquals("my.Gauge.bytes",collector.dataPoints.get(0).getMetric()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + Gauge gauge1 = Gauge.builder("my.Gauge", randomValue, Number::doubleValue).register(registry); + collector.collectGauge(gauge1.getId().getName(),gauge1); + assertEquals(1,collector.dataPoints.size()); + assertEquals(randomValue,collector.dataPoints.get(0).getValue()); + assertEquals("my.Gauge",collector.dataPoints.get(0).getMetric()); + collector.dataPoints.clear(); + } + + @Test + public void testFunctionCounter() { + FunctionCounter counter = FunctionCounter.builder("my.Function.Counter",0.0, Number::doubleValue).tag("type","testing").register(registry); + collector.collectFunctionCounter(counter.getId().getName(), counter); + registry.publish(); + assertEquals(1,collector.dataPoints.size()); + assertEquals(0.0,collector.dataPoints.get(0).getValue()); + assertEquals("my.Function.Counter.total",collector.dataPoints.get(0).getMetric()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + } + + @Test + public void testHistogram() { + DistributionSummary histogram = DistributionSummary.builder("my.Histogram").baseUnit("seconds").tag("type","testing").publishPercentiles(0.95).register(registry); + collector.collectHistogram(histogram.getId().getName(), histogram); + registry.publish(); + assertEquals(4,collector.dataPoints.size()); + assertEquals(0.0,collector.dataPoints.get(0).getValue()); + assertEquals("my.Histogram."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration.max",collector.dataPoints.get(0).getMetric()); + assertEquals("my.Histogram."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration.mean",collector.dataPoints.get(1).getMetric()); + assertEquals("my.Histogram."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration.count",collector.dataPoints.get(2).getMetric()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + } + + @Test + public void testTimer() { + Timer timer = Timer.builder("my.Timer").tag("type","testing").register(registry); + collector.collectTimer(timer.getId().getName(), timer); + registry.publish(); + assertEquals(3,collector.dataPoints.size()); + assertEquals(0.0,collector.dataPoints.get(0).getValue()); + assertEquals("my.Timer."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration.max",collector.dataPoints.get(0).getMetric()); + assertEquals("my.Timer."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration.mean",collector.dataPoints.get(1).getMetric()); + assertEquals("my.Timer."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration.count",collector.dataPoints.get(2).getMetric()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + } + + @Test + public void testLongTaskTimer() { + LongTaskTimer timer = LongTaskTimer.builder("my.LongTaskTimer").tag("type","testing").register(registry); + collector.collectLongTaskTimer(timer.getId().getName(), timer); + registry.publish(); + assertEquals(2,collector.dataPoints.size()); + assertEquals("my.LongTaskTimer.activeTasks",collector.dataPoints.get(0).getMetric()); + assertEquals("my.LongTaskTimer."+registry.getBaseTimeUnit().toString().toLowerCase()+".duration",collector.dataPoints.get(1).getMetric()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + } + + @Test + public void testMeter() { + Meter meter = Meter.builder("my.Meter",Meter.Type.COUNTER,Collections.singletonList(new Measurement(() -> 1.0, Statistic.COUNT))).tag("type","testing").register(registry); + collector.collectMeter(meter.getId().getName(),meter); + registry.publish(); + assertEquals(1,collector.dataPoints.size()); + assertEquals("my.Meter.count",collector.dataPoints.get(0).getMetric()); + assertEquals(1.0,collector.dataPoints.get(0).getValue()); + assertEquals(1,collector.dataPoints.get(0).getTags().size()); + collector.dataPoints.clear(); + } + + +} diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java new file mode 100644 index 0000000..1077d66 --- /dev/null +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import static org.assertj.core.api.Assertions.assertThat; + + + +public class ApptuitMetricsExportAutoConfigurationTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of(ApptuitMetricsExportAutoConfiguration.class)); + + @Test + public void backsOffWithoutAClock() { + this.contextRunner.withUserConfiguration(ApptuitMetricsExportAutoConfiguration.class) + .run((context) -> assertThat(context) + .doesNotHaveBean(ApptuitMeterRegistry.class)); + } + + @Test + public void autoConfigurationCanBeDisabled() { + this.contextRunner.withUserConfiguration(ApptuitMetricsExportAutoConfiguration.class) + .withPropertyValues("management.metrics.export.elastic.enabled=false") + .run((context) -> assertThat(context) + .doesNotHaveBean(ApptuitMeterRegistry.class) + .doesNotHaveBean(ApptuitConfig.class)); + } + +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java new file mode 100644 index 0000000..2a128b7 --- /dev/null +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java @@ -0,0 +1,32 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ApptuitPropertiesConfigAdapterTests { + + @Test + public void whenPropertiesTokenIsSetAdapterTokenReturnsIt() { + ApptuitProperties properties = new ApptuitProperties(); + properties.settoken("123456"); + assertThat(new ApptuitPropertiesConfigAdapter(properties).token()) + .isEqualTo("123456"); + } +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java new file mode 100644 index 0000000..c190c6e --- /dev/null +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer_registry_apptuit; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ApptuitPropertiesTests { + + @Test + public void defaultValuesAreConsistent() { + ApptuitProperties properties = new ApptuitProperties(); + ApptuitConfig config = ApptuitConfig.DEFAULT; + assertThat(properties.gettoken()).isEqualTo(config.token()); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index fb545cf..d8e3148 100644 --- a/pom.xml +++ b/pom.xml @@ -1,33 +1,25 @@ - - - 4.0.0 + + 4.0.0 ai.apptuit metrics-apptuit - 1.0-SNAPSHOT - jar - + 0.0.1-SNAPSHOT + pom metrics-apptuit https://github.com/ApptuitAI/metrics-apptuit/ + 2017 https://github.com/ApptuitAI/metrics-apptuit @@ -52,9 +44,16 @@ limitations under the License. https://api.bintray.com/maven/apptuitai/maven/metrics-apptuit/;publish=1 - + + + client + dropwizard + micrometer-registry-apptuit + + 1.8 + 1.8 UTF-8 @@ -93,60 +92,6 @@ limitations under the License. false - - - io.dropwizard.metrics - metrics-core - ${metrics.version} - - - - - junit - junit - ${junit.version} - test - - - org.mockito - mockito-core - 1.10.19 - test - - - org.powermock - powermock-api-mockito - ${powermock.version} - test - - - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - org.jacoco - org.jacoco.agent - runtime - ${jacoco.version} - test - - - org.awaitility - awaitility - 3.1.1 - test - - - com.googlecode.json-simple - json-simple - 1.1.1 - test - - @@ -324,6 +269,7 @@ limitations under the License. + @@ -462,3 +408,4 @@ limitations under the License. + diff --git a/src/test/java/ai/apptuit/metrics/client/DataPointTest.java b/src/test/java/ai/apptuit/metrics/client/DataPointTest.java deleted file mode 100644 index ffc4414..0000000 --- a/src/test/java/ai/apptuit/metrics/client/DataPointTest.java +++ /dev/null @@ -1,345 +0,0 @@ -/* - * Copyright 2017 Agilx, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ai.apptuit.metrics.client; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -import ai.apptuit.metrics.dropwizard.TagEncodedMetricName; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.junit.Before; -import org.junit.Test; - -/** - * @author Rajiv Shivane - */ - -public class DataPointTest { - - private TagEncodedMetricName tagEncodedMetricName; - - @Before - public void setUp() throws Exception { - tagEncodedMetricName = TagEncodedMetricName.decode("proc.stat.cpu") - .withTags("host", "myhost", "type", "idle"); - } - - @Test - public void testNotEqualsNull() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - assertNotEquals(dp, null); - } - - @Test - public void testNotEqualsString() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - assertNotEquals(dp, "Text"); - } - - @Test - public void testEqualsSelf() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - assertEquals(dp, dp); - } - - @Test - public void testEqualsNoTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp1 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - DataPoint dp2 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - assertEquals(dp1, dp2); - assertEquals(dp1.hashCode(), dp2.hashCode()); - } - - - @Test - public void testEqualsWithTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp1 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); - DataPoint dp2 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); - assertEquals(dp1, dp2); - assertEquals(dp1.hashCode(), dp2.hashCode()); - } - - @Test - public void testNotEqualsName() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp1 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); - DataPoint dp2 = new DataPoint(tagEncodedMetricName.getMetricName() + "x", epoch, value, - tagEncodedMetricName.getTags()); - assertNotEquals(dp1, dp2); - assertNotEquals(dp1.hashCode(), dp2.hashCode()); - } - - @Test - public void testNotEqualsTime() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp1 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); - DataPoint dp2 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch + 1, value, - tagEncodedMetricName.getTags()); - assertNotEquals(dp1, dp2); - assertNotEquals(dp1.hashCode(), dp2.hashCode()); - } - - @Test - public void testNotEqualsValue() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dp1 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - tagEncodedMetricName.getTags()); - DataPoint dp2 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value + 1, - tagEncodedMetricName.getTags()); - assertNotEquals(dp1, dp2); - assertNotEquals(dp1.hashCode(), dp2.hashCode()); - } - - @Test - public void testNotEqualsTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - Map tags = tagEncodedMetricName.getTags(); - DataPoint dp1 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, tags); - Map newtags = new HashMap<>(tags); - newtags.put("key", "value"); - DataPoint dp2 = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, newtags); - assertNotEquals(dp1, dp2); - assertNotEquals(dp1.hashCode(), dp2.hashCode()); - } - - @Test(expected = IllegalArgumentException.class) - public void testNullTags() throws Exception { - new DataPoint(tagEncodedMetricName.getMetricName(), System.currentTimeMillis(), 1515, null); - } - - @Test - public void testToStringNoTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - - assertEquals("proc.stat.cpu " + epoch + " " + value, dataPoint.toString()); - } - - @Test - public void testToStringWithTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, tagEncodedMetricName.getTags()); - - assertEquals("proc.stat.cpu " + epoch + " " + value + " host=myhost type=idle", - dataPoint.toString()); - } - - @Test - public void testToTextNoTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - dataPoint.toTextLine(out, null, Sanitizer.NO_OP_SANITIZER); - - assertEquals("proc.stat.cpu " + epoch + " " + value + "\n", out.toString()); - } - - @Test - public void testToTextWithTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, tagEncodedMetricName.getTags()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - dataPoint.toTextLine(out, null, Sanitizer.NO_OP_SANITIZER); - - assertEquals("proc.stat.cpu " + epoch + " " + value + " host=myhost type=idle\n", - out.toString()); - } - - @Test - public void testToTextWithGlobalTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - dataPoint.toTextLine(out, tagEncodedMetricName.getTags(), Sanitizer.NO_OP_SANITIZER); - - assertEquals("proc.stat.cpu " + epoch + " " + value + " host=myhost type=idle\n", - out.toString()); - } - - @Test - public void testToJsonNoTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), epoch, value, - Collections.emptyMap()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - dataPoint.toJson(new PrintStream(out), null, Sanitizer.NO_OP_SANITIZER); - String jsonTxt = out.toString(); - - DataPoint dp = Util.jsonToDataPoint(jsonTxt); - assertEquals(dataPoint, dp); - } - - @Test - public void testToJsonWithTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, tagEncodedMetricName.getTags()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - dataPoint.toJson(new PrintStream(out), null, Sanitizer.NO_OP_SANITIZER); - String jsonTxt = out.toString(); - - DataPoint dp = Util.jsonToDataPoint(jsonTxt); - assertEquals(dataPoint, dp); - } - - @Test - public void testToJsonWithGlobalTags() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - DataPoint dataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - dataPoint.toJson(new PrintStream(out), tagEncodedMetricName.getTags(), Sanitizer.NO_OP_SANITIZER); - String jsonTxt = out.toString(); - - DataPoint dp = Util.jsonToDataPoint(jsonTxt); - DataPoint expectedDataPoint = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, tagEncodedMetricName.getTags()); - assertEquals(expectedDataPoint, dp); - } - - @Test - public void testToPromSanitization() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - tagEncodedMetricName = TagEncodedMetricName.decode("1proc.stat$cpu") - .withTags("3host", "2myhost", "type__4", "idle"); - DataPoint dataPoint1 = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out1 = new ByteArrayOutputStream(); - dataPoint1.toTextLine(out1, tagEncodedMetricName.getTags(), Sanitizer.PROMETHEUS_SANITIZER); - - assertEquals("_1proc_stat_cpu " + epoch + " " + value + " _3host=2myhost type_4=idle\n", - out1.toString()); - - tagEncodedMetricName = TagEncodedMetricName.decode("1proc.stat_わcpu") - .withTags("3host", "2myhost_わ", "type__4", "idle"); - DataPoint dataPoint2 = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out2 = new ByteArrayOutputStream(); - dataPoint2.toTextLine(out2, tagEncodedMetricName.getTags(), Sanitizer.PROMETHEUS_SANITIZER); - - assertEquals("_1proc_stat_cpu " + epoch + " " + value + " _3host=2myhost_わ type_4=idle\n", - out2.toString()); - } - - @Test - public void testToApptuitSanitization() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - tagEncodedMetricName = TagEncodedMetricName.decode("1proc.stat$cpu_わ") - .withTags("3host", "2-myhost", "type__4", "idle"); - DataPoint dataPoint1 = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out1 = new ByteArrayOutputStream(); - dataPoint1.toTextLine(out1, tagEncodedMetricName.getTags(), Sanitizer.APPTUIT_SANITIZER); - - assertEquals("1proc.stat_cpu_わ " + epoch + " " + value + " 3host=2-myhost type_4=idle\n", - out1.toString()); - - - tagEncodedMetricName = TagEncodedMetricName.decode("1proc.stat$cpu") - .withTags("3.host", "2-myhost", "type_/4", "idle"); - DataPoint dataPoint2 = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out2 = new ByteArrayOutputStream(); - dataPoint2.toTextLine(out2, tagEncodedMetricName.getTags(), Sanitizer.APPTUIT_SANITIZER); - - assertEquals("1proc.stat_cpu " + epoch + " " + value + " 3.host=2-myhost type_/4=idle\n", - out2.toString()); - } - - @Test - public void testToNoOpSanitization() throws Exception { - long epoch = System.currentTimeMillis(); - long value = 1515; - tagEncodedMetricName = TagEncodedMetricName.decode("1proc.stat$cpu_わ") - .withTags("3host", "2-myhost", "type__4", "idle"); - DataPoint dataPoint1 = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out1 = new ByteArrayOutputStream(); - dataPoint1.toTextLine(out1, tagEncodedMetricName.getTags(), Sanitizer.NO_OP_SANITIZER); - - assertEquals("1proc.stat$cpu_わ " + epoch + " " + value + " 3host=2-myhost type__4=idle\n", - out1.toString()); - - - tagEncodedMetricName = TagEncodedMetricName.decode("1proc.stat$cpu") - .withTags("3.host", "2-myhost", "type_/4", "idle"); - DataPoint dataPoint2 = new DataPoint(tagEncodedMetricName.getMetricName(), - epoch, value, Collections.emptyMap()); - - ByteArrayOutputStream out2 = new ByteArrayOutputStream(); - dataPoint2.toTextLine(out2, tagEncodedMetricName.getTags(), Sanitizer.NO_OP_SANITIZER); - - assertEquals("1proc.stat$cpu " + epoch + " " + value + " 3.host=2-myhost type_/4=idle\n", - out2.toString()); - } - -} From 2b86a141159ad3923223c3280839c2355a29e498 Mon Sep 17 00:00:00 2001 From: hari9973 Date: Thu, 21 Mar 2019 10:58:29 +0530 Subject: [PATCH 2/5] removed unusedimports from ApptuitMeterRegistry --- .../micrometer_registry_apptuit/ApptuitMeterRegistry.java | 4 +--- .../micrometer_registry_apptuit/ApptuitMeterRegistryTest.java | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java index ba7af87..bb1d6c9 100644 --- a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java @@ -16,8 +16,6 @@ package ai.apptuit.metrics.micrometer_registry_apptuit; -import java.math.BigDecimal; -import java.math.BigInteger; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -244,4 +242,4 @@ public ApptuitMeterRegistry build() { return new ApptuitMeterRegistry(config, clock, threadFactory); } } -} \ No newline at end of file +} diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java index de8594b..8063f02 100644 --- a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java @@ -51,7 +51,7 @@ public boolean enabled() { }; private static final ThreadFactory DEFAULT_THREAD_FACTORY = new NamedThreadFactory("apptuit-metrics-publisher"); private ApptuitMeterRegistry registry = ApptuitMeterRegistry.builder(config).clock(clock).build(); - ApptuitMeterRegistry.DataPointCollector collector = registry.new DataPointCollector(System.currentTimeMillis() / 1000); + private ApptuitMeterRegistry.DataPointCollector collector = registry.new DataPointCollector(System.currentTimeMillis() / 1000); private Random r = new Random(); private int rangeMin = 1; private int rangeMax = 1000; From 3c831d6e517e7eca297e5b5e588cf98c36560dc0 Mon Sep 17 00:00:00 2001 From: hari9973 Date: Thu, 21 Mar 2019 15:19:39 +0530 Subject: [PATCH 3/5] fixed checkstyle warnings --- .../metrics/client/ApptuitPutClient.java | 11 +- .../metrics/client/XCollectorForwarder.java | 2 - .../metrics/dropwizard/ApptuitReporter.java | 26 +- .../dropwizard/ApptuitReporterFactory.java | 3 +- .../ApptuitConfig.java | 24 +- .../micrometer/ApptuitMeterRegistry.java | 269 ++++++++++++++++++ ...ApptuitMetricsExportAutoConfiguration.java | 32 +-- .../ApptuitProperties.java | 17 +- .../ApptuitPropertiesConfigAdapter.java | 20 +- .../ApptuitMeterRegistry.java | 245 ---------------- .../ApptuitMeterRegistryTest.java | 2 +- ...itMetricsExportAutoConfigurationTests.java | 2 +- .../ApptuitPropertiesConfigAdapterTests.java | 4 +- .../ApptuitPropertiesTests.java | 4 +- 14 files changed, 348 insertions(+), 313 deletions(-) rename micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitConfig.java (70%) create mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistry.java rename micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitMetricsExportAutoConfiguration.java (76%) rename micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitProperties.java (82%) rename micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitPropertiesConfigAdapter.java (69%) delete mode 100644 micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java rename micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitMeterRegistryTest.java (99%) rename micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitMetricsExportAutoConfigurationTests.java (96%) rename micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitPropertiesConfigAdapterTests.java (91%) rename micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/{micrometer_registry_apptuit => micrometer}/ApptuitPropertiesTests.java (88%) diff --git a/client/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java b/client/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java index ae1c6b6..d4a72e9 100644 --- a/client/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java +++ b/client/src/main/java/ai/apptuit/metrics/client/ApptuitPutClient.java @@ -16,7 +16,15 @@ package ai.apptuit.metrics.client; -import java.io.*; +import static ai.apptuit.metrics.client.Sanitizer.DEFAULT_SANITIZER; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -28,7 +36,6 @@ import java.util.logging.Logger; import java.util.zip.GZIPOutputStream; -import static ai.apptuit.metrics.client.Sanitizer.DEFAULT_SANITIZER; /** * @author Rajiv Shivane diff --git a/client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java b/client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java index 6e04e33..23ea267 100644 --- a/client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java +++ b/client/src/main/java/ai/apptuit/metrics/client/XCollectorForwarder.java @@ -1,5 +1,3 @@ - - /* * Copyright 2017 Agilx, Inc. * diff --git a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java index 27bfc38..87d1158 100644 --- a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java @@ -20,7 +20,6 @@ import ai.apptuit.metrics.client.DataPoint; import ai.apptuit.metrics.client.Sanitizer; import ai.apptuit.metrics.client.XCollectorForwarder; -import com.codahale.metrics.Timer; import com.codahale.metrics.Counter; import com.codahale.metrics.Counting; import com.codahale.metrics.Gauge; @@ -31,6 +30,7 @@ import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.ScheduledReporter; import com.codahale.metrics.Snapshot; +import com.codahale.metrics.Timer; import java.math.BigDecimal; import java.math.BigInteger; @@ -131,7 +131,8 @@ public Object call() { timers.forEach(collector::collectTimer); debug("################"); - int numMetrics = gauges.size() + counters.size() + histograms.size() + meters.size() + timers.size(); + int numMetrics = gauges.size() + counters.size() + histograms.size() + + meters.size() + timers.size(); metricsSentCounter.inc(numMetrics); pointsSentCounter.inc(collector.dataPoints.size()); return null; @@ -204,7 +205,8 @@ private void collectCounter(String name, Counter counter) { private void collectHistogram(String name, Histogram histogram) { TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); - collectCounting(rootMetric.submetric("count"), histogram, () -> reportSnapshot(rootMetric, histogram.getSnapshot())); + collectCounting(rootMetric.submetric("count"), histogram, + () -> reportSnapshot(rootMetric, histogram.getSnapshot())); } private void collectMeter(String name, Meter meter) { @@ -237,12 +239,18 @@ private void reportSnapshot(TagEncodedMetricName metric, Snapshot snapshot) { addDataPoint(metric.submetric("max"), convertDuration(snapshot.getMax())); addDataPoint(metric.submetric("mean"), convertDuration(snapshot.getMean())); addDataPoint(metric.submetric("stddev"), convertDuration(snapshot.getStdDev())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.5"), convertDuration(snapshot.getMedian())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.75"), convertDuration(snapshot.get75thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.95"), convertDuration(snapshot.get95thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.98"), convertDuration(snapshot.get98thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.99"), convertDuration(snapshot.get99thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.999"), convertDuration(snapshot.get999thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.5"), + convertDuration(snapshot.getMedian())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.75"), + convertDuration(snapshot.get75thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.95"), + convertDuration(snapshot.get95thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.98"), + convertDuration(snapshot.get98thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.99"), + convertDuration(snapshot.get99thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.999"), + convertDuration(snapshot.get999thPercentile())); } private void reportMetered(TagEncodedMetricName metric, Metered meter) { diff --git a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java index 866c2ae..5633ee6 100644 --- a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java @@ -133,7 +133,8 @@ public MetricFilter getFilter() { // Include the metric if its name is not excluded and its name is included // Where, by default, with no includes setting, all names are included. return !stringMatchingStrategy.containsMatch(getExcludes(), name) - && (getIncludes().isEmpty() || stringMatchingStrategy.containsMatch(getIncludes(), name)); + && (getIncludes().isEmpty() + || stringMatchingStrategy.containsMatch(getIncludes(), name)); }; } diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitConfig.java similarity index 70% rename from micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java rename to micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitConfig.java index c125f2f..dbdadbb 100644 --- a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitConfig.java +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitConfig.java @@ -14,23 +14,23 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import io.micrometer.core.instrument.step.StepRegistryConfig; public interface ApptuitConfig extends StepRegistryConfig { - ApptuitConfig DEFAULT = k -> null; + ApptuitConfig DEFAULT = k -> null; - String get(String key); + String get(String key); - default String prefix() { - return "apptuit"; - } - - default String token() { - String v = get(prefix() + ".token"); - return v == null ? "" : v; - } -} + default String prefix() { + return "apptuit"; + } + + default String token() { + String v = get(prefix() + ".token"); + return v == null ? "" : v; + } +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistry.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistry.java new file mode 100644 index 0000000..4b7c987 --- /dev/null +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistry.java @@ -0,0 +1,269 @@ +/* + * Copyright 2017 Agilx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.apptuit.metrics.micrometer; + +import ai.apptuit.metrics.client.ApptuitPutClient; +import ai.apptuit.metrics.client.DataPoint; +import ai.apptuit.metrics.client.Sanitizer; +import ai.apptuit.metrics.dropwizard.TagEncodedMetricName; +import io.micrometer.core.instrument.Clock; +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.DistributionSummary; +import io.micrometer.core.instrument.FunctionCounter; +import io.micrometer.core.instrument.FunctionTimer; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.LongTaskTimer; +import io.micrometer.core.instrument.Measurement; +import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Timer; +import io.micrometer.core.instrument.distribution.HistogramSnapshot; +import io.micrometer.core.instrument.distribution.ValueAtPercentile; +import io.micrometer.core.instrument.step.StepMeterRegistry; +import io.micrometer.core.instrument.util.MeterPartition; +import io.micrometer.core.instrument.util.NamedThreadFactory; +import io.micrometer.core.instrument.util.TimeUtils; +import io.micrometer.core.lang.NonNull; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ApptuitMeterRegistry extends StepMeterRegistry { + private static final ThreadFactory DEFAULT_THREAD_FACTORY = + new NamedThreadFactory("apptuit-metrics-publisher"); + + private final Logger logger = LoggerFactory.getLogger(ApptuitMeterRegistry.class); + + private final ApptuitConfig config; + + private final Map globalTagMap = new HashMap(); + + private ApptuitPutClient putClient; + + @SuppressWarnings("deprecation") + public ApptuitMeterRegistry(ApptuitConfig config, Clock clock) { + this(config, clock, DEFAULT_THREAD_FACTORY); + } + + private ApptuitMeterRegistry(ApptuitConfig config, Clock clock, ThreadFactory threadFactory) { + super(config, clock); + this.config = config; + putClient = getClient(); + globalTagMap.put("process", "spring-boot-application"); + start(threadFactory); + } + + public static Builder builder(ApptuitConfig config) { + return new Builder(config); + } + + private final ApptuitPutClient getClient() { + return new ApptuitPutClient(config.token(), globalTagMap); + } + + @Override + public void start(ThreadFactory threadFactory) { + if (config.enabled()) { + logger.info("publishing metrics to apptuit every " + TimeUtils.format(config.step())); + } + super.start(threadFactory); + } + + @Override + protected void publish() { + for (List batch : MeterPartition.partition(this, config.batchSize())) { + DataPointCollector collector = + new DataPointCollector(System.currentTimeMillis() / 1000); + for (int i = 0;i < batch.size();i++) { + if (batch.get(i) instanceof Gauge) { + collector.collectGauge(batch.get(i).getId().getName(),(Gauge)batch.get(i)); + } else if (batch.get(i) instanceof Counter) { + collector.collectCounter(batch.get(i).getId().getName(),(Counter)batch.get(i)); + } else if (batch.get(i) instanceof DistributionSummary) { + collector.collectHistogram(batch.get(i).getId().getName(), + (DistributionSummary)batch.get(i)); + } else if (batch.get(i) instanceof Timer) { + collector.collectTimer(batch.get(i).getId().getName(),(Timer)batch.get(i)); + } else if (batch.get(i) instanceof LongTaskTimer) { + collector.collectLongTaskTimer(batch.get(i).getId().getName(), + (LongTaskTimer)batch.get(i)); + } else if (batch.get(i) instanceof FunctionTimer) { + collector.collectFunctionTimer(batch.get(i).getId().getName(), + (FunctionTimer)batch.get(i)); + } else if (batch.get(i) instanceof FunctionCounter) { + collector.collectFunctionCounter(batch.get(i).getId().getName(), + (FunctionCounter)batch.get(i)); + } else if (batch.get(i) instanceof Meter) { + collector.collectMeter(batch.get(i).getId().getName(),(Meter)batch.get(i)); + } + } + try { + putClient.put(collector.dataPoints,Sanitizer.PROMETHEUS_SANITIZER); + } catch (Throwable e) { + logger.error("failed to send metrics to apptuit"); + } + } + } + + protected class DataPointCollector { + + private final long epoch; + protected final List dataPoints; + + DataPointCollector(long epoch) { + this.epoch = epoch; + this.dataPoints = new LinkedList<>(); + } + + private String addBaseUnitToName(String name,Meter m) { + if (m.getId().getBaseUnit() != null) { + return name + "." + m.getId().getBaseUnit(); + } + return name; + } + + protected void collectGauge(String name, Gauge gauge) { + Double value = gauge.value(); + if (Double.isFinite(value)) { + addDataPoint(addBaseUnitToName(name,gauge), + ((Number) value).doubleValue(),gauge.getId().getTags()); + } + } + + protected void collectCounter(String name, Counter counter) { + addDataPoint(name + ".total", counter.count(),counter.getId().getTags()); + } + + protected void collectFunctionCounter(String name,FunctionCounter counter) { + addDataPoint(name + ".total", counter.count(),counter.getId().getTags()); + } + + protected void collectHistogram(String name, DistributionSummary histogram) { + TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); + reportSnapshot(rootMetric, histogram.takeSnapshot(),histogram.getId().getTags()); + } + + protected void collectTimer(String name, Timer timer) { + TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); + reportSnapshot(rootMetric, timer.takeSnapshot(),timer.getId().getTags()); + } + + protected void collectLongTaskTimer(String name, LongTaskTimer longTimer) { + addDataPoint(name + ".activeTasks",(double)longTimer.activeTasks(), + longTimer.getId().getTags()); + addDataPoint(name + "." + getBaseTimeUnit().toString().toLowerCase() + ".duration", + longTimer.duration(getBaseTimeUnit()),longTimer.getId().getTags()); + } + + protected void collectFunctionTimer(String name,FunctionTimer funcTimer) { + addDataPoint(name + "." + getBaseTimeUnit().toString().toLowerCase() + ".count", + funcTimer.count(), funcTimer.getId().getTags()); + addDataPoint(name + "." + getBaseTimeUnit().toString().toLowerCase() + ".sum", + funcTimer.totalTime(getBaseTimeUnit()), funcTimer.getId().getTags()); + addDataPoint(name + "." + getBaseTimeUnit().toString().toLowerCase() + ".mean", + funcTimer.mean(getBaseTimeUnit()), funcTimer.getId().getTags()); + } + + protected void collectMeter(String name,Meter meter) { + for (Measurement measurement : meter.measure()) { + this.addDataPoint(name + "." + measurement.getStatistic().toString().toLowerCase(), + measurement.getValue(), meter.getId().getTags()); + } + } + + private void reportSnapshot(TagEncodedMetricName metric, HistogramSnapshot snapshot, + List tags) { + addDataPoint(metric.submetric(getBaseTimeUnit() + .toString() + .toLowerCase() + ".duration.max"), + snapshot.max(getBaseTimeUnit()), tags); + addDataPoint(metric.submetric(getBaseTimeUnit() + .toString() + .toLowerCase() + ".duration.mean"), + snapshot.mean(getBaseTimeUnit()),tags); + addDataPoint(metric.submetric(getBaseTimeUnit() + .toString() + .toLowerCase() + ".duration.count"), + snapshot.total(),tags); + ValueAtPercentile[] percentValues = snapshot.percentileValues(); + for (ValueAtPercentile obj: percentValues) { + addDataPoint(metric.submetric(getBaseTimeUnit() + .toString() + .toLowerCase() + ".duration") + .withTags("quantile", Double.toString(obj.percentile())), + obj.value(),tags); + } + } + + private void addDataPoint(String name, double value,List tags) { + addDataPoint(TagEncodedMetricName.decode(name), value,tags); + } + + private void addDataPoint(TagEncodedMetricName name, Number value,List tags) { + Map tag = new HashMap(); + for (Tag obj:tags) { + tag.put(obj.getKey(), obj.getValue()); + } + DataPoint dataPoint; + if (tag.isEmpty()) { + dataPoint = new DataPoint(name.getMetricName(), epoch, value, name.getTags()); + } else { + dataPoint = new DataPoint(name.getMetricName(), epoch, value, tag); + } + dataPoints.add(dataPoint); + //logger.error(dataPoint.getMetric()+dataPoint.getTags()+dataPoint.getValue()); + } + } + + @Override + @NonNull + protected TimeUnit getBaseTimeUnit() { + return TimeUnit.MILLISECONDS; + } + + public static class Builder { + private final ApptuitConfig config; + + private Clock clock = Clock.SYSTEM; + + private ThreadFactory threadFactory = DEFAULT_THREAD_FACTORY; + + @SuppressWarnings("deprecation") + Builder(ApptuitConfig config) { + this.config = config; + } + + public Builder clock(Clock clock) { + this.clock = clock; + return this; + } + + public Builder threadFactory(ThreadFactory threadFactory) { + this.threadFactory = threadFactory; + return this; + } + + public ApptuitMeterRegistry build() { + return new ApptuitMeterRegistry(config, clock, threadFactory); + } + } +} diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMetricsExportAutoConfiguration.java similarity index 76% rename from micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java rename to micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMetricsExportAutoConfiguration.java index 947b125..c589ca7 100644 --- a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfiguration.java +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitMetricsExportAutoConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import io.micrometer.core.instrument.Clock; @@ -34,25 +34,25 @@ @Configuration @AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, - SimpleMetricsExportAutoConfiguration.class }) + SimpleMetricsExportAutoConfiguration.class }) @AutoConfigureAfter(MetricsAutoConfiguration.class) @ConditionalOnBean(Clock.class) @ConditionalOnClass(ApptuitMeterRegistry.class) -@ConditionalOnProperty(prefix = "management.metrics.export.apptuit", name = "enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(prefix = "management.metrics.export.apptuit", name = "enabled", + havingValue = "true", matchIfMissing = true) @EnableConfigurationProperties(ApptuitProperties.class) public class ApptuitMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public ApptuitConfig apptuitConfig(ApptuitProperties apptuitProperties) { - return new ApptuitPropertiesConfigAdapter(apptuitProperties); - } - - @Bean - @ConditionalOnMissingBean - public ApptuitMeterRegistry apptuitMeterRegistry(ApptuitConfig apptuitConfig, - Clock clock) { - return new ApptuitMeterRegistry(apptuitConfig, clock); - } - + @Bean + @ConditionalOnMissingBean + public ApptuitConfig apptuitConfig(ApptuitProperties apptuitProperties) { + return new ApptuitPropertiesConfigAdapter(apptuitProperties); + } + + @Bean + @ConditionalOnMissingBean + public ApptuitMeterRegistry apptuitMeterRegistry(ApptuitConfig apptuitConfig, + Clock clock) { + return new ApptuitMeterRegistry(apptuitConfig, clock); + } } \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitProperties.java similarity index 82% rename from micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java rename to micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitProperties.java index e73f6d9..cf57c53 100644 --- a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitProperties.java +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitProperties.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -22,14 +22,13 @@ @ConfigurationProperties(prefix = "management.metrics.export.apptuit") public class ApptuitProperties extends StepRegistryProperties { - private String token = ""; + private String token = ""; - public String gettoken() { - return this.token; - } - - public void settoken(String token) { - this.token = token; - } + public String getToken() { + return this.token; + } + public void setToken(String token) { + this.token = token; + } } \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesConfigAdapter.java similarity index 69% rename from micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java rename to micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesConfigAdapter.java index 3a9acd1..734e53b 100644 --- a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapter.java +++ b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesConfigAdapter.java @@ -14,21 +14,19 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter; - class ApptuitPropertiesConfigAdapter extends - StepRegistryPropertiesConfigAdapter implements ApptuitConfig { - - ApptuitPropertiesConfigAdapter(ApptuitProperties properties) { - super(properties); - } + StepRegistryPropertiesConfigAdapter implements ApptuitConfig { - @Override - public String token() { - return get(ApptuitProperties::gettoken, ApptuitConfig.super::token); - } + ApptuitPropertiesConfigAdapter(ApptuitProperties properties) { + super(properties); + } + @Override + public String token() { + return get(ApptuitProperties::getToken, ApptuitConfig.super::token); + } } \ No newline at end of file diff --git a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java b/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java deleted file mode 100644 index bb1d6c9..0000000 --- a/micrometer-registry-apptuit/src/main/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistry.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright 2017 Agilx, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ai.apptuit.metrics.micrometer_registry_apptuit; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import ai.apptuit.metrics.client.ApptuitPutClient; -import ai.apptuit.metrics.client.DataPoint; -import ai.apptuit.metrics.client.Sanitizer; -import ai.apptuit.metrics.dropwizard.TagEncodedMetricName; -import io.micrometer.core.instrument.Clock; -import io.micrometer.core.instrument.Counter; -import io.micrometer.core.instrument.DistributionSummary; -import io.micrometer.core.instrument.FunctionCounter; -import io.micrometer.core.instrument.FunctionTimer; -import io.micrometer.core.instrument.Gauge; -import io.micrometer.core.instrument.LongTaskTimer; -import io.micrometer.core.instrument.Measurement; -import io.micrometer.core.instrument.Meter; -import io.micrometer.core.instrument.Tag; -import io.micrometer.core.instrument.Timer; -import io.micrometer.core.instrument.distribution.HistogramSnapshot; -import io.micrometer.core.instrument.distribution.ValueAtPercentile; -import io.micrometer.core.instrument.step.StepMeterRegistry; -import io.micrometer.core.instrument.util.MeterPartition; -import io.micrometer.core.instrument.util.NamedThreadFactory; -import io.micrometer.core.instrument.util.TimeUtils; -import io.micrometer.core.lang.NonNull; - -public class ApptuitMeterRegistry extends StepMeterRegistry { - private static final ThreadFactory DEFAULT_THREAD_FACTORY = new NamedThreadFactory("apptuit-metrics-publisher"); - - private final Logger logger = LoggerFactory.getLogger(ApptuitMeterRegistry.class); - - private final ApptuitConfig config; - - private final Map GLOBAL_TAGS_MAP = new HashMap(); - - private ApptuitPutClient putClient; - - public DataPointCollector testing = new DataPointCollector(System.currentTimeMillis() / 1000); - - @SuppressWarnings("deprecation") - public ApptuitMeterRegistry(ApptuitConfig config, Clock clock) { - this(config, clock, DEFAULT_THREAD_FACTORY); - } - - private ApptuitMeterRegistry(ApptuitConfig config, Clock clock, ThreadFactory threadFactory) { - super(config, clock); - this.config = config; - putClient = getClient(); - GLOBAL_TAGS_MAP.put("process", "spring-boot-application"); - start(threadFactory); - } - - public static Builder builder(ApptuitConfig config) { - return new Builder(config); - } - - private final ApptuitPutClient getClient(){ - return new ApptuitPutClient(config.token(), GLOBAL_TAGS_MAP); - } - - @Override - public void start(ThreadFactory threadFactory) { - if (config.enabled()) { - logger.info("publishing metrics to apptuit every " + TimeUtils.format(config.step())); - } - super.start(threadFactory); - } - - @Override - protected void publish() { - for (List batch : MeterPartition.partition(this, config.batchSize())) { - DataPointCollector collector = new DataPointCollector(System.currentTimeMillis() / 1000); - for(int i=0;i dataPoints; - - DataPointCollector(long epoch) { - this.epoch = epoch; - this.dataPoints = new LinkedList<>(); - } - - private String addBaseUnitToName(String name,Meter m) { - if(m.getId().getBaseUnit() != null) { - return name+"."+m.getId().getBaseUnit(); - } - return name; - } - - protected void collectGauge(String name, Gauge gauge) { - Double value = gauge.value(); - if (Double.isFinite(value)) { - addDataPoint(addBaseUnitToName(name,gauge), ((Number) value).doubleValue(),gauge.getId().getTags()); - } - } - - protected void collectCounter(String name, Counter counter) { - addDataPoint(name+".total", counter.count(),counter.getId().getTags()); - } - - protected void collectFunctionCounter(String name,FunctionCounter counter) { - addDataPoint(name+".total", counter.count(),counter.getId().getTags()); - } - - protected void collectHistogram(String name, DistributionSummary histogram) { - TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); - reportSnapshot(rootMetric, histogram.takeSnapshot(),histogram.getId().getTags()); - } - - protected void collectTimer(String name, Timer timer) { - TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); - reportSnapshot(rootMetric, timer.takeSnapshot(),timer.getId().getTags()); - } - - protected void collectLongTaskTimer(String name, LongTaskTimer longTimer) { - addDataPoint(name+".activeTasks",(double)longTimer.activeTasks(),longTimer.getId().getTags()); - addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".duration",longTimer.duration(getBaseTimeUnit()),longTimer.getId().getTags()); - } - protected void collectFunctionTimer(String name,FunctionTimer funcTimer) { - addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".count",funcTimer.count(),funcTimer.getId().getTags()); - addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".sum",funcTimer.totalTime(getBaseTimeUnit()),funcTimer.getId().getTags()); - addDataPoint(name+"."+getBaseTimeUnit().toString().toLowerCase()+".mean",funcTimer.mean(getBaseTimeUnit()),funcTimer.getId().getTags()); - } - - protected void collectMeter(String name,Meter meter) { - for (Measurement measurement : meter.measure()) { - this.addDataPoint(name+"."+measurement.getStatistic().toString().toLowerCase(), measurement.getValue(), meter.getId().getTags()); - } - } - - private void reportSnapshot(TagEncodedMetricName metric, HistogramSnapshot snapshot,List tags) { - addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration.max"), snapshot.max(getBaseTimeUnit()),tags); - addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration.mean"), snapshot.mean(getBaseTimeUnit()),tags); - addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration.count"), snapshot.total(),tags); - ValueAtPercentile[] percentValues = snapshot.percentileValues(); - for(ValueAtPercentile obj: percentValues){ - addDataPoint(metric.submetric(getBaseTimeUnit().toString().toLowerCase()+".duration").withTags("quantile", Double.toString(obj.percentile())), - obj.value(),tags); - } - } - - private void addDataPoint(String name, double value,List tags) { - addDataPoint(TagEncodedMetricName.decode(name), value,tags); - } - - private void addDataPoint(TagEncodedMetricName name, Number value,List tags) { - Map tag = new HashMap(); - for(Tag obj:tags) { - tag.put(obj.getKey(), obj.getValue()); - } - DataPoint dataPoint; - if(tag.isEmpty()) { - dataPoint = new DataPoint(name.getMetricName(), epoch, value, name.getTags()); - } else { - dataPoint = new DataPoint(name.getMetricName(), epoch, value, tag); - } - dataPoints.add(dataPoint); - //logger.error(dataPoint.getMetric()+dataPoint.getTags()+dataPoint.getValue()); - } - } - - @Override - @NonNull - protected TimeUnit getBaseTimeUnit() { - return TimeUnit.MILLISECONDS; - } - - public static class Builder { - private final ApptuitConfig config; - - private Clock clock = Clock.SYSTEM; - private ThreadFactory threadFactory = DEFAULT_THREAD_FACTORY; - - @SuppressWarnings("deprecation") - Builder(ApptuitConfig config) { - this.config = config; - } - - public Builder clock(Clock clock) { - this.clock = clock; - return this; - } - - public Builder threadFactory(ThreadFactory threadFactory) { - this.threadFactory = threadFactory; - return this; - } - - public ApptuitMeterRegistry build() { - return new ApptuitMeterRegistry(config, clock, threadFactory); - } - } -} diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistryTest.java similarity index 99% rename from micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java rename to micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistryTest.java index 8063f02..aa2ad58 100644 --- a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMeterRegistryTest.java +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitMeterRegistryTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import static org.junit.Assert.assertEquals; diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitMetricsExportAutoConfigurationTests.java similarity index 96% rename from micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java rename to micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitMetricsExportAutoConfigurationTests.java index 1077d66..27de83b 100644 --- a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitMetricsExportAutoConfigurationTests.java +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitMetricsExportAutoConfigurationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import org.junit.Test; diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesConfigAdapterTests.java similarity index 91% rename from micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java rename to micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesConfigAdapterTests.java index 2a128b7..5746234 100644 --- a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesConfigAdapterTests.java +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesConfigAdapterTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import org.junit.Test; @@ -25,7 +25,7 @@ public class ApptuitPropertiesConfigAdapterTests { @Test public void whenPropertiesTokenIsSetAdapterTokenReturnsIt() { ApptuitProperties properties = new ApptuitProperties(); - properties.settoken("123456"); + properties.setToken("123456"); assertThat(new ApptuitPropertiesConfigAdapter(properties).token()) .isEqualTo("123456"); } diff --git a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesTests.java similarity index 88% rename from micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java rename to micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesTests.java index c190c6e..3557399 100644 --- a/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer_registry_apptuit/ApptuitPropertiesTests.java +++ b/micrometer-registry-apptuit/src/test/java/ai/apptuit/metrics/micrometer/ApptuitPropertiesTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ai.apptuit.metrics.micrometer_registry_apptuit; +package ai.apptuit.metrics.micrometer; import org.junit.Test; @@ -26,6 +26,6 @@ public class ApptuitPropertiesTests { public void defaultValuesAreConsistent() { ApptuitProperties properties = new ApptuitProperties(); ApptuitConfig config = ApptuitConfig.DEFAULT; - assertThat(properties.gettoken()).isEqualTo(config.token()); + assertThat(properties.getToken()).isEqualTo(config.token()); } } \ No newline at end of file From 4d2ca37bd5e508263011bef1e3f5e14fe2b6ffe1 Mon Sep 17 00:00:00 2001 From: hari9973 Date: Mon, 1 Apr 2019 17:32:56 +0530 Subject: [PATCH 4/5] fixed site command --- .../metrics/dropwizard/ApptuitReporter.java | 34 +++++++------------ .../dropwizard/ApptuitReporterFactory.java | 15 ++++---- .../dropwizard/ApptuitReporterTest.java | 2 +- micrometer-registry-apptuit/pom.xml | 11 +++--- pom.xml | 25 +++++++++----- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java index 87d1158..cbc62de 100644 --- a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java @@ -20,6 +20,7 @@ import ai.apptuit.metrics.client.DataPoint; import ai.apptuit.metrics.client.Sanitizer; import ai.apptuit.metrics.client.XCollectorForwarder; +import com.codahale.metrics.Timer; import com.codahale.metrics.Counter; import com.codahale.metrics.Counting; import com.codahale.metrics.Gauge; @@ -30,7 +31,6 @@ import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.ScheduledReporter; import com.codahale.metrics.Snapshot; -import com.codahale.metrics.Timer; import java.math.BigDecimal; import java.math.BigInteger; @@ -131,8 +131,7 @@ public Object call() { timers.forEach(collector::collectTimer); debug("################"); - int numMetrics = gauges.size() + counters.size() + histograms.size() - + meters.size() + timers.size(); + int numMetrics = gauges.size() + counters.size() + histograms.size() + meters.size() + timers.size(); metricsSentCounter.inc(numMetrics); pointsSentCounter.inc(collector.dataPoints.size()); return null; @@ -205,8 +204,7 @@ private void collectCounter(String name, Counter counter) { private void collectHistogram(String name, Histogram histogram) { TagEncodedMetricName rootMetric = TagEncodedMetricName.decode(name); - collectCounting(rootMetric.submetric("count"), histogram, - () -> reportSnapshot(rootMetric, histogram.getSnapshot())); + collectCounting(rootMetric.submetric("count"), histogram, () -> reportSnapshot(rootMetric, histogram.getSnapshot())); } private void collectMeter(String name, Meter meter) { @@ -239,27 +237,21 @@ private void reportSnapshot(TagEncodedMetricName metric, Snapshot snapshot) { addDataPoint(metric.submetric("max"), convertDuration(snapshot.getMax())); addDataPoint(metric.submetric("mean"), convertDuration(snapshot.getMean())); addDataPoint(metric.submetric("stddev"), convertDuration(snapshot.getStdDev())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.5"), - convertDuration(snapshot.getMedian())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.75"), - convertDuration(snapshot.get75thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.95"), - convertDuration(snapshot.get95thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.98"), - convertDuration(snapshot.get98thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.99"), - convertDuration(snapshot.get99thPercentile())); - addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.999"), - convertDuration(snapshot.get999thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.5"), convertDuration(snapshot.getMedian())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.75"), convertDuration(snapshot.get75thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.95"), convertDuration(snapshot.get95thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.98"), convertDuration(snapshot.get98thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.99"), convertDuration(snapshot.get99thPercentile())); + addDataPoint(metric.withTags(QUANTILE_TAG_NAME, "0.999"), convertDuration(snapshot.get999thPercentile())); } private void reportMetered(TagEncodedMetricName metric, Metered meter) { addDataPoint(metric.submetric(RATE_SUBMETRIC).withTags(WINDOW_TAG_NAME, "1m"), - convertRate(meter.getOneMinuteRate())); + convertRate(meter.getOneMinuteRate())); addDataPoint(metric.submetric(RATE_SUBMETRIC).withTags(WINDOW_TAG_NAME, "5m"), - convertRate(meter.getFiveMinuteRate())); + convertRate(meter.getFiveMinuteRate())); addDataPoint(metric.submetric(RATE_SUBMETRIC).withTags(WINDOW_TAG_NAME, "15m"), - convertRate(meter.getFifteenMinuteRate())); + convertRate(meter.getFifteenMinuteRate())); //addDataPoint(rootMetric.submetric("rate", "window", "all"), epoch, meter.getMeanRate()); } @@ -292,4 +284,4 @@ private void addDataPoint(TagEncodedMetricName name, Number value) { debug(dataPoint); } } -} +} \ No newline at end of file diff --git a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java index 5633ee6..702683b 100644 --- a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java @@ -36,10 +36,10 @@ public class ApptuitReporterFactory { private static final DefaultStringMatchingStrategy DEFAULT_STRING_MATCHING_STRATEGY = - new DefaultStringMatchingStrategy(); + new DefaultStringMatchingStrategy(); private static final RegexStringMatchingStrategy REGEX_STRING_MATCHING_STRATEGY = - new RegexStringMatchingStrategy(); + new RegexStringMatchingStrategy(); private TimeUnit durationUnit = TimeUnit.MILLISECONDS; @@ -127,22 +127,21 @@ public Sanitizer getSanitizer() { public MetricFilter getFilter() { final StringMatchingStrategy stringMatchingStrategy = getUseRegexFilters() - ? REGEX_STRING_MATCHING_STRATEGY : DEFAULT_STRING_MATCHING_STRATEGY; + ? REGEX_STRING_MATCHING_STRATEGY : DEFAULT_STRING_MATCHING_STRATEGY; return (name, metric) -> { // Include the metric if its name is not excluded and its name is included // Where, by default, with no includes setting, all names are included. return !stringMatchingStrategy.containsMatch(getExcludes(), name) - && (getIncludes().isEmpty() - || stringMatchingStrategy.containsMatch(getIncludes(), name)); + && (getIncludes().isEmpty() || stringMatchingStrategy.containsMatch(getIncludes(), name)); }; } public ScheduledReporter build(MetricRegistry registry) { try { return new ApptuitReporter(registry, getFilter(), getRateUnit(), getDurationUnit(), - globalTags, apiKey, apiUrl != null ? new URL(apiUrl) : null, - reportingMode, sanitizer); + globalTags, apiKey, apiUrl != null ? new URL(apiUrl) : null, + reportingMode, sanitizer); } catch (MalformedURLException e) { throw new IllegalArgumentException(e); } @@ -187,4 +186,4 @@ public boolean containsMatch(Set matchExpressions, String metricName) { } */ } -} +} \ No newline at end of file diff --git a/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java index e8cb88e..66c0f8d 100644 --- a/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java +++ b/dropwizard/src/test/java/ai/apptuit/metrics/dropwizard/ApptuitReporterTest.java @@ -284,4 +284,4 @@ private ScheduledReporter createReporter(ReportingMode mode) { reporter.start(period, TimeUnit.SECONDS); return reporter; } -} +} \ No newline at end of file diff --git a/micrometer-registry-apptuit/pom.xml b/micrometer-registry-apptuit/pom.xml index 2cb2a4d..ec408e1 100644 --- a/micrometer-registry-apptuit/pom.xml +++ b/micrometer-registry-apptuit/pom.xml @@ -21,7 +21,6 @@ limitations under the License. 0.0.1-SNAPSHOT jar micrometer-registry-apptuit - io.micrometer @@ -29,11 +28,6 @@ limitations under the License. 1.1.3 compile - - ai.apptuit - metrics-apptuit-client - 0.0.1-SNAPSHOT - ai.apptuit metrics-apptuit-dropwizard @@ -72,6 +66,11 @@ limitations under the License. 5.1.5.RELEASE test + + org.apache.logging.log4j + log4j-api + 2.9.1 + junit junit diff --git a/pom.xml b/pom.xml index d8e3148..38d1305 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,9 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 ai.apptuit metrics-apptuit @@ -19,7 +21,6 @@ limitations under the License. pom metrics-apptuit https://github.com/ApptuitAI/metrics-apptuit/ - 2017 https://github.com/ApptuitAI/metrics-apptuit @@ -43,8 +44,13 @@ limitations under the License. metrics-apptuit https://api.bintray.com/maven/apptuitai/maven/metrics-apptuit/;publish=1 + + ai.apptuit + metrics-apptuit-dropwizard + 0.0.1-SNAPSHOT + - + client dropwizard @@ -52,9 +58,11 @@ limitations under the License. - 1.8 - 1.8 UTF-8 + 1.8 + 1.8 + + false ${project.build.directory}/${project.build.finalName}.jar @@ -80,7 +88,7 @@ limitations under the License. 2.21.0 4.12 1.7.4 - 0.8.1 + 0.8.3 3.2.6 @@ -110,6 +118,7 @@ limitations under the License. default-restore-instrumented-classes + test restore-instrumented-classes @@ -269,7 +278,6 @@ limitations under the License. - @@ -407,5 +415,4 @@ limitations under the License. - - + \ No newline at end of file From 2672ab4b4b12b26bbc86083fecbdd283c338607a Mon Sep 17 00:00:00 2001 From: hari9973 Date: Mon, 1 Apr 2019 17:47:07 +0530 Subject: [PATCH 5/5] fixed checkstyle warnings --- .../java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java | 2 +- .../ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java index cbc62de..3a34aaf 100644 --- a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporter.java @@ -20,7 +20,6 @@ import ai.apptuit.metrics.client.DataPoint; import ai.apptuit.metrics.client.Sanitizer; import ai.apptuit.metrics.client.XCollectorForwarder; -import com.codahale.metrics.Timer; import com.codahale.metrics.Counter; import com.codahale.metrics.Counting; import com.codahale.metrics.Gauge; @@ -31,6 +30,7 @@ import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.ScheduledReporter; import com.codahale.metrics.Snapshot; +import com.codahale.metrics.Timer; import java.math.BigDecimal; import java.math.BigInteger; diff --git a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java index 702683b..aaee784 100644 --- a/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java +++ b/dropwizard/src/main/java/ai/apptuit/metrics/dropwizard/ApptuitReporterFactory.java @@ -126,8 +126,8 @@ public Sanitizer getSanitizer() { } public MetricFilter getFilter() { - final StringMatchingStrategy stringMatchingStrategy = getUseRegexFilters() - ? REGEX_STRING_MATCHING_STRATEGY : DEFAULT_STRING_MATCHING_STRATEGY; + final StringMatchingStrategy stringMatchingStrategy = + getUseRegexFilters() ? REGEX_STRING_MATCHING_STRATEGY : DEFAULT_STRING_MATCHING_STRATEGY; return (name, metric) -> { // Include the metric if its name is not excluded and its name is included