Search in sources :

Example 1 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

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 2 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

the class MetricFetcher method writeMetric.

private void writeMetric(Map<String, MetricEntity> map) {
    if (map.isEmpty()) {
        return;
    }
    Date date = new Date();
    for (MetricEntity entity : map.values()) {
        entity.setGmtCreate(date);
        entity.setGmtModified(date);
    }
    metricStore.saveAll(map.values());
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) Date(java.util.Date)

Example 3 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

the class InMemoryMetricsRepository method listResourcesOfApp.

@Override
public List<String> listResourcesOfApp(String app) {
    List<String> results = new ArrayList<>();
    if (StringUtil.isBlank(app)) {
        return results;
    }
    // resource -> timestamp -> metric
    Map<String, LinkedHashMap<Long, MetricEntity>> resourceMap = allMetrics.get(app);
    if (resourceMap == null) {
        return results;
    }
    final long minTimeMs = System.currentTimeMillis() - 1000 * 60;
    Map<String, MetricEntity> resourceCount = new ConcurrentHashMap<>(32);
    readWriteLock.readLock().lock();
    try {
        for (Entry<String, LinkedHashMap<Long, MetricEntity>> resourceMetrics : resourceMap.entrySet()) {
            for (Entry<Long, MetricEntity> metrics : resourceMetrics.getValue().entrySet()) {
                if (metrics.getKey() < minTimeMs) {
                    continue;
                }
                MetricEntity newEntity = metrics.getValue();
                if (resourceCount.containsKey(resourceMetrics.getKey())) {
                    MetricEntity oldEntity = resourceCount.get(resourceMetrics.getKey());
                    oldEntity.addPassQps(newEntity.getPassQps());
                    oldEntity.addRtAndSuccessQps(newEntity.getRt(), newEntity.getSuccessQps());
                    oldEntity.addBlockQps(newEntity.getBlockQps());
                    oldEntity.addExceptionQps(newEntity.getExceptionQps());
                    oldEntity.addCount(1);
                } else {
                    resourceCount.put(resourceMetrics.getKey(), MetricEntity.copyOf(newEntity));
                }
            }
        }
        // Order by last minute b_qps DESC.
        return resourceCount.entrySet().stream().sorted((o1, o2) -> {
            MetricEntity e1 = o1.getValue();
            MetricEntity e2 = o2.getValue();
            int t = e2.getBlockQps().compareTo(e1.getBlockQps());
            if (t != 0) {
                return t;
            }
            return e2.getPassQps().compareTo(e1.getPassQps());
        }).map(Entry::getKey).collect(Collectors.toList());
    } finally {
        readWriteLock.readLock().unlock();
    }
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

the class InMemoryMetricsRepositoryTest method testSaveAll.

@Test
public void testSaveAll() {
    List<MetricEntity> entities = new ArrayList<>(10000);
    for (int i = 0; i < 10000; i++) {
        MetricEntity entry = new MetricEntity();
        entry.setApp("testSaveAll");
        entry.setResource("testResource" + i);
        entry.setTimestamp(new Date(System.currentTimeMillis()));
        entry.setPassQps(1L);
        entry.setExceptionQps(1L);
        entry.setBlockQps(0L);
        entry.setSuccessQps(1L);
        entities.add(entry);
    }
    inMemoryMetricsRepository.saveAll(entities);
    List<String> result = inMemoryMetricsRepository.listResourcesOfApp("testSaveAll");
    Assert.assertTrue(result.size() == entities.size());
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 5 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project pig by pig-mesh.

the class MetricFetcher method writeMetric.

private void writeMetric(Map<String, MetricEntity> map) {
    if (map.isEmpty()) {
        return;
    }
    Date date = new Date();
    for (MetricEntity entity : map.values()) {
        entity.setGmtCreate(date);
        entity.setGmtModified(date);
    }
    metricStore.saveAll(map.values());
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) Date(java.util.Date)

Aggregations

MetricEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity)28 Date (java.util.Date)18 ConnectException (java.net.ConnectException)10 SocketTimeoutException (java.net.SocketTimeoutException)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 AppInfo (com.alibaba.csp.sentinel.dashboard.discovery.AppInfo)5 MachineInfo (com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo)5 MetricNode (com.alibaba.csp.sentinel.node.metric.MetricNode)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 HttpResponse (org.apache.http.HttpResponse)5 HttpGet (org.apache.http.client.methods.HttpGet)5 LinkedHashMap (java.util.LinkedHashMap)4 ConcurrentLinkedHashMap (com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)1