Skip to content

Commit d3fdfe5

Browse files
authored
fix(cli): watch logs always end with the 'truncated' message (#19241)
CloudWatchLogs.filterLogEvents will always return a `nextToken` as long as there are _any_ additional logs. This is regardless of any filter used (i.e. `startTime`). This PR updates the logic to only display the truncated message if `CloudWatchLogs.filterLogEvents` returns 100 events (the `limit`) _and_ the `nextToken`. If the limit is hit and there is a `nextToken` then we can assume that the _current_ request for log events was truncated. fixes #18805 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent de31f9f commit d3fdfe5

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

packages/aws-cdk/lib/api/logs/logs-monitor.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ export class CloudWatchLogEventMonitor {
198198

199199
}
200200
}
201-
// if we have > 100 events let the user know some
202-
// messages have been supressed. We are essentially
203-
// showing them a sampling (10000 events printed out is not very useful)
204-
if (filteredEvents.length > 0 && response.nextToken) {
201+
// As long as there are _any_ events in the log group `filterLogEvents` will return a nextToken.
202+
// This is true even if these events are before `startTime`. So if we have 100 events and a nextToken
203+
// then assume that we have hit the limit and let the user know some messages have been supressed.
204+
// We are essentially showing them a sampling (10000 events printed out is not very useful)
205+
if (filteredEvents.length === 100 && response.nextToken) {
205206
events.push({
206207
message: '>>> `watch` shows only the first 100 log messages - the rest have been truncated...',
207208
logGroupName,

packages/aws-cdk/test/api/logs/logs-monitor.test.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,55 @@ beforeEach(() => {
1212
sdk = new MockSdk();
1313
});
1414

15-
afterAll(() => {
15+
afterEach(() => {
1616
stderrMock.mockRestore();
1717
monitor.deactivate();
1818
});
1919

20-
test('continue to the next page if it exists', async () => {
20+
test('process events', async () => {
2121
// GIVEN
2222
const eventDate = new Date(T0 + 102 * 1000);
2323
sdk.stubCloudWatchLogs({
2424
filterLogEvents() {
2525
return {
2626
events: [event(102, 'message', eventDate)],
27+
};
28+
},
29+
});
30+
monitor.addLogGroups(
31+
{
32+
name: 'name',
33+
account: '11111111111',
34+
region: 'us-east-1',
35+
},
36+
sdk,
37+
['loggroup'],
38+
);
39+
// WHEN
40+
monitor.activate();
41+
// need time for the log processing to occur
42+
await sleep(1000);
43+
44+
// THEN
45+
const expectedLocaleTimeString = eventDate.toLocaleTimeString();
46+
expect(stderrMock).toHaveBeenCalledTimes(1);
47+
expect(stderrMock.mock.calls[0][0]).toContain(
48+
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} message`,
49+
);
50+
});
51+
52+
test('process truncated events', async () => {
53+
// GIVEN
54+
const eventDate = new Date(T0 + 102 * 1000);
55+
const events: AWS.CloudWatchLogs.FilteredLogEvents = [];
56+
for (let i = 0; i < 100; i++) {
57+
events.push(event(102+i, 'message' + i, eventDate));
58+
}
59+
60+
sdk.stubCloudWatchLogs({
61+
filterLogEvents() {
62+
return {
63+
events,
2764
nextToken: 'some-token',
2865
};
2966
},
@@ -44,11 +81,11 @@ test('continue to the next page if it exists', async () => {
4481

4582
// THEN
4683
const expectedLocaleTimeString = eventDate.toLocaleTimeString();
47-
expect(stderrMock).toHaveBeenCalledTimes(2);
84+
expect(stderrMock).toHaveBeenCalledTimes(101);
4885
expect(stderrMock.mock.calls[0][0]).toContain(
4986
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} message`,
5087
);
51-
expect(stderrMock.mock.calls[1][0]).toContain(
88+
expect(stderrMock.mock.calls[100][0]).toContain(
5289
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} >>> \`watch\` shows only the first 100 log messages - the rest have been truncated...`,
5390
);
5491
});

0 commit comments

Comments
 (0)