Skip to content

Commit 403b32f

Browse files
Danilo_Silva@epam.comDanilo_Silva@epam.com
Danilo_Silva@epam.com
authored and
Danilo_Silva@epam.com
committed
changes to tests, to include a scenario with 100% sampling overwriting LOG_LEVEL.
comments/docstrings from IDE removed.
1 parent e25ab11 commit 403b32f

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

python/aws_lambda_powertools/logging/logger.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def logger_setup(
2929
service name
3030
LOG_LEVEL: str
3131
logging level (e.g. INFO, DEBUG)
32-
POWERTOOLS_LOGGER_SAMPLE_RATE: str
33-
samping rate ranging from 0 to 1, float precision
32+
POWERTOOLS_LOGGER_SAMPLE_RATE: float
33+
samping rate ranging from 0 to 1, 1 being 100% sampling
3434
3535
Parameters
3636
----------
@@ -54,29 +54,26 @@ def logger_setup(
5454
Setups structured logging in JSON for Lambda functions using env vars
5555
5656
$ export POWERTOOLS_SERVICE_NAME="payment"
57+
$ export POWERTOOLS_LOGGER_SAMPLE_RATE=0.01 # 1% debug sampling
5758
>>> from aws_lambda_powertools.logging import logger_setup
5859
>>> logger = logger_setup()
5960
>>>
6061
>>> def handler(event, context):
6162
logger.info("Hello")
62-
:param service:
63-
:param level:
64-
:param sampling_rate:
6563
6664
"""
6765
service = os.getenv("POWERTOOLS_SERVICE_NAME") or service
6866
sampling_rate = os.getenv("POWERTOOLS_LOGGER_SAMPLE_RATE") or sampling_rate
6967
log_level = os.getenv("LOG_LEVEL") or level
7068
logger = logging.getLogger(name=service)
7169

72-
# sampling a small percentage of requests with debug level, using a float value 0.1 = 10%~
73-
7470
try:
7571
if sampling_rate and random.random() <= float(sampling_rate):
7672
log_level = logging.DEBUG
7773
except ValueError:
78-
logger.debug(
79-
"POWERTOOLS_LOGGER_SAMPLE_RATE provided value {0} is not valid.".format(sampling_rate)
74+
raise ValueError(
75+
f"Expected a float value ranging 0 to 1, but received {sampling_rate} instead. Please review "
76+
f"POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
8077
)
8178

8279
logger.setLevel(log_level)
@@ -134,8 +131,6 @@ def logger_inject_lambda_context(
134131
-------
135132
decorate : Callable
136133
Decorated lambda handler
137-
:param log_event:
138-
:param lambda_handler:
139134
"""
140135

141136
# If handler is None we've been called with parameters

python/tests/functional/test_logger.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ def test_setup_service_env_var(monkeypatch, root_logger, stdout):
8383
def test_setup_sampling_rate(monkeypatch, root_logger, stdout):
8484
# GIVEN samping rate is explicitly defined via POWERTOOLS_LOGGER_SAMPLE_RATE env
8585
# WHEN logger is setup
86-
# THEN sampling rate should be equals POWERTOOLS_LOGGER_SAMPLE_RATE value
87-
sampling_rate = "0.1"
86+
# THEN sampling rate should be equals POWERTOOLS_LOGGER_SAMPLE_RATE value and should sample debug logs
87+
88+
sampling_rate = "1"
8889
monkeypatch.setenv("POWERTOOLS_LOGGER_SAMPLE_RATE", sampling_rate)
90+
monkeypatch.setenv("LOG_LEVEL", "INFO")
8991

9092
logger = logger_setup()
91-
logger.info("Hello")
93+
logger.debug("I am being sampled")
9294
log = json.loads(stdout.getvalue())
95+
9396
assert sampling_rate == log["sampling_rate"]
97+
assert "DEBUG" == log["level"]
9498

9599

96100
def test_inject_lambda_context(root_logger, stdout, lambda_context):

0 commit comments

Comments
 (0)