-
Notifications
You must be signed in to change notification settings - Fork 146
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
fix(logger): POWERTOOLS_LOGGER_LOG_EVENT precedence is respected #1015
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c70a0b
fix: POWERTOOLS_LOGGER_LOG_EVENT precedence is respected
dreamorosi a034a60
fix: decorator logic
dreamorosi 0dd4568
chore: added unit test cases for decorator
dreamorosi 076660c
chore: added e2e test cases
dreamorosi 00f5f73
chore: address review comments
dreamorosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.FunctionCode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { injectLambdaContext, Logger } from '../../src'; | ||
import { Context } from 'aws-lambda'; | ||
import middy from '@middy/core'; | ||
|
||
type LambdaEvent = { | ||
invocation: number | ||
}; | ||
|
||
const logger = new Logger(); | ||
|
||
const testFunction = async (event: LambdaEvent, context: Context): Promise<{requestId: string}> => ({ | ||
requestId: context.awsRequestId, | ||
}); | ||
|
||
export const handler = middy(testFunction) | ||
.use(injectLambdaContext(logger)); |
110 changes: 110 additions & 0 deletions
110
packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT-0 | ||
|
||
/** | ||
* Test logger basic features | ||
* | ||
* @group e2e/logger/logEventEnvVarSetting | ||
*/ | ||
|
||
import path from 'path'; | ||
import { App, Stack } from 'aws-cdk-lib'; | ||
import { v4 } from 'uuid'; | ||
import { | ||
createStackWithLambdaFunction, | ||
generateUniqueName, | ||
invokeFunction, | ||
isValidRuntimeKey | ||
} from '../../../commons/tests/utils/e2eUtils'; | ||
import { InvocationLogs } from '../../../commons/tests/utils/InvocationLogs'; | ||
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli'; | ||
import { | ||
RESOURCE_NAME_PREFIX, | ||
STACK_OUTPUT_LOG_GROUP, | ||
SETUP_TIMEOUT, | ||
TEST_CASE_TIMEOUT, | ||
TEARDOWN_TIMEOUT | ||
} from './constants'; | ||
|
||
const runtime: string = process.env.RUNTIME || 'nodejs16x'; | ||
|
||
if (!isValidRuntimeKey(runtime)) { | ||
throw new Error(`Invalid runtime key value: ${runtime}`); | ||
} | ||
|
||
const uuid = v4(); | ||
const stackName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'LogEventEnvVarSetting-Middy'); | ||
const functionName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'LogEventEnvVarSetting-Middy'); | ||
const lambdaFunctionCodeFile = 'logEventEnvVarSetting.middy.test.FunctionCode.ts'; | ||
|
||
const integTestApp = new App(); | ||
let logGroupName: string; // We do not know it until deployment | ||
let stack: Stack; | ||
|
||
describe(`logger E2E tests log event via env var setting (middy) for runtime: ${runtime}`, () => { | ||
|
||
let invocationLogs: InvocationLogs[]; | ||
|
||
beforeAll(async () => { | ||
// Create and deploy a stack with AWS CDK | ||
stack = createStackWithLambdaFunction({ | ||
app: integTestApp, | ||
stackName: stackName, | ||
functionName: functionName, | ||
functionEntry: path.join(__dirname, lambdaFunctionCodeFile), | ||
environment: { | ||
LOG_LEVEL: 'INFO', | ||
POWERTOOLS_SERVICE_NAME: 'logger-e2e-testing', | ||
UUID: uuid, | ||
|
||
// Enabling the logger to log events via env var | ||
POWERTOOLS_LOGGER_LOG_EVENT: 'true', | ||
}, | ||
logGroupOutputKey: STACK_OUTPUT_LOG_GROUP, | ||
runtime: runtime, | ||
}); | ||
|
||
const result = await deployStack(integTestApp, stack); | ||
logGroupName = result.outputs[STACK_OUTPUT_LOG_GROUP]; | ||
|
||
// Invoke the function twice (one for cold start, another for warm start) | ||
invocationLogs = await invokeFunction(functionName, 2, 'SEQUENTIAL'); | ||
|
||
console.log('logGroupName', logGroupName); | ||
|
||
}, SETUP_TIMEOUT); | ||
|
||
describe('Log event', () => { | ||
|
||
it('should log the event at both invocations', async () => { | ||
const firstInvocationMessages = invocationLogs[0].getAllFunctionLogs(); | ||
let eventLoggedInFirstInvocation = false; | ||
for (const message of firstInvocationMessages) { | ||
if (message.includes(`event`)) { | ||
eventLoggedInFirstInvocation = true; | ||
expect(message).toContain(`"event":{"invocation":0}`); | ||
} | ||
} | ||
|
||
const secondInvocationMessages = invocationLogs[1].getAllFunctionLogs(); | ||
let eventLoggedInSecondInvocation = false; | ||
for (const message of secondInvocationMessages) { | ||
if (message.includes(`event`)) { | ||
eventLoggedInSecondInvocation = true; | ||
expect(message).toContain(`"event":{"invocation":1}`); | ||
} | ||
} | ||
|
||
expect(eventLoggedInFirstInvocation).toBe(true); | ||
expect(eventLoggedInSecondInvocation).toBe(true); | ||
|
||
}, TEST_CASE_TIMEOUT); | ||
|
||
}); | ||
|
||
afterAll(async () => { | ||
if (!process.env.DISABLE_TEARDOWN) { | ||
await destroyStack(integTestApp, stack); | ||
} | ||
}, TEARDOWN_TIMEOUT); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) missing Copyright header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of the functions in that group of e2e tests has the copyright header, should I add it?