-
Notifications
You must be signed in to change notification settings - Fork 433
feat: sampling feature for logger #7
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
heitorlessa
merged 4 commits into
aws-powertools:develop
from
danilohgds:sampling_feature
Feb 20, 2020
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f64cd4
feature: ability to pass sampling_rate to logger setup so calls may g…
danilohgds 881393b
formatting
danilohgds bec6d91
changes to tests, to include a scenario with 100% sampling overwritin…
4cf254c
additional assertion to check message from stdout.
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 |
---|---|---|
|
@@ -2,19 +2,22 @@ | |
import itertools | ||
import logging | ||
import os | ||
import random | ||
from distutils.util import strtobool | ||
from typing import Any, Callable, Dict | ||
|
||
from . import aws_lambda_logging | ||
from ..helper.models import MetricUnit, build_lambda_context_model, build_metric_unit_from_str | ||
from . import aws_lambda_logging | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(os.getenv("LOG_LEVEL", "INFO")) | ||
|
||
is_cold_start = True | ||
|
||
|
||
def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwargs): | ||
def logger_setup( | ||
service: str = "service_undefined", level: str = "INFO", sampling_rate: float = 0.0, **kwargs | ||
): | ||
"""Setups root logger to format statements in JSON. | ||
|
||
Includes service name and any additional key=value into logs | ||
|
@@ -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: float | ||
samping rate ranging from 0 to 1, 1 being 100% sampling | ||
|
||
Parameters | ||
---------- | ||
service : str, optional | ||
service name to be appended in logs, by default "service_undefined" | ||
level : str, optional | ||
logging.level, by default "INFO" | ||
sample_rate: float, optional | ||
sample rate for debug calls within execution context defaults to 0 | ||
|
||
Example | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
------- | ||
|
@@ -47,6 +54,7 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar | |
Setups structured logging in JSON for Lambda functions using env vars | ||
|
||
$ export POWERTOOLS_SERVICE_NAME="payment" | ||
$ export POWERTOOLS_LOGGER_SAMPLE_RATE=0.01 # 1% debug sampling | ||
>>> from aws_lambda_powertools.logging import logger_setup | ||
>>> logger = logger_setup() | ||
>>> | ||
|
@@ -55,12 +63,25 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar | |
|
||
""" | ||
service = os.getenv("POWERTOOLS_SERVICE_NAME") or service | ||
sampling_rate = os.getenv("POWERTOOLS_LOGGER_SAMPLE_RATE") or sampling_rate | ||
log_level = os.getenv("LOG_LEVEL") or level | ||
logger = logging.getLogger(name=service) | ||
|
||
try: | ||
if sampling_rate and random.random() <= float(sampling_rate): | ||
log_level = logging.DEBUG | ||
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 commentThe 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." |
||
) | ||
|
||
logger.setLevel(log_level) | ||
|
||
# Patch logger by structuring its outputs as JSON | ||
aws_lambda_logging.setup(level=log_level, service=service, **kwargs) | ||
aws_lambda_logging.setup( | ||
level=log_level, service=service, sampling_rate=sampling_rate, **kwargs | ||
) | ||
|
||
return logger | ||
|
||
|
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
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.