feat: sampling feature for logger#7
Conversation
…et sampled in "debug" setup.
heitorlessa
left a comment
There was a problem hiding this comment.
Looking quite good!! Some minor adjustments and one ask to improve functional testing.
| >>> | ||
| >>> def handler(event, context): | ||
| logger.info("Hello") | ||
| :param service: |
There was a problem hiding this comment.
62-64 doesn't seem right - Can you remove it?
| service name | ||
| LOG_LEVEL: str | ||
| logging level (e.g. INFO, DEBUG) | ||
| POWERTOOLS_LOGGER_SAMPLE_RATE: str |
There was a problem hiding this comment.
I think you meant float in type annotation here - POWERTOOLS_LOGGER_SAMPLE_RATE: float
| 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.
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.
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.")| ------- | ||
| decorate : Callable | ||
| Decorated lambda handler | ||
| :param log_event: |
There was a problem hiding this comment.
Drop these two - I believe these are coming from your IDE automatically ;)
There was a problem hiding this comment.
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.
Is this in the right place? ;)
There was a problem hiding this comment.
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.
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.