Skip to content

Commit 8a59be6

Browse files
Closes #1407 - Insert events into preloaded logs (#1411)
* buffer artificial log messages for invalidation events * use custom logger name to highlight invalidation events * properly handle uninitialized log preloader * add some hints in the code
1 parent 0699fad commit 8a59be6

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

inspectit-ocelot-core/src/main/java/rocks/inspectit/ocelot/core/selfmonitoring/LogPreloader.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package rocks.inspectit.ocelot.core.selfmonitoring;
22

33
import ch.qos.logback.classic.Level;
4+
import ch.qos.logback.classic.Logger;
45
import ch.qos.logback.classic.spi.ILoggingEvent;
6+
import ch.qos.logback.classic.spi.LoggingEvent;
57
import lombok.extern.slf4j.Slf4j;
8+
import org.slf4j.LoggerFactory;
69
import org.springframework.stereotype.Component;
710
import rocks.inspectit.ocelot.config.model.InspectitConfig;
811
import rocks.inspectit.ocelot.config.model.selfmonitoring.LogPreloadingSettings;
@@ -23,6 +26,10 @@
2326
@Slf4j
2427
public class LogPreloader extends DynamicallyActivatableService implements InternalProcessingAppender.LogEventConsumer {
2528

29+
private static final String LOG_INVALIDATION_EVENT = "{}! Some previous log messages may now be outdated.";
30+
31+
private final Logger invalidationLogger = (Logger) LoggerFactory.getLogger("### LOG-INVALIDATING EVENT ###");
32+
2633
private ILoggingEvent[] buffer;
2734

2835
private final AtomicInteger currentIndex = new AtomicInteger(0);
@@ -41,7 +48,36 @@ public LogPreloader() {
4148
*/
4249
@Override
4350
public void onLoggingEvent(ILoggingEvent event, Class<?> invalidator) {
44-
if (buffer != null && event.getLevel().isGreaterOrEqual(minimumPreloadingLevel)) {
51+
if (event.getLevel().isGreaterOrEqual(minimumPreloadingLevel)) {
52+
recordLoggingEvent(event);
53+
}
54+
}
55+
56+
/**
57+
* Appends the invalidation event as an artificial log message into the buffer.
58+
*
59+
* @param invalidator The invalidator
60+
*/
61+
@Override
62+
public void onInvalidationEvent(Object invalidator) {
63+
if (invalidator != null) {
64+
String invalidationString = invalidator.getClass().getSimpleName();
65+
if (invalidationString.endsWith("Event")) {
66+
// pretty-format event, e.g., transform 'SomethingHappenedEvent' into 'Something happened'
67+
String invalidationLowercase = invalidationString.substring(0, invalidationString.length() - "Event".length())
68+
.replaceAll("([a-z])([A-Z]+)", "$1 $2")
69+
.toLowerCase();
70+
invalidationString = invalidationLowercase.substring(0, 1)
71+
.toUpperCase() + invalidationLowercase.substring(1);
72+
}
73+
74+
ILoggingEvent logEvent = new LoggingEvent(getClass().getName(), invalidationLogger, Level.INFO, LOG_INVALIDATION_EVENT, null, new String[]{invalidationString});
75+
recordLoggingEvent(logEvent);
76+
}
77+
}
78+
79+
private void recordLoggingEvent(ILoggingEvent event) {
80+
if (buffer != null) {
4581
int index = currentIndex.getAndIncrement() % buffer.length;
4682
try {
4783
buffer[index] = event;

inspectit-ocelot-core/src/test/java/rocks/inspectit/ocelot/core/selfmonitoring/LogPreloaderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.slf4j.LoggerFactory;
1313
import rocks.inspectit.ocelot.config.model.InspectitConfig;
1414
import rocks.inspectit.ocelot.config.model.selfmonitoring.LogPreloadingSettings;
15+
import rocks.inspectit.ocelot.core.instrumentation.config.event.InstrumentationConfigurationChangedEvent;
1516

1617
import java.util.stream.IntStream;
1718
import java.util.stream.StreamSupport;
@@ -96,6 +97,16 @@ void logMessagesAndChangeBufferSize() {
9697
.spliterator(), false)).extracting(ILoggingEvent::getLevel).containsOnly(Level.ERROR);
9798
}
9899

100+
@Test
101+
void sendInvalidationEvents() {
102+
logPreloader.onLoggingEvent(warnEvent, null);
103+
logPreloader.onInvalidationEvent(new InstrumentationConfigurationChangedEvent(this, null, null));
104+
105+
assertThat(StreamSupport.stream(logPreloader.getPreloadedLogs().spliterator(), false).count()).isEqualTo(2);
106+
assertThat(StreamSupport.stream(logPreloader.getPreloadedLogs().spliterator(), false)
107+
.map(ILoggingEvent::getFormattedMessage)).allMatch(s -> s.contains("Dummy") || s.contains("Instrumentation configuration changed!"));
108+
}
109+
99110
}
100111

101112
}

0 commit comments

Comments
 (0)