Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1687 Fix native image build with CloudWatchLoggingAppender #1688

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public final class CloudWatchLoggingAppender extends AppenderBase<ILoggingEvent>
private int maxBatchSize = DEFAULT_MAX_BATCH_SIZE;
private String groupName;
private String streamName;
private boolean dispatchOnStart = false;
private volatile boolean dispatchThreadStarted = false;

public int getQueueSize() {
return queueSize;
Expand Down Expand Up @@ -120,6 +122,14 @@ public void setCreateGroupAndStream(boolean createGroupAndStream) {
this.createGroupAndStream = createGroupAndStream;
}

public boolean isDispatchOnStart() {
return dispatchOnStart;
}

public void setDispatchOnStart(boolean dispatchOnStart) {
this.dispatchOnStart = dispatchOnStart;
}

@Override
public void start() {
if (isStarted()) {
Expand Down Expand Up @@ -158,14 +168,21 @@ public void start() {

deque = queueFactory.newLinkedBlockingDeque(queueSize);

if (dispatchOnStart) {
startDispatch();
}
super.start();
}

private void startDispatch() {
task = getContext().getScheduledExecutorService().scheduleAtFixedRate(() -> {
try {
dispatchEvents();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, 0, 100, TimeUnit.MILLISECONDS);
super.start();
dispatchThreadStarted = true;
}

@Override
Expand All @@ -182,6 +199,13 @@ protected void append(ILoggingEvent eventObject) {
if (eventObject == null || !isStarted() || blackListLoggerName.contains(eventObject.getLoggerName())) {
return;
}
if (!dispatchThreadStarted && !dispatchOnStart) {
synchronized(this) {
if (!dispatchThreadStarted) {
startDispatch();
}
}
}

try {
final boolean inserted = deque.offer(eventObject, eventDelayLimit.getMilliseconds(), TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -347,7 +371,7 @@ public boolean detachAppender(String name) {
return false;
}
}

private PutLogEventsResponse putLogs(List<InputLogEvent> logEvents,
String groupName,
String streamName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.micronaut.aws.cloudwatch.logging
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.classic.spi.LoggingEvent
import ch.qos.logback.core.encoder.LayoutWrappingEncoder
import io.micronaut.discovery.ServiceInstance
Expand Down Expand Up @@ -64,6 +65,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
void 'test error queue size equal to 0'() {
when:
appender.queueSize = 0
appender.dispatchOnStart = true
appender.start()

then:
Expand All @@ -75,6 +77,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
when:
appender.queueSize = 100
appender.publishPeriod = 0
appender.dispatchOnStart = true
appender.start()

then:
Expand All @@ -85,6 +88,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
void 'test error max batch size less or equal to 0'() {
when:
appender.maxBatchSize = 0
appender.dispatchOnStart = true
appender.start()

then:
Expand All @@ -97,6 +101,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
appender.queueSize = 100
appender.publishPeriod = 100
appender.encoder = null
appender.dispatchOnStart = true
appender.start()

then:
Expand Down Expand Up @@ -167,6 +172,49 @@ class CloudWatchLoggingAppenderSpec extends Specification {
thrown(UnsupportedOperationException)
}

void 'test start dispatch on start'() {
given:
PollingConditions conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
LoggingEvent event = createEvent("name", Level.INFO, "testMessage", System.currentTimeMillis())

when:
appender.dispatchOnStart = true
appender.start()
appender.doAppend(event)

then:
conditions.eventually {
cloudWatchLogsClient.putLogsRequestList.size() == 1
}
cloudWatchLogsClient.putLogsRequestList.get(0).logEvents()[0].message().contains("testMessage")
}

void 'test start dispatch on append'() {
given:
PollingConditions conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
LoggingEvent event = createEvent("name", Level.INFO, "testMessage", System.currentTimeMillis())

when:
appender.groupName = "testGroup"
appender.streamName = "testStream"
appender.start()

then:
conditions.within(1) {
cloudWatchLogsClient.putLogsRequestList.size() == 0
}

when:
appender.doAppend(event)

then:
conditions.eventually {
cloudWatchLogsClient.putLogsRequestList.size() == 1
}
cloudWatchLogsClient.putLogsRequestList.get(0).logEvents()[0].message().contains("testMessage")
cloudWatchLogsClient.putLogsRequestList.get(0).logStreamName() == "testStream"
}

void 'custom groupName and StreamName'() {
given:
def testGroup = "testGroup"
Expand All @@ -178,6 +226,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
when:
appender.groupName = testGroup
appender.streamName = testStream
appender.dispatchOnStart = true
appender.start()
appender.doAppend(event)

Expand Down