REQ-3284: 还原配置
This commit is contained in:
parent
fdf35b9f41
commit
d057ef0942
@ -19,8 +19,7 @@ import org.springframework.core.env.Environment;
|
||||
public class DiagnosisProps implements EnvironmentAware {
|
||||
|
||||
private Environment environment;
|
||||
private int sqlResultCountWarningThreshold = 6000;
|
||||
private boolean enableResultCountWarning = true;
|
||||
private SqlResultCount sqlResultCount = new SqlResultCount();
|
||||
|
||||
public String getActiveProfile() {
|
||||
String[] profiles = environment.getActiveProfiles();
|
||||
@ -32,4 +31,12 @@ public class DiagnosisProps implements EnvironmentAware {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
public static class SqlResultCount {
|
||||
private int warningThreshold = 5;
|
||||
private boolean enable = true;
|
||||
private int resetSetWarnCount = 20;
|
||||
}
|
||||
|
||||
}
|
||||
@ -43,6 +43,7 @@ class ProxyResultSet implements ResultSet {
|
||||
|
||||
@Override public void close() throws SQLException {
|
||||
delegate.close();
|
||||
statement.resultSetClosed();
|
||||
}
|
||||
|
||||
@Override public boolean wasNull() throws SQLException {
|
||||
|
||||
@ -20,8 +20,6 @@ import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
@ -39,11 +37,10 @@ class ProxyStatement extends StatementWrapper {
|
||||
private final PreparedStatement rawStatement;
|
||||
private final DiagnosisProps props;
|
||||
private final ListableBeanFactory beanFactory;
|
||||
private final AtomicInteger resultCount = new AtomicInteger(0);
|
||||
private final AtomicBoolean isThresholdWarned = new AtomicBoolean(false);
|
||||
private final AtomicBoolean isCloseWarned = new AtomicBoolean(false);
|
||||
|
||||
private volatile String sql;
|
||||
private int rowCount = 0;
|
||||
private int resultSetCount = 0;
|
||||
private int resetSetWarnCount = 0;
|
||||
|
||||
public ProxyStatement(PreparedStatement rawStatement,
|
||||
DiagnosisProps props,
|
||||
@ -55,31 +52,38 @@ class ProxyStatement extends StatementWrapper {
|
||||
}
|
||||
|
||||
void rowAdvanced() {
|
||||
int currentCount = resultCount.incrementAndGet();
|
||||
maybeWarn("Threshold", isThresholdWarned, currentCount);
|
||||
int currentRowCount = rowCount++;
|
||||
maybeWarn("Threshold", currentRowCount);
|
||||
}
|
||||
|
||||
public void resultSetClosed() {
|
||||
if (rowCount % 2000 == 0 && resetSetWarnCount <= props.getSqlResultCount().getResetSetWarnCount()) {
|
||||
maybeWarn(String.format("ResultSet-%d-%d", resetSetWarnCount, resultSetCount), rowCount);
|
||||
resetSetWarnCount++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet getResultSet() throws SQLException {
|
||||
ResultSet resultSet = super.getResultSet();
|
||||
resultSetCount++;
|
||||
return new ProxyResultSet(this, resultSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
super.close();
|
||||
maybeWarn("Close", isCloseWarned, resultCount.get());
|
||||
maybeWarn("Close", rowCount);
|
||||
}
|
||||
|
||||
private void maybeWarn(String phase, AtomicBoolean warned, int currentCount) {
|
||||
if (currentCount < props.getSqlResultCountWarningThreshold()) return;
|
||||
if (!warned.compareAndSet(false, true)) return;
|
||||
private void maybeWarn(String phase, int rowCount) {
|
||||
if (rowCount < props.getSqlResultCount().getWarningThreshold()) return;
|
||||
try {
|
||||
if (sql == null)
|
||||
sql = determineSQL();
|
||||
EXECUTOR.execute(() -> {
|
||||
try {
|
||||
sendWarn(phase, currentCount, sql);
|
||||
sendWarn(phase, rowCount, sql);
|
||||
} catch (Exception e) {
|
||||
log.warn("[Task] Error sending warning", e);
|
||||
}
|
||||
@ -103,7 +107,7 @@ class ProxyStatement extends StatementWrapper {
|
||||
"环境: " + props.getActiveProfile() + "\n\n" +
|
||||
"TraceId: " + TraceUtils.getOrCreateTraceId() + "\n\n" +
|
||||
"阶段: " + phase + "\n\n" +
|
||||
"阈值数量: " + props.getSqlResultCountWarningThreshold() + "\n\n" +
|
||||
"阈值数量: " + props.getSqlResultCount().getWarningThreshold() + "\n\n" +
|
||||
"当前数量: " + currentCount + "\n\n" +
|
||||
"SQL: " + sql + "\n";
|
||||
dingTaskMessageService.send(title, markdown);
|
||||
|
||||
@ -29,7 +29,7 @@ public class ResultCountInterceptor implements Interceptor {
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
Statement stmt = (Statement)invocation.proceed();
|
||||
if (!props.isEnableResultCountWarning()) return stmt;
|
||||
if (!props.getSqlResultCount().isEnable()) return stmt;
|
||||
if (!(stmt instanceof PreparedStatement)) return stmt;
|
||||
return new ProxyStatement((PreparedStatement)stmt, props, beanFactory);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user