You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by wu...@apache.org on 2018/01/08 03:32:42 UTC

[incubator-skywalking] branch collector/instrument updated: Finish the agent codes. Wait for @peng-yongsheng 's metric requirements.

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

wusheng pushed a commit to branch collector/instrument
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git


The following commit(s) were added to refs/heads/collector/instrument by this push:
     new f13d63a  Finish the agent codes. Wait for @peng-yongsheng 's metric requirements.
f13d63a is described below

commit f13d63a20dd88cba78754b3eeb6ed8c5724a7659
Author: wu-sheng <wu...@foxmail.com>
AuthorDate: Mon Jan 8 11:31:40 2018 +0800

    Finish the agent codes. Wait for @peng-yongsheng 's metric requirements.
---
 .../apm/collector/instrument/MetricTree.java       | 142 ++++++++++++++++++++-
 .../apm/collector/instrument/ReportWriter.java     |   4 +
 .../apm/collector/instrument/ServiceMetric.java    |   1 -
 .../collector/instrument/ServiceMetricTracing.java |  15 ++-
 4 files changed, 159 insertions(+), 3 deletions(-)

diff --git a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/MetricTree.java b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/MetricTree.java
index 9dd1f2f..c8b17f5 100644
--- a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/MetricTree.java
+++ b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/MetricTree.java
@@ -18,8 +18,148 @@
 
 package org.apache.skywalking.apm.collector.instrument;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.skywalking.apm.collector.core.annotations.trace.BatchParameter;
+
 /**
  * @author wusheng
  */
-public class MetricTree {
+public enum MetricTree implements Runnable {
+    INSTANCE;
+
+    private MetricNode root = new MetricNode("/", "/");
+    private ScheduledFuture<?> scheduledFuture;
+    private String lineSeparater = System.getProperty("line.separator");
+
+    MetricTree() {
+        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
+        scheduledFuture = service.scheduleAtFixedRate(this, 60, 60, TimeUnit.SECONDS);
+    }
+
+    synchronized MetricNode lookup(String metricName) {
+        String[] metricSections = metricName.split("/");
+        MetricNode node = root;
+        for (String metricSection : metricSections) {
+            node = node.addChild(metricSection, metricName);
+        }
+        return node;
+    }
+
+    @Override
+    public void run() {
+        root.exchange();
+
+        try {
+            Thread.sleep(5 * 1000);
+        } catch (InterruptedException e) {
+
+        }
+
+        StringBuilder logBuffer = new StringBuilder();
+        root.toOutput(new ReportWriter() {
+            private int stackDepth = 0;
+
+            @Override public void writeMetricName(String name) {
+                for (int i = 0; i < stackDepth; i++) {
+                    logBuffer.append("\t");
+                }
+                logBuffer.append(name).append("").append(lineSeparater);
+            }
+
+            @Override public void writeMetric(String metrics) {
+                for (int i = 0; i < stackDepth; i++) {
+                    logBuffer.append("\t");
+                }
+                logBuffer.append("\t");
+                logBuffer.append(metrics).append("").append(lineSeparater);
+            }
+
+            @Override public void prepare4Child() {
+                stackDepth++;
+            }
+
+            @Override public void finished() {
+                stackDepth--;
+            }
+        });
+    }
+
+    class MetricNode {
+        private String nodeName;
+        private String metricName;
+        private volatile ServiceMetric metric;
+        private List<MetricNode> childs = new LinkedList<>();
+
+        public MetricNode(String nodeName, String metricName) {
+            this.nodeName = nodeName;
+            this.metricName = metricName;
+
+        }
+
+        ServiceMetric getMetric(Method targetMethod) {
+            if (metric == null) {
+                synchronized (nodeName) {
+                    if (metric == null) {
+                        boolean isBatchDetected = false;
+                        if (targetMethod != null) {
+                            Annotation[][] annotations = targetMethod.getParameterAnnotations();
+                            if (annotations != null) {
+                                for (Annotation[] parameterAnnotation : annotations) {
+                                    if (parameterAnnotation != null) {
+                                        for (Annotation annotation : parameterAnnotation) {
+                                            if (annotation.equals(BatchParameter.class)) {
+                                                isBatchDetected = true;
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                        metric = new ServiceMetric(metricName, isBatchDetected);
+                    }
+                }
+            }
+            return metric;
+        }
+
+        MetricNode addChild(String nodeName, String metricName) {
+            MetricNode childNode = new MetricNode(nodeName, metricName);
+            this.childs.add(childNode);
+            return childNode;
+        }
+
+        void exchange() {
+            if (metric != null) {
+                metric.exchangeWindows();
+            }
+            if (childs.size() > 0) {
+                for (MetricNode child : childs) {
+                    child.exchange();
+                }
+            }
+        }
+
+        void toOutput(ReportWriter writer) {
+            writer.writeMetric(nodeName);
+            if (metric != null) {
+                writer.prepare4Child();
+                metric.toOutput(writer);
+                writer.finished();
+            }
+            if (childs.size() > 0) {
+                for (MetricNode child : childs) {
+                    writer.prepare4Child();
+                    child.toOutput(writer);
+                    writer.finished();
+                }
+            }
+        }
+    }
 }
diff --git a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ReportWriter.java b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ReportWriter.java
index 303bccc..cb7e342 100644
--- a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ReportWriter.java
+++ b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ReportWriter.java
@@ -25,4 +25,8 @@ public interface ReportWriter {
     void writeMetricName(String name);
 
     void writeMetric(String metrics);
+
+    void prepare4Child();
+
+    void finished();
 }
diff --git a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetric.java b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetric.java
index e35b895..afae79d 100644
--- a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetric.java
+++ b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetric.java
@@ -44,7 +44,6 @@ public class ServiceMetric {
     }
 
     public void toOutput(ReportWriter writer) {
-        writer.writeMetricName(metricName);
         /**
          * If using A, then B is available and free to output.
          */
diff --git a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetricTracing.java b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetricTracing.java
index 978c4e5..b10e888 100644
--- a/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetricTracing.java
+++ b/apm-collector/apm-collector-instrument/src/main/java/org/apache/skywalking/apm/collector/instrument/ServiceMetricTracing.java
@@ -20,14 +20,17 @@ package org.apache.skywalking.apm.collector.instrument;
 
 import java.lang.reflect.Method;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
 import net.bytebuddy.implementation.bind.annotation.Origin;
 import net.bytebuddy.implementation.bind.annotation.RuntimeType;
 import net.bytebuddy.implementation.bind.annotation.SuperCall;
+import org.apache.skywalking.apm.collector.core.annotations.trace.GraphComputingMetric;
 
 /**
  * @author wu-sheng
  */
 public class ServiceMetricTracing {
+    private volatile ConcurrentHashMap<Method, ServiceMetric> metrics = new ConcurrentHashMap<>();
 
     public ServiceMetricTracing() {
     }
@@ -37,6 +40,15 @@ public class ServiceMetricTracing {
         @SuperCall Callable<?> zuper,
         @Origin Method method
     ) throws Throwable {
+        ServiceMetric metric = this.metrics.get(method);
+        if (metric == null) {
+            GraphComputingMetric annotation = method.getAnnotation(GraphComputingMetric.class);
+            String metricName = annotation.name();
+            MetricTree.MetricNode metricNode = MetricTree.INSTANCE.lookup(metricName);
+            ServiceMetric serviceMetric = metricNode.getMetric(method);
+            metrics.put(method, serviceMetric);
+            metric = serviceMetric;
+        }
         boolean occurError = false;
         long startNano = System.nanoTime();
         long endNano;
@@ -47,7 +59,8 @@ public class ServiceMetricTracing {
             throw t;
         } finally {
             endNano = System.nanoTime();
-            //MetricCollector.INSTANCE.trace(serviceName, method, endNano - startNano, occurError);
+
+            metric.trace(endNano - startNano, occurError);
         }
     }
 }

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