You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/03/08 09:31:16 UTC

[GitHub] yangbor commented on a change in pull request #572: [SCB-370] Improve latency and max precision

yangbor commented on a change in pull request #572: [SCB-370] Improve latency and max precision
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/572#discussion_r173104487
 
 

 ##########
 File path: metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsUtils.java
 ##########
 @@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+
+package org.apache.servicecomb.metrics.core;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
+
+import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
+import org.apache.servicecomb.foundation.metrics.MetricsConst;
+
+import com.netflix.servo.monitor.MonitorConfig;
+import com.netflix.servo.tag.Tag;
+import com.netflix.servo.tag.TagList;
+import com.netflix.servo.tag.Tags;
+
+public class MetricsUtils {
+
+  private static Map<TimeUnit, Function<Double, Double>> convertFunctions;
+
+  static {
+    //don't use TimeUnit convert method because it will return long and lost decimal part
+    convertFunctions = new HashMap<>();
+    convertFunctions.put(TimeUnit.NANOSECONDS, value -> value);
+    convertFunctions.put(TimeUnit.MICROSECONDS, value -> value * 0.001);
+    convertFunctions.put(TimeUnit.MILLISECONDS, value -> value * 0.001 * 0.001);
+    convertFunctions.put(TimeUnit.SECONDS, value -> value * 0.001 * 0.001 * 0.001);
+  }
+
+  public static Map<String, Double> convertMeasurements(Map<MonitorConfig, Double> measurements, TimeUnit unit) {
+    if (validateTimeUnit(unit)) {
+      Map<String, Double> metrics = new HashMap<>();
+      for (Entry<MonitorConfig, Double> measurement : measurements.entrySet()) {
+        if (measurement.getKey().getTags().containsKey(MetricsConst.TAG_UNIT)) {
+          metrics.put(getMonitorKey(
+              measurement.getKey().withAdditionalTag(Tags.newTag(MetricsConst.TAG_UNIT, String.valueOf(unit)))),
+              convertFunctions.get(unit).apply(measurement.getValue()));
+        } else {
+          metrics.put(getMonitorKey(measurement.getKey()), measurement.getValue());
+        }
+      }
+      return metrics;
+    }
+    //no need support MINUTES,HOURS,DAYS because latency under this unit is unsuitable
+    throw new ServiceCombException(
+        "illegal unit : " + String.valueOf(unit) + ", only support NANOSECONDS,MICROSECONDS,MILLISECONDS and SECONDS");
 
 Review comment:
   Metrics calculated in minutes or even hours is normal, we should support at least to hours.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services