Skip to content

Commit 470b6bb

Browse files
#1687 Fix native image build with CloudWatchLoggingAppender (#1688)
* Fix native image build with CloudWatchLoggingAppender - Start the thread to send logs to cloudwatch only when append command is called, so it is not started during build time - Create dispatchOnStart property to return to previous behavior * Modify CloudWatchLoggingAppender tests
1 parent 06aede0 commit 470b6bb

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

aws-cloudwatch-logging/src/main/java/io/micronaut/aws/cloudwatch/logging/CloudWatchLoggingAppender.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public final class CloudWatchLoggingAppender extends AppenderBase<ILoggingEvent>
6767
private int maxBatchSize = DEFAULT_MAX_BATCH_SIZE;
6868
private String groupName;
6969
private String streamName;
70+
private boolean dispatchOnStart = false;
71+
private volatile boolean dispatchThreadStarted = false;
7072

7173
public int getQueueSize() {
7274
return queueSize;
@@ -120,6 +122,14 @@ public void setCreateGroupAndStream(boolean createGroupAndStream) {
120122
this.createGroupAndStream = createGroupAndStream;
121123
}
122124

125+
public boolean isDispatchOnStart() {
126+
return dispatchOnStart;
127+
}
128+
129+
public void setDispatchOnStart(boolean dispatchOnStart) {
130+
this.dispatchOnStart = dispatchOnStart;
131+
}
132+
123133
@Override
124134
public void start() {
125135
if (isStarted()) {
@@ -158,14 +168,21 @@ public void start() {
158168

159169
deque = queueFactory.newLinkedBlockingDeque(queueSize);
160170

171+
if (dispatchOnStart) {
172+
startDispatch();
173+
}
174+
super.start();
175+
}
176+
177+
private void startDispatch() {
161178
task = getContext().getScheduledExecutorService().scheduleAtFixedRate(() -> {
162179
try {
163180
dispatchEvents();
164181
} catch (InterruptedException e) {
165182
Thread.currentThread().interrupt();
166183
}
167184
}, 0, 100, TimeUnit.MILLISECONDS);
168-
super.start();
185+
dispatchThreadStarted = true;
169186
}
170187

171188
@Override
@@ -182,6 +199,13 @@ protected void append(ILoggingEvent eventObject) {
182199
if (eventObject == null || !isStarted() || blackListLoggerName.contains(eventObject.getLoggerName())) {
183200
return;
184201
}
202+
if (!dispatchThreadStarted && !dispatchOnStart) {
203+
synchronized(this) {
204+
if (!dispatchThreadStarted) {
205+
startDispatch();
206+
}
207+
}
208+
}
185209

186210
try {
187211
final boolean inserted = deque.offer(eventObject, eventDelayLimit.getMilliseconds(), TimeUnit.MILLISECONDS);
@@ -347,7 +371,7 @@ public boolean detachAppender(String name) {
347371
return false;
348372
}
349373
}
350-
374+
351375
private PutLogEventsResponse putLogs(List<InputLogEvent> logEvents,
352376
String groupName,
353377
String streamName,

aws-cloudwatch-logging/src/test/groovy/io/micronaut/aws/cloudwatch/logging/CloudWatchLoggingAppenderSpec.groovy

+49
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.micronaut.aws.cloudwatch.logging
33
import ch.qos.logback.classic.Level
44
import ch.qos.logback.classic.LoggerContext
55
import ch.qos.logback.classic.PatternLayout
6+
import ch.qos.logback.classic.spi.ILoggingEvent
67
import ch.qos.logback.classic.spi.LoggingEvent
78
import ch.qos.logback.core.encoder.LayoutWrappingEncoder
89
import io.micronaut.runtime.ApplicationConfiguration
@@ -64,6 +65,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
6465
void 'test error queue size equal to 0'() {
6566
when:
6667
appender.queueSize = 0
68+
appender.dispatchOnStart = true
6769
appender.start()
6870

6971
then:
@@ -75,6 +77,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
7577
when:
7678
appender.queueSize = 100
7779
appender.publishPeriod = 0
80+
appender.dispatchOnStart = true
7881
appender.start()
7982

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

9094
then:
@@ -97,6 +101,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
97101
appender.queueSize = 100
98102
appender.publishPeriod = 100
99103
appender.encoder = null
104+
appender.dispatchOnStart = true
100105
appender.start()
101106

102107
then:
@@ -167,6 +172,49 @@ class CloudWatchLoggingAppenderSpec extends Specification {
167172
thrown(UnsupportedOperationException)
168173
}
169174

175+
void 'test start dispatch on start'() {
176+
given:
177+
PollingConditions conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
178+
LoggingEvent event = createEvent("name", Level.INFO, "testMessage", System.currentTimeMillis())
179+
180+
when:
181+
appender.dispatchOnStart = true
182+
appender.start()
183+
appender.doAppend(event)
184+
185+
then:
186+
conditions.eventually {
187+
cloudWatchLogsClient.putLogsRequestList.size() == 1
188+
}
189+
cloudWatchLogsClient.putLogsRequestList.get(0).logEvents()[0].message().contains("testMessage")
190+
}
191+
192+
void 'test start dispatch on append'() {
193+
given:
194+
PollingConditions conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
195+
LoggingEvent event = createEvent("name", Level.INFO, "testMessage", System.currentTimeMillis())
196+
197+
when:
198+
appender.groupName = "testGroup"
199+
appender.streamName = "testStream"
200+
appender.start()
201+
202+
then:
203+
conditions.within(1) {
204+
cloudWatchLogsClient.putLogsRequestList.size() == 0
205+
}
206+
207+
when:
208+
appender.doAppend(event)
209+
210+
then:
211+
conditions.eventually {
212+
cloudWatchLogsClient.putLogsRequestList.size() == 1
213+
}
214+
cloudWatchLogsClient.putLogsRequestList.get(0).logEvents()[0].message().contains("testMessage")
215+
cloudWatchLogsClient.putLogsRequestList.get(0).logStreamName() == "testStream"
216+
}
217+
170218
void 'custom groupName and StreamName'() {
171219
given:
172220
def testGroup = "testGroup"
@@ -178,6 +226,7 @@ class CloudWatchLoggingAppenderSpec extends Specification {
178226
when:
179227
appender.groupName = testGroup
180228
appender.streamName = testStream
229+
appender.dispatchOnStart = true
181230
appender.start()
182231
appender.doAppend(event)
183232

0 commit comments

Comments
 (0)