Skip to content

Commit

Permalink
监控基本实现,留了一些保留字段
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFatRatre committed Nov 7, 2024
1 parent c791fd2 commit ebc5e0e
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,10 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
* ClassName: VTTaskStats
* ClassName: VTExecutorStats
* Package: org.dromara.dynamictp.common.entity
* Description:
* Wanted performance is like:
* "virtual_threads": [
* {
* "id": 1,
* "name": "VirtualThread-1",
* "state": "RUNNABLE",
* "stack_trace": [
* {
* "class": "java.base/java.lang.Thread",
* "method": "lambda$main$0",
* "file": "Main.java",
* "line": 10
* }
* ]
* },
* {
* "id": 2,
* "name": "VirtualThread-2",
* "state": "BLOCKED",
* "stack_trace": [
* {
* "class": "java.base/java.net.SocketInputStream",
* "method": "socketRead0",
* "file": "SocketInputStream.java",
* "line": 61
* }
* ]
* }
*
* @author CYC
* @create 2024/11/4 16:52
Expand All @@ -46,82 +16,83 @@
public class VTExecutorStats extends Metrics {

/**
* 虚拟线程执行器的id
* 虚拟线程执行器名字
*/
private int id;
private String executorName;

/**
* 虚拟线程执行器的名称
* 虚拟线程执行器别名
*/
private String name;
private String executorAliasName;

/**
* 虚拟线程执行器的别名
* 正在执行任务的活跃线程大致总数
*/
private String aliasName;
private int activeCount;

/**
* 虚拟线程执行器所承载的任务
* 大致任务总数
*/
private List<VTTaskStats> tasks;
private long taskCount;

public int getTasksCount() {
return tasks.size();
}
/**
* 执行超时任务数量
*/
private long runTimeoutCount;

@Data
public class VTTaskStats {
/**
* 是否为DtpExecutor
*/
private boolean dynamic;

/**
* 虚拟线程的id
*/
private int id;
/**
* tps
*/
private double tps;

/**
* 虚拟线程的名称
*/
private String name;
/**
* 最大任务耗时
*/
private long maxRt;

/**
* 最小任务耗时
*/
private long minRt;

/**
* 虚拟线程的状态
*/
private String state;
/**
* 任务平均耗时(单位:ms)
*/
private double avg;

/**
* 虚拟线程所承载任务的所有堆栈信息
*/
private List<String> stack;
/**
* 满足50%的任务执行所需的最低耗时
*/
private double tp50;

/**
* 虚拟线程所承载任务的理想的堆栈信息
*/
private StackForGood stackForGood;
/**
* 满足75%的任务执行所需的最低耗时
*/
private double tp75;

/**
* 虚拟线程的任务数
*/
private int taskCount;
/**
* 满足90%的任务执行所需的最低耗时
*/
private double tp90;

@Data
private class StackForGood {
/**
* Wanted stack trace is like:
* "stack_trace": [
* {
* "class": "java.base/java.net.SocketInputStream",
* "method": "socketRead0",
* "file": "SocketInputStream.java",
* "line": 61
* }
* ]
*/
private String clazz;
/**
* 满足95%的任务执行所需的最低耗时
*/
private double tp95;

private String method;
/**
* 满足99%的任务执行所需的最低耗时
*/
private double tp99;

private String file;
/**
* 满足99.9%的任务执行所需的最低耗时
*/
private double tp999;

private String line;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,30 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) {
return poolStats;
}

public static VTExecutorStats toVTTaskMetrics(ExecutorWrapper wrapper) {
VTExecutorStats vtTaskStats = new VTExecutorStats();
public static VTExecutorStats toVTExecutorMetrics(ExecutorWrapper wrapper) {
ExecutorAdapter<?> executor = wrapper.getExecutor();
if (executor == null) {
return null;
}
ThreadPoolStatProvider provider = wrapper.getThreadPoolStatProvider();
PerformanceProvider performanceProvider = provider.getPerformanceProvider();
val performanceSnapshot = performanceProvider.getSnapshotAndReset();
VTExecutorStats executorStats = convertCommonVT(executor);
executorStats.setExecutorName(wrapper.getThreadPoolName());
executorStats.setExecutorAliasName(wrapper.getThreadPoolAliasName());
executorStats.setDynamic(executor instanceof DtpExecutor);

return vtTaskStats;
executorStats.setTps(performanceSnapshot.getTps());
executorStats.setAvg(performanceSnapshot.getAvg());
executorStats.setMaxRt(performanceSnapshot.getMaxRt());
executorStats.setMinRt(performanceSnapshot.getMinRt());
executorStats.setTp50(performanceSnapshot.getTp50());
executorStats.setTp75(performanceSnapshot.getTp75());
executorStats.setTp90(performanceSnapshot.getTp90());
executorStats.setTp95(performanceSnapshot.getTp95());
executorStats.setTp99(performanceSnapshot.getTp99());
executorStats.setTp999(performanceSnapshot.getTp999());
return executorStats;
}

private static ThreadPoolStats convertCommon(ExecutorAdapter<?> executor) {
Expand All @@ -107,4 +127,11 @@ private static ThreadPoolStats convertCommon(ExecutorAdapter<?> executor) {
poolStats.setKeepAliveTime(executor.getKeepAliveTime(TimeUnit.MILLISECONDS));
return poolStats;
}

private static VTExecutorStats convertCommonVT(ExecutorAdapter<?> executor) {
VTExecutorStats executorStats = new VTExecutorStats();
executorStats.setActiveCount(executor.getActiveCount());
executorStats.setTaskCount(executor.getTaskCount());
return executorStats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void collectMetrics(Set<String> executorNames) {
executorNames.forEach(x -> {
ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x);
if(wrapper.isVirtualThreadExecutor()) {
doCollectVTTask(ExecutorConverter.toVTTaskMetrics(wrapper));
doCollectVTTask(ExecutorConverter.toVTExecutorMetrics(wrapper));
} else {
doCollect(ExecutorConverter.toMetrics(wrapper));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.dromara.dynamictp.common.entity.ThreadPoolStats;
import org.dromara.dynamictp.common.em.CollectorTypeEnum;
import org.dromara.dynamictp.common.entity.VTExecutorStats;
import org.dromara.dynamictp.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -33,6 +34,11 @@ public void collect(ThreadPoolStats poolStats) {
log.info("dynamic.tp metrics: {}", JsonUtil.toJson(poolStats));
}

@Override
public void collect(VTExecutorStats vtTaskStats) {
log.info("dynamic.vt metrics: {}", JsonUtil.toJson(vtTaskStats));
}

@Override
public String type() {
return CollectorTypeEnum.INTERNAL_LOGGING.name().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.dromara.dynamictp.common.em.CollectorTypeEnum;
import org.dromara.dynamictp.common.entity.ThreadPoolStats;
import org.dromara.dynamictp.common.entity.VTExecutorStats;
import org.dromara.dynamictp.common.util.JsonUtil;
import org.dromara.dynamictp.logging.LogHelper;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -42,6 +43,16 @@ public void collect(ThreadPoolStats threadPoolStats) {
LogHelper.getMonitorLogger().info("{}", metrics);
}

@Override
public void collect(VTExecutorStats vtTaskStats) {
String metrics = JsonUtil.toJson(vtTaskStats);
if (LogHelper.getMonitorLogger() == null) {
log.error("Cannot find monitor logger...");
return;
}
LogHelper.getMonitorLogger().info("{}", metrics);
}

@Override
public String type() {
return CollectorTypeEnum.LOGGING.name().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
import org.dromara.dynamictp.common.util.CommonUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -74,14 +70,14 @@ public void collect(ThreadPoolStats threadPoolStats) {
}

@Override
public void collect(VTExecutorStats vtTaskStats) {
VTExecutorStats oldStats = (VTExecutorStats) GAUGE_CACHE.get(vtTaskStats.getName());
public void collect(VTExecutorStats vtExecutorStats) {
VTExecutorStats oldStats = (VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName());
if (Objects.isNull(oldStats)) {
GAUGE_CACHE.put(vtTaskStats.getName(), vtTaskStats);
GAUGE_CACHE.put(vtExecutorStats.getExecutorName(), vtExecutorStats);
} else {
BeanUtils.copyProperties(vtTaskStats, oldStats);
BeanUtils.copyProperties(vtExecutorStats, oldStats);
}
gauge((VTExecutorStats) GAUGE_CACHE.get(vtTaskStats.getName()));
gauge((VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName()));
}

@Override
Expand Down Expand Up @@ -124,16 +120,23 @@ public void gauge(ThreadPoolStats poolStats) {
}

private void gauge(VTExecutorStats vtExecutorStats) {
Iterable<Tag> tags = getTags(vtExecutorStats);

Metrics.gauge(metricName("virtual.thread.executor.id"), tags, vtExecutorStats, VTExecutorStats::getId);
Metrics.gauge(metricName("virtual.thread.executor.task.count"), tags, vtExecutorStats, VTExecutorStats::getTasksCount);

vtExecutorStats.getTasks().forEach(vtTaskStats -> {
Metrics.gauge(metricName("task.id"), tags, vtTaskStats, VTExecutorStats.VTTaskStats::getId);

});
Iterable<Tag> tags = getTags(vtExecutorStats);

Metrics.gauge(metricName("active.count"), tags, vtExecutorStats, VTExecutorStats::getActiveCount);
Metrics.gauge(metricName("task.count"), tags, vtExecutorStats, VTExecutorStats::getTaskCount);
Metrics.gauge(metricName("run.timeout.count"), tags, vtExecutorStats, VTExecutorStats::getRunTimeoutCount);

Metrics.gauge(metricName("tps"), tags, vtExecutorStats, VTExecutorStats::getTps);
Metrics.gauge(metricName("completed.task.time.avg"), tags, vtExecutorStats, VTExecutorStats::getAvg);
Metrics.gauge(metricName("completed.task.time.max"), tags, vtExecutorStats, VTExecutorStats::getMaxRt);
Metrics.gauge(metricName("completed.task.time.min"), tags, vtExecutorStats, VTExecutorStats::getMinRt);
Metrics.gauge(metricName("completed.task.time.tp50"), tags, vtExecutorStats, VTExecutorStats::getTp50);
Metrics.gauge(metricName("completed.task.time.tp75"), tags, vtExecutorStats, VTExecutorStats::getTp75);
Metrics.gauge(metricName("completed.task.time.tp90"), tags, vtExecutorStats, VTExecutorStats::getTp90);
Metrics.gauge(metricName("completed.task.time.tp95"), tags, vtExecutorStats, VTExecutorStats::getTp95);
Metrics.gauge(metricName("completed.task.time.tp99"), tags, vtExecutorStats, VTExecutorStats::getTp99);
Metrics.gauge(metricName("completed.task.time.tp999"), tags, vtExecutorStats, VTExecutorStats::getTp999);
}

private static String metricName(String name) {
Expand All @@ -151,9 +154,9 @@ private Iterable<Tag> getTags(ThreadPoolStats poolStats) {

private Iterable<Tag> getTags(VTExecutorStats vtExecutorStats) {
ArrayList<Tag> tags = new ArrayList<>(3);
tags.add(Tag.of(VTE_NAME_TAG, vtExecutorStats.getName()));
tags.add(Tag.of(VTE_NAME_TAG, vtExecutorStats.getExecutorName()));
tags.add(Tag.of(APP_NAME_TAG, CommonUtil.getInstance().getServiceName()));
tags.add(Tag.of(VTE_ALIAS_TAG, Optional.ofNullable(vtExecutorStats.getAliasName()).orElse(vtExecutorStats.getName())));
tags.add(Tag.of(VTE_ALIAS_TAG, Optional.ofNullable(vtExecutorStats.getExecutorAliasName()).orElse(vtExecutorStats.getExecutorName())));
return tags;
}
}
Expand Down
Loading

0 comments on commit ebc5e0e

Please sign in to comment.