Search in sources :

Example 11 with MetricNode

use of com.alibaba.csp.sentinel.node.metric.MetricNode in project Sentinel by alibaba.

the class MetricCollector method collectMetric.

/**
 * collect the metrics in {@link MetricNode}.
 *
 * @return the metric grouped by resource name.
 */
public Map<String, MetricNode> collectMetric() {
    final long currentTime = TimeUtil.currentTimeMillis();
    final long maxTime = currentTime - currentTime % 1000;
    final long minTime = maxTime - 1000;
    Map<String, MetricNode> metricNodeMap = new HashMap<>();
    for (Map.Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
        ClusterNode node = e.getValue();
        List<MetricNode> metrics = getLastMetrics(node, minTime, maxTime);
        aggregate(metricNodeMap, metrics, node);
    }
    aggregate(metricNodeMap, getLastMetrics(Constants.ENTRY_NODE, minTime, maxTime), Constants.ENTRY_NODE);
    return metricNodeMap;
}
Also used : ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) HashMap(java.util.HashMap) MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with MetricNode

use of com.alibaba.csp.sentinel.node.metric.MetricNode in project Sentinel by alibaba.

the class MetricBeanWriter method write.

/**
 * write the MetricNode value to MetricBean
 * if the MetricBean is not registered into {@link MBeanRegistry},
 * it will be created and registered into {@link MBeanRegistry}.
 * else it will update the value of MetricBean.
 * Notes. if the MetricNode is null, then {@link MetricBean} will be reset.
 * @param map metricNode value group by resource
 * @throws Exception write failed exception
 */
public synchronized void write(Map<String, MetricNode> map) throws Exception {
    if (map == null || map.isEmpty()) {
        List<MetricBean> metricNodes = mBeanRegistry.listAllMBeans();
        if (metricNodes == null || metricNodes.isEmpty()) {
            return;
        }
        for (MetricBean metricNode : metricNodes) {
            metricNode.reset();
        }
        return;
    }
    String appName = SentinelConfig.getAppName();
    if (appName == null) {
        appName = DEFAULT_APP_NAME;
    }
    long version = System.currentTimeMillis();
    // set or update the new value
    for (MetricNode metricNode : map.values()) {
        final String mBeanName = "Sentinel:type=" + appName + ",name=\"" + metricNode.getResource() + "\",classification=\"" + metricNode.getClassification() + "\"";
        MetricBean metricBean = mBeanRegistry.findMBean(mBeanName);
        if (metricBean != null) {
            metricBean.setValueFromNode(metricNode);
            metricBean.setVersion(version);
        } else {
            metricBean = new MetricBean();
            metricBean.setValueFromNode(metricNode);
            metricBean.setVersion(version);
            mBeanRegistry.register(metricBean, mBeanName);
            RecordLog.info("[MetricBeanWriter] Registering with JMX as Metric MBean [{}]", mBeanName);
        }
    }
    // reset the old value
    List<MetricBean> metricBeans = mBeanRegistry.listAllMBeans();
    if (metricBeans == null || metricBeans.isEmpty()) {
        return;
    }
    for (MetricBean metricBean : metricBeans) {
        if (!Objects.equals(metricBean.getVersion(), version)) {
            metricBean.reset();
            mBeanRegistry.unRegister(metricBean);
        }
    }
}
Also used : MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode)

Example 13 with MetricNode

use of com.alibaba.csp.sentinel.node.metric.MetricNode in project XHuiCloud by sindaZeng.

the class MetricFetcher method handleBody.

private void handleBody(String[] lines, MachineInfo machine, Map<String, MetricEntity> map) {
    // logger.info("handleBody() lines=" + lines.length + ", machine=" + machine);
    if (lines.length < 1) {
        return;
    }
    for (String line : lines) {
        try {
            MetricNode node = MetricNode.fromThinString(line);
            if (shouldFilterOut(node.getResource())) {
                continue;
            }
            /*
                 * aggregation metrics by app_resource_timeSecond, ignore ip and port.
                 */
            String key = buildMetricKey(machine.getApp(), node.getResource(), node.getTimestamp());
            MetricEntity entity = map.get(key);
            if (entity != null) {
                entity.addPassQps(node.getPassQps());
                entity.addBlockQps(node.getBlockQps());
                entity.addRtAndSuccessQps(node.getRt(), node.getSuccessQps());
                entity.addExceptionQps(node.getExceptionQps());
                entity.addCount(1);
            } else {
                entity = new MetricEntity();
                entity.setApp(machine.getApp());
                entity.setTimestamp(new Date(node.getTimestamp()));
                entity.setPassQps(node.getPassQps());
                entity.setBlockQps(node.getBlockQps());
                entity.setRtAndSuccessQps(node.getRt(), node.getSuccessQps());
                entity.setExceptionQps(node.getExceptionQps());
                entity.setCount(1);
                entity.setResource(node.getResource());
                map.put(key, entity);
            }
        } catch (Exception e) {
            logger.warn("handleBody line exception, machine: {}, line: {}", machine.toLogString(), line);
        }
    }
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode) Date(java.util.Date) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException)

Aggregations

MetricNode (com.alibaba.csp.sentinel.node.metric.MetricNode)13 MetricEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity)5 ConnectException (java.net.ConnectException)5 SocketTimeoutException (java.net.SocketTimeoutException)5 Date (java.util.Date)5 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 MetricSearcher (com.alibaba.csp.sentinel.node.metric.MetricSearcher)1 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1