Skip to content

Commit b2e1fab

Browse files
authored
Fix flaky application logging test (#9341)
1 parent 91dce58 commit b2e1fab

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactory.java

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected void install(InternalLogger.Factory applicationLoggerFactory) {
4545
while (inMemoryLogStore.currentSize() > 0) {
4646
inMemoryLogStore.flush(applicationLoggerFactory);
4747
}
48+
inMemoryLogStore.setApplicationLoggerFactory(applicationLoggerFactory);
4849

4950
// actually install the application logger - from this point, everything will be logged
5051
// directly through the application logging system

javaagent-internal-logging-application/src/main/java/io/opentelemetry/javaagent/logging/application/InMemoryLogStore.java

+16
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ final class InMemoryLogStore {
2020

2121
private final int limit;
2222

23+
private InternalLogger.Factory applicationLoggerFactory;
24+
2325
InMemoryLogStore(int limit) {
2426
this.limit = limit;
2527
}
2628

2729
void write(InMemoryLog log) {
2830
synchronized (lock) {
31+
// redirect to application logging system if it is already set up
32+
// this is here to ensure that we don't lose any logs when switching from in memory to
33+
// application logging
34+
if (applicationLoggerFactory != null) {
35+
applicationLoggerFactory.create(log.name()).log(log.level(), log.message(), log.error());
36+
return;
37+
}
38+
2939
// just drop the log if hit the limit
3040
if (limit >= 0 && inMemoryLogs.size() >= limit) {
3141
return;
@@ -49,6 +59,12 @@ void flush(InternalLogger.Factory applicationLoggerFactory) {
4959
}
5060
}
5161

62+
void setApplicationLoggerFactory(InternalLogger.Factory applicationLoggerFactory) {
63+
synchronized (lock) {
64+
this.applicationLoggerFactory = applicationLoggerFactory;
65+
}
66+
}
67+
5268
int currentSize() {
5369
synchronized (lock) {
5470
return inMemoryLogs.size();

javaagent-internal-logging-application/src/test/java/io/opentelemetry/javaagent/logging/application/ApplicationLoggerFactoryTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void shouldOnlyInstallTheFirstBridge() {
6060

6161
verify(logStore, times(3)).currentSize();
6262
verify(logStore).flush(applicationLoggerBridge);
63+
verify(logStore).setApplicationLoggerFactory(applicationLoggerBridge);
6364
verify(logStore).freeMemory();
6465

6566
underTest.install(applicationLoggerBridge);

0 commit comments

Comments
 (0)