You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/01/09 06:27:18 UTC

[incubator-servicecomb-java-chassis] 07/13: SCB-85 add round_places format and test

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit ae3788a4710158f653ea1fba7d4feb202c3e43a4
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Fri Dec 29 09:46:14 2017 +0800

    SCB-85 add round_places format and test
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../writefile/SimpleFileContentConvertor.java      | 30 ++++++++-
 .../writefile/SimpleFileContentFormatter.java      |  7 +-
 .../extension/writefile/WriteFileInitializer.java  | 20 ++++--
 .../src/test/java/TestWriteFile.java               | 74 ++++++++++++++++++++++
 .../metrics-write-file-log4j2-springboot/pom.xml   | 13 ----
 5 files changed, 123 insertions(+), 21 deletions(-)

diff --git a/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentConvertor.java b/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentConvertor.java
index 1fbe822..df3f8b1 100644
--- a/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentConvertor.java
+++ b/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentConvertor.java
@@ -17,22 +17,48 @@
 
 package io.servicecomb.metrics.extension.writefile;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
 import org.springframework.stereotype.Component;
 
+import com.netflix.config.DynamicPropertyFactory;
+
 import io.servicecomb.metrics.common.RegistryMetric;
 
 @Component
 public class SimpleFileContentConvertor implements FileContentConvertor {
+
+  private static final String METRICS_ROUND_PLACES = "servicecomb.metrics.round_places";
+
+  private final int doubleRoundPlaces;
+
+  private final String doubleStringFormatter;
+
+  public SimpleFileContentConvertor() {
+    doubleRoundPlaces = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_ROUND_PLACES, 1).get();
+    doubleStringFormatter = "%." + String.valueOf(doubleRoundPlaces) + "f";
+  }
+
   @Override
   public Map<String, String> convert(RegistryMetric registryMetric) {
     Map<String, String> pickedMetrics = new HashMap<>();
-    for (Entry<String,Number> metric : registryMetric.toMap().entrySet()) {
-      pickedMetrics.put(metric.getKey(),String.valueOf(metric.getValue()));
+    for (Entry<String, Number> metric : registryMetric.toMap().entrySet()) {
+      pickedMetrics.put(metric.getKey(), String.format(doubleStringFormatter,
+          round(metric.getValue().doubleValue(), doubleRoundPlaces)));
     }
     return pickedMetrics;
   }
+
+  private double round(double value, int places) {
+    if (!Double.isNaN(value) && !Double.isInfinite(value)) {
+      BigDecimal decimal = new BigDecimal(value);
+      return decimal.setScale(places, RoundingMode.HALF_UP).doubleValue();
+    } else {
+      return 0;
+    }
+  }
 }
diff --git a/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentFormatter.java b/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentFormatter.java
index 8df1349..cc12964 100644
--- a/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentFormatter.java
+++ b/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/SimpleFileContentFormatter.java
@@ -51,13 +51,18 @@ public class SimpleFileContentFormatter implements FileContentFormatter {
     }
 
     //may any problem ?
-    if(RegistryUtils.getServiceRegistry() == null) {
+    if (RegistryUtils.getServiceRegistry() == null) {
       RegistryUtils.init();
     }
     Microservice microservice = RegistryUtils.getMicroservice();
     applicationName = microservice.getAppId() + "." + microservice.getServiceName();
   }
 
+  public SimpleFileContentFormatter(String hostName, String applicationName) {
+    this.hostName = hostName;
+    this.applicationName = applicationName;
+  }
+
   @Override
   public Map<String, String> format(Map<String, String> input) {
     return input.entrySet().stream()
diff --git a/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/WriteFileInitializer.java b/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/WriteFileInitializer.java
index cd37873..f52dc30 100644
--- a/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/WriteFileInitializer.java
+++ b/metrics/metrics-extension/metrics-write-file/src/main/java/io/servicecomb/metrics/extension/writefile/WriteFileInitializer.java
@@ -60,23 +60,33 @@ public class WriteFileInitializer {
     this.dataSource = dataSource;
 
     //may any problem ?
-    if(RegistryUtils.getServiceRegistry() == null) {
+    if (RegistryUtils.getServiceRegistry() == null) {
       RegistryUtils.init();
     }
     Microservice microservice = RegistryUtils.getMicroservice();
-    filePrefix = microservice.getAppId() + "." + microservice.getServiceName() + ".";
+    this.filePrefix = microservice.getAppId() + "." + microservice.getServiceName() + ".";
 
     this.init();
   }
 
-  public void init() {
+  public WriteFileInitializer(MetricsFileWriter fileWriter, FileContentConvertor convertor,
+      FileContentFormatter formatter, DataSource dataSource, String filePrefix) {
+    metricPoll = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_WINDOW_TIME, 5000).get();
+    this.fileWriter = fileWriter;
+    this.convertor = convertor;
+    this.formatter = formatter;
+    this.dataSource = dataSource;
+    this.filePrefix = filePrefix;
+  }
+
+  private void init() {
     final Runnable poller = this::run;
     Executors.newScheduledThreadPool(1)
         .scheduleWithFixedDelay(poller, 0, metricPoll, MILLISECONDS);
   }
 
-  private void run() {
-    RegistryMetric registryMetric = dataSource.getRegistryMetric(0);
+  public void run() {
+    RegistryMetric registryMetric = dataSource.getRegistryMetric();
     Map<String, String> convertedMetrics = convertor.convert(registryMetric);
     Map<String, String> formattedMetrics = formatter.format(convertedMetrics);
 
diff --git a/metrics/metrics-extension/metrics-write-file/src/test/java/TestWriteFile.java b/metrics/metrics-extension/metrics-write-file/src/test/java/TestWriteFile.java
new file mode 100644
index 0000000..0240868
--- /dev/null
+++ b/metrics/metrics-extension/metrics-write-file/src/test/java/TestWriteFile.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import io.servicecomb.metrics.common.CallMetric;
+import io.servicecomb.metrics.common.ConsumerInvocationMetric;
+import io.servicecomb.metrics.common.RegistryMetric;
+import io.servicecomb.metrics.common.SystemMetric;
+import io.servicecomb.metrics.common.TimerMetric;
+import io.servicecomb.metrics.core.publish.DataSource;
+import io.servicecomb.metrics.extension.writefile.SimpleFileContentConvertor;
+import io.servicecomb.metrics.extension.writefile.SimpleFileContentFormatter;
+import io.servicecomb.metrics.extension.writefile.WriteFileInitializer;
+import io.servicecomb.metrics.extension.writefile.config.MetricsFileWriter;
+
+public class TestWriteFile {
+
+  @Test
+  public void test() {
+
+    StringBuilder builder = new StringBuilder();
+
+    SimpleFileContentFormatter formatter = new SimpleFileContentFormatter("localhost", "appId.serviceName");
+    SimpleFileContentConvertor convertor = new SimpleFileContentConvertor();
+
+    MetricsFileWriter writer = (loggerName, filePrefix, content) ->
+        builder.append(loggerName).append(filePrefix).append(content);
+
+    SystemMetric systemMetric = new SystemMetric(50, 10, 1, 2, 3,
+        4, 5, 6, 7, 8);
+
+    Map<String, ConsumerInvocationMetric> consumerInvocationMetricMap = new HashMap<>();
+    consumerInvocationMetricMap.put("A", new ConsumerInvocationMetric("A", "A",
+        new TimerMetric("A1", 1, 2, 3, 4), new CallMetric("A2", 100, 999.44444)));
+
+    consumerInvocationMetricMap.put("B", new ConsumerInvocationMetric("B", "B",
+        new TimerMetric("B1", 1, 2, 3, 4), new CallMetric("B2", 100, 888.66666)));
+
+    RegistryMetric metric = new RegistryMetric(systemMetric, consumerInvocationMetricMap, new HashMap<>());
+
+    DataSource dataSource = Mockito.mock(DataSource.class);
+    Mockito.when(dataSource.getRegistryMetric()).thenReturn(metric);
+
+    WriteFileInitializer writeFileInitializer = new WriteFileInitializer(writer, convertor, formatter, dataSource,
+        "appId.serviceName");
+
+    writeFileInitializer.run();
+
+    String sb = builder.toString();
+
+    Assert.assertTrue(sb.contains("999.4"));
+    Assert.assertTrue(sb.contains("888.7"));
+  }
+}
diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml
index ccb66d2..2e49818 100644
--- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml
+++ b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml
@@ -29,23 +29,10 @@
   <artifactId>metrics-write-file-log4j2-springboot</artifactId>
 
   <dependencies>
-
-    <!--exclude lof4j from spring boot and import log4j2-->
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
-      <exclusions>
-        <exclusion>
-          <groupId>org.springframework.boot</groupId>
-          <artifactId>spring-boot-starter-logging</artifactId>
-        </exclusion>
-      </exclusions>
     </dependency>
-    <dependency>
-      <groupId>org.springframework.boot</groupId>
-      <artifactId>spring-boot-starter-log4j2</artifactId>
-    </dependency>
-
 
     <dependency>
       <groupId>io.servicecomb</groupId>

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.