-
Notifications
You must be signed in to change notification settings - Fork 156
feat(logger): set correlation ID in Logger #3726
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
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
50733c9
feat(logger): add correlation ID extraction and search functionality
am29d f8a8cac
test(logger): add unit tests for correlation ID handling in logger
am29d ad31658
feat(logger): add correlation paths for various AWS services
am29d 4e8e2b3
feat(logger): export correlation paths and update exports in index
am29d 61357ce
feat(logger): enhance correlation ID handling with getter and setter …
am29d 59850ca
test(logger): remove unnecessary tests
am29d 23a62c0
docs(logger): add documentation and examples for correlation ID handling
am29d f82f7b2
fix(logger): correct return statement in search function and update c…
am29d 6be9f4f
refactor(logger): update correlation ID imports and restructure logge…
am29d e4e989e
Merge branch 'main' into issue2863/correlation-id-in-logger
am29d 452ad79
test(logger): fix tests after merge issues
am29d 667ae6c
chore: clean up and fix signatures
am29d 2daf9fa
docs(logger): fix typo in examples
am29d c5b471a
test(logger): refactor tests
am29d c588056
chore: addressed PR comments
am29d b67d631
chore(docs): remove return statement form the docs
am29d b3918a2
chore: another batch of review
am29d 9fe3bde
chore: final touch of a sprinkle
am29d 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 hidden or 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 hidden or 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,19 @@ | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { search } from '@aws-lambda-powertools/logger/correlationId'; | ||
|
||
const logger = new Logger({ | ||
correlationIdSearchFn: search, | ||
}); | ||
|
||
class Lambda implements LambdaInterface { | ||
@logger.injectLambdaContext({ | ||
ccorrelationIdPath: 'headers.my_request_id_header', | ||
}) | ||
public async handler(_event: unknown, _context: unknown): Promise<void> { | ||
logger.info('This is an INFO log with some context'); | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
This file contains hidden or 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,6 @@ | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { search } from '@aws-lambda-powertools/logger/correlationId'; | ||
|
||
const logger = new Logger({ | ||
correlationIdSearchFn: search, | ||
}); |
This file contains hidden or 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,13 @@ | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import type { APIGatewayProxyEvent, Context } from 'aws-lambda'; | ||
|
||
const logger = new Logger(); | ||
|
||
export const handler = async ( | ||
event: APIGatewayProxyEvent, | ||
context: Context | ||
): Promise<void> => { | ||
logger.setCorrelationId(event.requestContext.requestId); | ||
|
||
logger.info('This is an INFO log with some context'); | ||
}; | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,21 @@ | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { search } from '@aws-lambda-powertools/logger/correlationId'; | ||
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware'; | ||
import middy from '@middy/core'; | ||
|
||
const logger = new Logger({ | ||
correlationIdSearchFn: search, | ||
}); | ||
|
||
const lambdaHandler = async ( | ||
_event: unknown, | ||
_context: unknown | ||
): Promise<void> => { | ||
logger.info('This is an INFO log with some context'); | ||
}; | ||
|
||
export const handler = middy(lambdaHandler).use( | ||
injectLambdaContext(logger, { | ||
correlationIdPath: 'headers.my_request_id_header', | ||
}) | ||
); | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,7 @@ | ||
{ | ||
"level": "INFO", | ||
"message": "This is an INFO log with some context", | ||
"timestamp": "2021-05-03 11:47:12,494+0000", | ||
"service": "payment", | ||
"correlation_id": "correlation_id_value" | ||
} |
This file contains hidden or 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,22 @@ | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { | ||
correlationPaths, | ||
search, | ||
} from '@aws-lambda-powertools/logger/correlationId'; | ||
|
||
const logger = new Logger({ | ||
correlationIdSearchFn: search, | ||
}); | ||
|
||
class Lambda implements LambdaInterface { | ||
@logger.injectLambdaContext({ | ||
ccorrelationIdPath: correlationPaths.API_GATEWAY_REST, | ||
}) | ||
public async handler(_event: unknown, _context: unknown): Promise<void> { | ||
logger.info('This is an INFO log with some context'); | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,5 @@ | ||
{ | ||
"headers": { | ||
"my_request_id_header": "correlation_id_value" | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
import type { JSONObject } from '@aws-lambda-powertools/commons/types'; | ||
import { search as JMESPathSearch } from '@aws-lambda-powertools/jmespath'; | ||
import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions'; | ||
|
||
const search = (expression: string, data: JSONObject) => { | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return JMESPathSearch(expression, data, { | ||
customFunctions: new PowertoolsFunctions(), | ||
}); | ||
}; | ||
|
||
const correlationPaths = { | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
API_GATEWAY_REST: 'requestContext.requestId', | ||
API_GATEWAY_HTTP: 'requestContext.requestId', | ||
APPSYNC_AUTHORIZER: 'requestContext.requestId', | ||
APPSYNC_RESOLVER: 'request.headers."x-amzn-trace-id"', | ||
APPLICATION_LOAD_BALANCER: 'headers."x-amzn-trace-id"', | ||
EVENT_BRIDGE: 'id', | ||
LAMBDA_FUNCTION_URL: 'requestContext.requestId', | ||
S3_OBJECT_LAMBDA: 'xAmzRequestId', | ||
VPC_LATTICE: 'headers."x-amzn-trace-id"', | ||
}; | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export { correlationPaths, search }; |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { Logger } from './Logger.js'; | ||
export { LogLevel, LogLevelThreshold } from './constants.js'; | ||
export { LogFormatter } from './formatter/LogFormatter.js'; | ||
export { LogItem } from './formatter/LogItem.js'; | ||
export { LogLevel, LogLevelThreshold } from './constants.js'; | ||
export { Logger } from './Logger.js'; |
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.