From 341b6426f06c07ef3cbd2cb74b7386348dbfadff Mon Sep 17 00:00:00 2001 From: Carpenter Lee Date: Mon, 11 Mar 2019 18:25:45 +0800 Subject: [PATCH] Record total inbound traffic data in metric file (#555) - Regard the total inbound as a "virtual node" with the name `__total_inbound_traffic__` Signed-off-by: Carpenter Lee --- .../com/alibaba/csp/sentinel/Constants.java | 2 ++ .../node/metric/MetricTimerListener.java | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java index 45d46852..bc022f07 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java @@ -38,6 +38,8 @@ public final class Constants { public final static String ROOT_ID = "machine-root"; public final static String CONTEXT_DEFAULT_NAME = "sentinel_default_context"; + public final static String TOTAL_IN_RESOURCE_NAME = "__total_inbound_traffic__"; + public final static DefaultNode ROOT = new EntranceNode(new StringResourceWrapper(ROOT_ID, EntryType.IN), Env.nodeBuilder.buildClusterNode()); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/metric/MetricTimerListener.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/metric/MetricTimerListener.java index a873a633..4e3d3a1e 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/metric/MetricTimerListener.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/metric/MetricTimerListener.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; +import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.node.ClusterNode; @@ -38,23 +39,13 @@ public class MetricTimerListener implements Runnable { @Override public void run() { Map> maps = new TreeMap>(); - for (Entry e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) { String name = e.getKey().getName(); ClusterNode node = e.getValue(); Map metrics = node.metrics(); - - for (Entry entry : metrics.entrySet()) { - long time = entry.getKey(); - MetricNode metricNode = entry.getValue(); - metricNode.setResource(name); - if (maps.get(time) == null) { - maps.put(time, new ArrayList()); - } - List nodes = maps.get(time); - nodes.add(entry.getValue()); - } + aggregate(maps, metrics, name); } + aggregate(maps, Constants.ENTRY_NODE.metrics(), Constants.TOTAL_IN_RESOURCE_NAME); if (!maps.isEmpty()) { for (Entry> entry : maps.entrySet()) { try { @@ -66,4 +57,17 @@ public class MetricTimerListener implements Runnable { } } + private void aggregate(Map> maps, Map metrics, String resourceName) { + for (Entry entry : metrics.entrySet()) { + long time = entry.getKey(); + MetricNode metricNode = entry.getValue(); + metricNode.setResource(resourceName); + if (maps.get(time) == null) { + maps.put(time, new ArrayList()); + } + List nodes = maps.get(time); + nodes.add(entry.getValue()); + } + } + }