Skip to content

Commit 8af391e

Browse files
committed
Log some instrumentation errors in INFO level
1 parent d0c5472 commit 8af391e

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

ulyp-agent/src/main/java/com/ulyp/agent/util/InstrumentationListener.java

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88
import net.bytebuddy.dynamic.DynamicType;
99
import net.bytebuddy.utility.JavaModule;
1010

11+
import javax.annotation.concurrent.ThreadSafe;
1112
import java.time.Duration;
13+
import java.util.ArrayList;
14+
import java.util.List;
1215
import java.util.concurrent.Executors;
1316
import java.util.concurrent.ScheduledExecutorService;
1417
import java.util.concurrent.TimeUnit;
18+
import java.util.concurrent.atomic.AtomicReference;
1519

16-
import static com.ulyp.core.util.LoggingSettings.LOG_LEVEL_PROPERTY;
17-
20+
/**
21+
* A listener which is called by byte-buddy library
22+
*/
1823
@Slf4j
1924
public class InstrumentationListener implements AgentBuilder.Listener {
2025

2126
private static final Duration ERROR_DUMP_INTERVAL = Duration.ofSeconds(10);
2227

23-
private final InstrumentationErrors errors = new InstrumentationErrors();
28+
private final AtomicReference<Errors> errors = new AtomicReference<>(new Errors());
2429

2530
public InstrumentationListener() {
2631
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
@@ -62,37 +67,54 @@ public void onError(String typeName, ClassLoader classLoader, JavaModule module,
6267
if (LoggingSettings.DEBUG_ENABLED) {
6368
log.debug("Failed to instrument class " + typeName, throwable);
6469
}
65-
errors.onError();
70+
errors.get().onError(typeName, throwable);
71+
}
72+
73+
@Override
74+
public void onComplete(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {
75+
6676
}
6777

6878
private void dumpErrors() {
69-
long errorCount = errors.getErrorCountAndReset();
70-
if (errorCount > 0) {
71-
log.info("There were {} instrumentation errors. You may want to enable " +
72-
"debug log level using {} system prop to check actual errors",
73-
errorCount,
74-
LOG_LEVEL_PROPERTY
75-
);
79+
Errors errors = this.errors.getAndSet(new Errors());
80+
if (!errors.isEmpty()) {
81+
log.info("There were {} instrumentation errors. Some types: {}", errors.errorsCount, errors.errors);
7682
}
7783
}
7884

79-
@Override
80-
public void onComplete(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {
85+
private static class Error {
86+
87+
private final String typeName;
88+
private final Throwable throwable;
89+
90+
private Error(String typeName, Throwable throwable) {
91+
this.typeName = typeName;
92+
this.throwable = throwable;
93+
}
8194

95+
@Override
96+
public String toString() {
97+
return "Error{typeName='" + typeName + '\'' + ", throwable=" + throwable + '}';
98+
}
8299
}
83100

84-
private static class InstrumentationErrors {
101+
@ThreadSafe
102+
private static class Errors {
103+
104+
private static final int MAX_ERRORS_STORED = 10;
85105

86-
private long errorsCount = 0L;
106+
private int errorsCount;
107+
private final List<Error> errors = new ArrayList<>();
87108

88-
public synchronized void onError() {
109+
public synchronized void onError(String typeName, Throwable throwable) {
89110
errorsCount++;
111+
if (errors.size() < MAX_ERRORS_STORED) {
112+
errors.add(new Error(typeName, throwable));
113+
}
90114
}
91115

92-
public synchronized long getErrorCountAndReset() {
93-
long value = errorsCount;
94-
errorsCount = 0L;
95-
return value;
116+
public boolean isEmpty() {
117+
return errorsCount == 0;
96118
}
97119
}
98120
}

0 commit comments

Comments
 (0)