Skip to content

test: fix transient fails in log sampling tests #3736

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

Merged
merged 3 commits into from
Mar 17, 2025
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 @@ -9,23 +9,12 @@ const LOG_MSG = process.env.LOG_MSG || 'Hello World';
const logger = new Logger({
sampleRateValue: SAMPLE_RATE,
});
let firstInvocation = true;

class Lambda implements LambdaInterface {
private readonly logMsg: string;
private readonly logMsg = LOG_MSG;

public constructor() {
this.logMsg = LOG_MSG;
}

// Decorate your handler class method
@logger.injectLambdaContext()
public async handler(_event: TestEvent, context: Context): TestOutput {
if (firstInvocation) {
firstInvocation = false;
} else {
logger.refreshSampleRateCalculation();
}
this.printLogInAllLevels();

return {
Expand Down
50 changes: 47 additions & 3 deletions packages/testing/src/TestStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,63 @@ class TestStack {
Service: 'Powertools-for-AWS-e2e-tests',
},
});
let lastCreateLog = 0;
let lastDestroyLog = 0;
const creationDeleteLogFrequency = 10000; // 10 seconds
const that = this;
this.#cli = new Toolkit({
color: false,
ioHost: {
/**
* Log messages to the console depending on the log level.
*
* If the `RUNNER_DEBUG` environment variable is set to `1`, all messages are logged.
*
* Otherwise, we log messages that are either warnings or errors as well as periodic
* updates on the stack creation and destruction process.
*
* @param msg - Message to log sent by the CDK CLI
*/
async notify(msg) {
if (process.env.RUNNER_DEBUG === '1') {
testConsole.log(msg);
return;
}
if (msg.message.includes('destroyed') && msg.message.includes('✅')) {
testConsole.log(msg.message);
return;
}
if (msg.message.includes('✅') && !msg.message.includes('deployed')) {
testConsole.log(`${that.testName} deployed successfully`);
return;
}
if (msg.message.includes('CREATE_IN_PROGRESS')) {
if (Date.now() - lastCreateLog < creationDeleteLogFrequency) {
return;
}
lastCreateLog = Date.now();
testConsole.log(`${that.testName} stack is being created...`);
return;
}
if (msg.message.includes('DELETE_IN_PROGRESS')) {
if (Date.now() - lastDestroyLog < creationDeleteLogFrequency) {
return;
}
lastDestroyLog = Date.now();
testConsole.log(`${that.testName} stack is being destroyed...`);
return;
}
if (['warning', 'error'].includes(msg.level)) {
testConsole.log(msg);
}
},
async requestResponse(msg) {
if (
process.env.RUNNER_DEBUG === '1' ||
['warning', 'error'].includes(msg.level)
) {
testConsole.log(msg);
}
},
async requestResponse(msg) {
testConsole.log(msg);
return msg.defaultResponse;
},
},
Expand Down
Loading