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;
}
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);
}
}
}
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);
}
}
}
Aggregations