Search in sources :

Example 6 with MetricNode

use of com.alibaba.csp.sentinel.node.metric.MetricNode in project RuoYi-Cloud-Plus by JavaLionLi.

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 metricEntity = map.computeIfAbsent(key, s -> {
                MetricEntity initMetricEntity = new MetricEntity();
                initMetricEntity.setApp(machine.getApp());
                initMetricEntity.setTimestamp(new Date(node.getTimestamp()));
                initMetricEntity.setPassQps(0L);
                initMetricEntity.setBlockQps(0L);
                initMetricEntity.setRtAndSuccessQps(0, 0L);
                initMetricEntity.setExceptionQps(0L);
                initMetricEntity.setCount(0);
                initMetricEntity.setResource(node.getResource());
                return initMetricEntity;
            });
            metricEntity.addPassQps(node.getPassQps());
            metricEntity.addBlockQps(node.getBlockQps());
            metricEntity.addRtAndSuccessQps(node.getRt(), node.getSuccessQps());
            metricEntity.addExceptionQps(node.getExceptionQps());
            metricEntity.addCount(1);
        } 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)

Example 7 with MetricNode

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

the class SendMetricCommandHandler method handle.

@Override
public CommandResponse<String> handle(CommandRequest request) {
    // Note: not thread-safe.
    if (searcher == null) {
        synchronized (lock) {
            String appName = SentinelConfig.getAppName();
            if (appName == null) {
                appName = "";
            }
            if (searcher == null) {
                searcher = new MetricSearcher(MetricWriter.METRIC_BASE_DIR, MetricWriter.formMetricFileName(appName, PidUtil.getPid()));
            }
        }
    }
    String startTimeStr = request.getParam("startTime");
    String endTimeStr = request.getParam("endTime");
    String maxLinesStr = request.getParam("maxLines");
    String identity = request.getParam("identity");
    long startTime = -1;
    int maxLines = 6000;
    if (StringUtil.isNotBlank(startTimeStr)) {
        startTime = Long.parseLong(startTimeStr);
    } else {
        return CommandResponse.ofSuccess("");
    }
    List<MetricNode> list;
    try {
        // Find by end time if set.
        if (StringUtil.isNotBlank(endTimeStr)) {
            long endTime = Long.parseLong(endTimeStr);
            list = searcher.findByTimeAndResource(startTime, endTime, identity);
        } else {
            if (StringUtil.isNotBlank(maxLinesStr)) {
                maxLines = Integer.parseInt(maxLinesStr);
            }
            maxLines = Math.min(maxLines, 12000);
            list = searcher.find(startTime, maxLines);
        }
    } catch (Exception ex) {
        return CommandResponse.ofFailure(new RuntimeException("Error when retrieving metrics", ex));
    }
    if (list == null) {
        list = new ArrayList<>();
    }
    if (StringUtil.isBlank(identity)) {
        addCpuUsageAndLoad(list);
    }
    StringBuilder sb = new StringBuilder();
    for (MetricNode node : list) {
        sb.append(node.toThinString()).append("\n");
    }
    return CommandResponse.ofSuccess(sb.toString());
}
Also used : MetricSearcher(com.alibaba.csp.sentinel.node.metric.MetricSearcher) MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode)

Example 8 with MetricNode

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

the class SendMetricCommandHandler method toNode.

/**
 * transfer the value to a MetricNode, the value will multiply 10000 then truncate
 * to long value, and as the {@link MetricNode#passQps}.
 * <p>
 * This is an eclectic scheme before we have a standard metric format.
 * </p>
 *
 * @param value    value to save.
 * @param ts       timestamp
 * @param resource resource name.
 * @return a MetricNode represents the value.
 */
private MetricNode toNode(double value, long ts, String resource) {
    MetricNode node = new MetricNode();
    node.setPassQps((long) (value * 10000));
    node.setTimestamp(ts);
    node.setResource(resource);
    return node;
}
Also used : MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode)

Example 9 with MetricNode

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

the class StatisticNode method metrics.

@Override
public Map<Long, MetricNode> metrics() {
    // The fetch operation is thread-safe under a single-thread scheduler pool.
    long currentTime = TimeUtil.currentTimeMillis();
    currentTime = currentTime - currentTime % 1000;
    Map<Long, MetricNode> metrics = new ConcurrentHashMap<>();
    List<MetricNode> nodesOfEverySecond = rollingCounterInMinute.details();
    long newLastFetchTime = lastFetchTime;
    // Iterate metrics of all resources, filter valid metrics (not-empty and up-to-date).
    for (MetricNode node : nodesOfEverySecond) {
        if (isNodeInTime(node, currentTime) && isValidMetricNode(node)) {
            metrics.put(node.getTimestamp(), node);
            newLastFetchTime = Math.max(newLastFetchTime, node.getTimestamp());
        }
    }
    lastFetchTime = newLastFetchTime;
    return metrics;
}
Also used : MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 10 with MetricNode

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

the class ArrayMetric method fromBucket.

private MetricNode fromBucket(WindowWrap<MetricBucket> wrap) {
    MetricNode node = new MetricNode();
    node.setBlockQps(wrap.value().block());
    node.setExceptionQps(wrap.value().exception());
    node.setPassQps(wrap.value().pass());
    long successQps = wrap.value().success();
    node.setSuccessQps(successQps);
    if (successQps != 0) {
        node.setRt(wrap.value().rt() / successQps);
    } else {
        node.setRt(wrap.value().rt());
    }
    node.setTimestamp(wrap.windowStart());
    node.setOccupiedPassQps(wrap.value().occupiedPass());
    return node;
}
Also used : MetricNode(com.alibaba.csp.sentinel.node.metric.MetricNode)

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