-
Notifications
You must be signed in to change notification settings - Fork 156
fix(logger): correctly refresh sample rate #3722
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,27 +1,16 @@ | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
// Notice the log level set to 'ERROR' | ||
const logger = new Logger({ | ||
logLevel: 'ERROR', | ||
logLevel: 'ERROR', // (1)! | ||
sampleRateValue: 0.5, | ||
}); | ||
|
||
export const handler = async ( | ||
_event: unknown, | ||
_context: unknown | ||
): Promise<void> => { | ||
// Refresh sample rate calculation on runtime, only when not using injectLambdaContext | ||
logger.refreshSampleRateCalculation(); | ||
// This log item (equal to log level 'ERROR') will be printed to standard output | ||
// in all Lambda invocations | ||
logger.error('This is an ERROR log'); | ||
export const handler = async () => { | ||
logger.refreshSampleRateCalculation(); // (2)! | ||
|
||
// These log items (below the log level 'ERROR') have ~50% chance | ||
// of being printed in a Lambda invocation | ||
logger.debug('This is a DEBUG log that has 50% chance of being printed'); | ||
logger.info('This is an INFO log that has 50% chance of being printed'); | ||
logger.warn('This is a WARN log that has 50% chance of being printed'); | ||
logger.error('This log is always emitted'); | ||
|
||
// Optional: refresh sample rate calculation on runtime | ||
// logger.refreshSampleRateCalculation(); | ||
logger.debug('This log has ~50% chance of being emitted'); | ||
logger.info('This log has ~50% chance of being emitted'); | ||
logger.warn('This log has ~50% chance of being emitted'); | ||
}; |
8 changes: 8 additions & 0 deletions
8
examples/snippets/logger/samples/debugLogSamplingNotSampled.json
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,8 @@ | ||
{ | ||
"level": "ERROR", | ||
"message": "This log is always emitted", | ||
"sampling_rate": "0.5", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.334Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/snippets/logger/samples/debugLogSamplingSampled.json
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,34 @@ | ||
[ | ||
{ | ||
"level": "ERROR", | ||
"message": "This log is always emitted", | ||
"sampling_rate": "0.5", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.334Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
}, | ||
{ | ||
"level": "DEBUG", | ||
"message": "This log has ~50% chance of being emitted", | ||
"sampling_rate": "0.5", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.337Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
}, | ||
{ | ||
"level": "INFO", | ||
"message": "This log has ~50% chance of being emitted", | ||
"sampling_rate": "0.5", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.338Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
}, | ||
{ | ||
"level": "WARN", | ||
"message": "This log has ~50% chance of being emitted", | ||
"sampling_rate": "0.5", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.338Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
] |
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.
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.