Browse Source

Fix flaky application logging test (#9341)

Lauri Tulmin 1 year ago
parent
commit
b2e1fabc9f

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

@@ -45,6 +45,7 @@ final class ApplicationLoggerFactory extends ApplicationLoggerBridge
     while (inMemoryLogStore.currentSize() > 0) {
       inMemoryLogStore.flush(applicationLoggerFactory);
     }
+    inMemoryLogStore.setApplicationLoggerFactory(applicationLoggerFactory);
 
     // actually install the application logger - from this point, everything will be logged
     // directly through the application logging system

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

@@ -20,12 +20,22 @@ final class InMemoryLogStore {
 
   private final int limit;
 
+  private InternalLogger.Factory applicationLoggerFactory;
+
   InMemoryLogStore(int limit) {
     this.limit = limit;
   }
 
   void write(InMemoryLog log) {
     synchronized (lock) {
+      // redirect to application logging system if it is already set up
+      // this is here to ensure that we don't lose any logs when switching from in memory to
+      // application logging
+      if (applicationLoggerFactory != null) {
+        applicationLoggerFactory.create(log.name()).log(log.level(), log.message(), log.error());
+        return;
+      }
+
       // just drop the log if hit the limit
       if (limit >= 0 && inMemoryLogs.size() >= limit) {
         return;
@@ -49,6 +59,12 @@ final class InMemoryLogStore {
     }
   }
 
+  void setApplicationLoggerFactory(InternalLogger.Factory applicationLoggerFactory) {
+    synchronized (lock) {
+      this.applicationLoggerFactory = applicationLoggerFactory;
+    }
+  }
+
   int currentSize() {
     synchronized (lock) {
       return inMemoryLogs.size();

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

@@ -60,6 +60,7 @@ class ApplicationLoggerFactoryTest {
 
     verify(logStore, times(3)).currentSize();
     verify(logStore).flush(applicationLoggerBridge);
+    verify(logStore).setApplicationLoggerFactory(applicationLoggerBridge);
     verify(logStore).freeMemory();
 
     underTest.install(applicationLoggerBridge);