Skip to content

Commit 3d5a0e2

Browse files
authored
Merge pull request #527 from lostsnow/feature/frequent-log-sampling
reduce frequent log output
2 parents e7e1009 + 6846db4 commit 3d5a0e2

File tree

3 files changed

+111
-42
lines changed

3 files changed

+111
-42
lines changed

dongtai-log/src/main/java/io/dongtai/log/DongTaiLog.java

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.*;
44
import java.text.SimpleDateFormat;
5-
import java.util.Date;
6-
import java.util.TimeZone;
5+
import java.util.*;
6+
import java.util.concurrent.ConcurrentHashMap;
77
import java.util.logging.Level;
88
import java.util.regex.Matcher;
99

@@ -23,9 +23,25 @@ public class DongTaiLog {
2323
private static final int YELLOW = 33;
2424
private static final int BLUE = 34;
2525

26+
// 5min
27+
public static int FREQUENT_INTERVAL = 300000;
28+
2629
private static final String TITLE = "[io.dongtai.iast.agent] ";
2730
private static final String TITLE_COLOR = "[" + colorStr("io.dongtai.iast.agent", BLUE) + "] ";
2831

32+
private static final Set<ErrorCode> RESTRICTED_ERRORS = new HashSet<ErrorCode>(Arrays.asList(
33+
ErrorCode.AGENT_MONITOR_COLLECT_PERFORMANCE_METRICS_FAILED,
34+
ErrorCode.AGENT_MONITOR_CHECK_PERFORMANCE_METRICS_FAILED,
35+
ErrorCode.AGENT_MONITOR_GET_DISK_USAGE_FAILED,
36+
ErrorCode.REPORT_SEND_FAILED,
37+
ErrorCode.REPLAY_REQUEST_FAILED,
38+
ErrorCode.GRAPH_BUILD_AND_REPORT_FAILED,
39+
ErrorCode.TAINT_COMMAND_GET_PARAMETERS_FAILED,
40+
ErrorCode.TAINT_COMMAND_RANGE_PROCESS_FAILED
41+
));
42+
43+
private static final ConcurrentHashMap<ErrorCode, ErrorRecord> ERROR_RECORD_MAP = new ConcurrentHashMap<ErrorCode, ErrorRecord>();
44+
2945
static {
3046
if (System.console() != null && !System.getProperty("os.name").toLowerCase().contains("windows")) {
3147
ENABLE_COLOR = true;
@@ -35,6 +51,35 @@ public class DongTaiLog {
3551
LOG_DIR = IastProperties.getLogDir();
3652
}
3753

54+
private static class ErrorRecord {
55+
private long lastWriteTime;
56+
private int count;
57+
58+
public ErrorRecord() {
59+
this.lastWriteTime = new Date().getTime();
60+
this.count = 0;
61+
}
62+
63+
public boolean needWrite() {
64+
long now = new Date().getTime();
65+
// 5min
66+
return now - this.lastWriteTime > FREQUENT_INTERVAL;
67+
}
68+
69+
public int getCount() {
70+
return this.count;
71+
}
72+
73+
public void incrementCount() {
74+
this.count++;
75+
}
76+
77+
public void rotate() {
78+
this.lastWriteTime = new Date().getTime();
79+
this.count = 0;
80+
}
81+
}
82+
3883
public static void configure(Integer agentId) throws Exception {
3984
ENABLED = IastProperties.isEnabled();
4085
if (!ENABLED) {
@@ -142,7 +187,7 @@ private static String colorStr(String msg, int colorCode) {
142187
return "\033[" + colorCode + "m" + msg + RESET;
143188
}
144189

145-
private static String getPrefix(LogLevel lvl, int code, boolean useColor) {
190+
private static String getPrefix(LogLevel lvl, int code, int cnt, boolean useColor) {
146191
String prefix;
147192
if (useColor) {
148193
prefix = getTime() + TITLE_COLOR + lvl.getColorPrefix();
@@ -154,6 +199,10 @@ private static String getPrefix(LogLevel lvl, int code, boolean useColor) {
154199
prefix += "[" + String.valueOf(code) + "] ";
155200
}
156201

202+
if (cnt > 0) {
203+
prefix += "[occurred " + String.valueOf(cnt) + " times] ";
204+
}
205+
157206
return prefix;
158207
}
159208

@@ -168,11 +217,28 @@ private static String getMessage(String msg, Throwable t) {
168217
return msg;
169218
}
170219

171-
private static void log(LogLevel lvl, int code, String fmt, Object... arguments) {
220+
private static void log(LogLevel lvl, ErrorCode ec, String fmt, Object... arguments) {
172221
if (!canLog(lvl)) {
173222
return;
174223
}
175224

225+
int cnt = 0;
226+
if (RESTRICTED_ERRORS.contains(ec)) {
227+
ErrorRecord er = ERROR_RECORD_MAP.get(ec);
228+
if (er == null) {
229+
ERROR_RECORD_MAP.put(ec, new ErrorRecord());
230+
} else {
231+
if (!er.needWrite()) {
232+
er.incrementCount();
233+
return;
234+
}
235+
236+
cnt = er.getCount();
237+
er.rotate();
238+
}
239+
}
240+
241+
int code = ec.getCode();
176242
Throwable t = null;
177243
String msg = fmt;
178244
if (arguments.length == 1 && arguments[0] instanceof Throwable) {
@@ -192,44 +258,36 @@ private static void log(LogLevel lvl, int code, String fmt, Object... arguments)
192258
if (msg.isEmpty()) {
193259
return;
194260
}
195-
System.out.println(getPrefix(lvl, code, ENABLE_COLOR) + msg);
196-
writeLogToFile(getPrefix(lvl, code, false) + msg, t);
261+
System.out.println(getPrefix(lvl, code, cnt, ENABLE_COLOR) + msg);
262+
writeLogToFile(getPrefix(lvl, code, cnt, false) + msg, t);
197263
}
198264

199265
public static void trace(String fmt, Object... arguments) {
200-
log(LogLevel.TRACE, 0, fmt, arguments);
266+
log(LogLevel.TRACE, ErrorCode.NO_CODE, fmt, arguments);
201267
}
202268

203269
public static void debug(String fmt, Object... arguments) {
204-
log(LogLevel.DEBUG, 0, fmt, arguments);
270+
log(LogLevel.DEBUG, ErrorCode.NO_CODE, fmt, arguments);
205271
}
206272

207273
public static void info(String fmt, Object... arguments) {
208-
log(LogLevel.INFO, 0, fmt, arguments);
209-
}
210-
211-
public static void warn(int code, String fmt, Object... arguments) {
212-
log(LogLevel.WARN, code, fmt, arguments);
274+
log(LogLevel.INFO, ErrorCode.NO_CODE, fmt, arguments);
213275
}
214276

215277
public static void warn(ErrorCode ec, Object... arguments) {
216-
log(LogLevel.WARN, ec.getCode(), ec.getMessage(), arguments);
278+
log(LogLevel.WARN, ec, ec.getMessage(), arguments);
217279
}
218280

219281
public static void warn(String format, Object... arguments) {
220-
log(LogLevel.WARN, 0, format, arguments);
221-
}
222-
223-
public static void error(int code, String fmt, Object... arguments) {
224-
log(LogLevel.ERROR, code, fmt, arguments);
282+
log(LogLevel.WARN, ErrorCode.NO_CODE, format, arguments);
225283
}
226284

227285
public static void error(ErrorCode ec, Object... arguments) {
228-
log(LogLevel.ERROR, ec.getCode(), ec.getMessage(), arguments);
286+
log(LogLevel.ERROR, ec, ec.getMessage(), arguments);
229287
}
230288

231289
public static void error(String format, Object... arguments) {
232-
log(LogLevel.ERROR, 0, format, arguments);
290+
log(LogLevel.ERROR, ErrorCode.NO_CODE, format, arguments);
233291
}
234292

235293
private static String format(String from, Object... arguments) {

dongtai-log/src/main/java/io/dongtai/log/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public enum ErrorCode {
104104
UTIL_TAINT_PARSE_CUSTOM_MODEL_FAILED(20612, "parse custom model {} getter {} failed"),
105105

106106
UNKNOWN(99999, "unknown error"),
107+
NO_CODE(0, "no error code"),
107108
;
108109

109110
private final int code;

dongtai-log/src/test/java/io.dongtai.log/DongTaiLogTest.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,26 +193,6 @@ public void logTest() {
193193
Assert.assertEquals("ERROR log message with exception",
194194
TITLE + "[ERROR] foo, Exception: java.lang.Exception: bar" + LS,
195195
outputStreamCaptor.toString().substring(20));
196-
clear();
197-
DongTaiLog.error(110, "foo {} {}", "bar", "baz");
198-
Assert.assertEquals("ERROR log format", TITLE + "[ERROR] [110] foo bar baz" + LS,
199-
outputStreamCaptor.toString().substring(20));
200-
clear();
201-
DongTaiLog.error(110, "foo {} {}", "bar", "baz", new Exception("bar"));
202-
Assert.assertEquals("ERROR log format with code and exception",
203-
TITLE + "[ERROR] [110] foo bar baz, Exception: java.lang.Exception: bar" + LS,
204-
outputStreamCaptor.toString().substring(20));
205-
206-
clear();
207-
DongTaiLog.error(110, "foo {}", "bar", "baz", new Exception("bar"));
208-
Assert.assertEquals("ERROR log format less with code and exception",
209-
TITLE + "[ERROR] [110] foo bar, Exception: java.lang.Exception: bar" + LS,
210-
outputStreamCaptor.toString().substring(20));
211-
clear();
212-
DongTaiLog.error(110, "foo {} {} {}", "bar", "baz", new Exception("bar"));
213-
Assert.assertEquals("ERROR log format more with code and exception",
214-
TITLE + "[ERROR] [110] foo bar baz {}, Exception: java.lang.Exception: bar" + LS,
215-
outputStreamCaptor.toString().substring(20));
216196

217197
int code;
218198
String fmt;
@@ -243,7 +223,6 @@ public void logTest() {
243223
clear();
244224
DongTaiLog.error(ErrorCode.get("NOT EXISTS"));
245225
code = ErrorCode.UNKNOWN.getCode();
246-
fmt = String.format(ErrorCode.UNKNOWN.getMessage());
247226
Assert.assertEquals("ERROR log with ErrorCode invalid name",
248227
TITLE + "[ERROR] [" + code + "] NOT EXISTS" + LS,
249228
outputStreamCaptor.toString().substring(20));
@@ -253,6 +232,37 @@ public void logTest() {
253232
TITLE + "[ERROR] [" + code + "] NOT EXISTS" + LS,
254233
outputStreamCaptor.toString().substring(20));
255234

235+
// System.setOut(standardOut);
236+
int fi = DongTaiLog.FREQUENT_INTERVAL;
237+
DongTaiLog.FREQUENT_INTERVAL = 3000;
238+
code = ErrorCode.REPORT_SEND_FAILED.getCode();
239+
fmt = String.format(ErrorCode.REPORT_SEND_FAILED.getMessage().replaceAll("\\{\\}", "%s"), "a", "b");
240+
for (int i = 0; i < 8; i++) {
241+
clear();
242+
DongTaiLog.error(ErrorCode.REPORT_SEND_FAILED, "a", "b");
243+
if (i == 0) {
244+
String msg = outputStreamCaptor.toString();
245+
Assert.assertTrue("ERROR log with frequent log " + i, msg.length() > 20);
246+
Assert.assertEquals("ERROR log with frequent log " + i,
247+
TITLE + "[ERROR] [" + code + "] " + fmt + LS,
248+
msg.substring(20));
249+
} else if (i % 3 == 0) {
250+
String msg = outputStreamCaptor.toString();
251+
Assert.assertTrue("ERROR log with frequent log " + i, msg.length() > 20);
252+
Assert.assertEquals("ERROR log with frequent log " + i,
253+
TITLE + "[ERROR] [" + code + "] [occurred 2 times] " + fmt + LS,
254+
outputStreamCaptor.toString().substring(20));
255+
} else {
256+
Assert.assertEquals("ERROR log with frequent log " + i,
257+
"", outputStreamCaptor.toString());
258+
}
259+
try {
260+
Thread.sleep(1000);
261+
} catch (InterruptedException ignore) {
262+
}
263+
}
264+
DongTaiLog.FREQUENT_INTERVAL = fi;
265+
256266
clear();
257267
}
258268
}

0 commit comments

Comments
 (0)