-
Notifications
You must be signed in to change notification settings - Fork 406
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
feat: sampling feature for logger #7
feat: sampling feature for logger #7
Conversation
…et sampled in "debug" setup.
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.
Looking quite good!! Some minor adjustments and one ask to improve functional testing.
@@ -52,15 +59,32 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar | |||
>>> | |||
>>> def handler(event, context): | |||
logger.info("Hello") | |||
:param service: |
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.
62-64 doesn't seem right - Can you remove it?
@@ -26,13 +29,17 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar | |||
service name | |||
LOG_LEVEL: str | |||
logging level (e.g. INFO, DEBUG) | |||
POWERTOOLS_LOGGER_SAMPLE_RATE: str |
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.
I think you meant float in type annotation here - POWERTOOLS_LOGGER_SAMPLE_RATE: float
@@ -26,13 +29,17 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar | |||
service name | |||
LOG_LEVEL: str | |||
logging level (e.g. INFO, DEBUG) | |||
POWERTOOLS_LOGGER_SAMPLE_RATE: str | |||
samping rate ranging from 0 to 1, float precision |
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.
Drop ", float precision" and use "1 being 100% sampling" instead. This along with float type annotation should clear out any ambiguity.
if sampling_rate and random.random() <= float(sampling_rate): | ||
log_level = logging.DEBUG | ||
except ValueError: | ||
logger.debug( |
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.
Raise the exception instead so they know they need to take corrective actions.
raise ValueError(f"Expected a float value ranging 0 to 1, but received {sampling_rate} instead. Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable.")
@@ -110,6 +134,8 @@ def logger_inject_lambda_context( | |||
------- | |||
decorate : Callable | |||
Decorated lambda handler | |||
:param log_event: |
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.
Drop these two - I believe these are coming from your IDE automatically ;)
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.
yup, pycharm being cheeky :)
log_level = os.getenv("LOG_LEVEL") or level | ||
logger = logging.getLogger(name=service) | ||
|
||
# sampling a small percentage of requests with debug level, using a float value 0.1 = 10%~ |
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.
Is this in the right place? ;)
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.
removed, good catch.
…g LOG_LEVEL. comments/docstrings from IDE removed.
except ValueError: | ||
raise ValueError( | ||
f"Expected a float value ranging 0 to 1, but received {sampling_rate} instead. Please review " | ||
f"POWERTOOLS_LOGGER_SAMPLE_RATE environment variable." |
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.
You can merge this line with the above instead of having two lines ;)
f"Expected a float value ranging 0 to 1, but received {sampling_rate} instead. Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
minor reformat removal ( pycharm pep8 complains with 120 characters on line )
Sampling feature
Issue #, if available:
This relates to issue Enhancement Suggestion - Log Sampling
Description of changes:
This pull request introduces a new variable/ constructor parameter to enable debug log sampling of a given percentage, similar to what DAZN powertools has. POWERTOOLS_LOGGER_SAMPLE_RATE accepts in 0-1 range, to log in debug mode at the given rate.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.