Skip to content

Commit 7e32a87

Browse files
committed
Merge branch 'develop' of https://github.com/awslabs/aws-lambda-powertools into develop
* 'develop' of https://github.com/awslabs/aws-lambda-powertools: additional assertion to check message from stdout. changes to tests, to include a scenario with 100% sampling overwriting LOG_LEVEL. formatting feature: ability to pass sampling_rate to logger setup so calls may get sampled in "debug" setup.
2 parents 76f19d4 + ccd9f03 commit 7e32a87

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,5 @@ $RECYCLE.BIN/
291291
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
292292

293293
# Misc
294-
295-
test_report
294+
test_report
295+
/.idea/*

python/aws_lambda_powertools/logging/logger.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import itertools
33
import logging
44
import os
5+
import random
56
from distutils.util import strtobool
67
from typing import Any, Callable, Dict
78

@@ -14,7 +15,9 @@
1415
is_cold_start = True
1516

1617

17-
def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwargs):
18+
def logger_setup(
19+
service: str = "service_undefined", level: str = "INFO", sampling_rate: float = 0.0, **kwargs
20+
):
1821
"""Setups root logger to format statements in JSON.
1922
2023
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
2629
service name
2730
LOG_LEVEL: str
2831
logging level (e.g. INFO, DEBUG)
32+
POWERTOOLS_LOGGER_SAMPLE_RATE: float
33+
samping rate ranging from 0 to 1, 1 being 100% sampling
2934
3035
Parameters
3136
----------
3237
service : str, optional
3338
service name to be appended in logs, by default "service_undefined"
3439
level : str, optional
3540
logging.level, by default "INFO"
41+
sample_rate: float, optional
42+
sample rate for debug calls within execution context defaults to 0
3643
3744
Example
3845
-------
@@ -47,6 +54,7 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar
4754
Setups structured logging in JSON for Lambda functions using env vars
4855
4956
$ export POWERTOOLS_SERVICE_NAME="payment"
57+
$ export POWERTOOLS_LOGGER_SAMPLE_RATE=0.01 # 1% debug sampling
5058
>>> from aws_lambda_powertools.logging import logger_setup
5159
>>> logger = logger_setup()
5260
>>>
@@ -55,12 +63,24 @@ def logger_setup(service: str = "service_undefined", level: str = "INFO", **kwar
5563
5664
"""
5765
service = os.getenv("POWERTOOLS_SERVICE_NAME") or service
66+
sampling_rate = os.getenv("POWERTOOLS_LOGGER_SAMPLE_RATE") or sampling_rate
5867
log_level = os.getenv("LOG_LEVEL") or level
5968
logger = logging.getLogger(name=service)
69+
70+
try:
71+
if sampling_rate and random.random() <= float(sampling_rate):
72+
log_level = logging.DEBUG
73+
except ValueError:
74+
raise ValueError(
75+
f"Expected a float value ranging 0 to 1, but received {sampling_rate} instead. Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
76+
)
77+
6078
logger.setLevel(log_level)
6179

6280
# Patch logger by structuring its outputs as JSON
63-
aws_lambda_logging.setup(level=log_level, service=service, **kwargs)
81+
aws_lambda_logging.setup(
82+
level=log_level, service=service, sampling_rate=sampling_rate, **kwargs
83+
)
6484

6585
return logger
6686

python/tests/functional/test_logger.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ def test_setup_service_env_var(monkeypatch, root_logger, stdout):
8080
assert service_name == log["service"]
8181

8282

83+
def test_setup_sampling_rate(monkeypatch, root_logger, stdout):
84+
# GIVEN samping rate is explicitly defined via POWERTOOLS_LOGGER_SAMPLE_RATE env
85+
# WHEN logger is setup
86+
# THEN sampling rate should be equals POWERTOOLS_LOGGER_SAMPLE_RATE value and should sample debug logs
87+
88+
sampling_rate = "1"
89+
monkeypatch.setenv("POWERTOOLS_LOGGER_SAMPLE_RATE", sampling_rate)
90+
monkeypatch.setenv("LOG_LEVEL", "INFO")
91+
92+
logger = logger_setup()
93+
logger.debug("I am being sampled")
94+
log = json.loads(stdout.getvalue())
95+
96+
assert sampling_rate == log["sampling_rate"]
97+
assert "DEBUG" == log["level"]
98+
assert "I am being sampled" == log["message"]
99+
100+
83101
def test_inject_lambda_context(root_logger, stdout, lambda_context):
84102
# GIVEN a lambda function is decorated with logger
85103
# WHEN logger is setup
@@ -130,7 +148,6 @@ def handler(event, context):
130148
def test_inject_lambda_context_log_event_request_env_var(
131149
monkeypatch, root_logger, stdout, lambda_context
132150
):
133-
134151
# GIVEN a lambda function is decorated with logger instructed to log event
135152
# via POWERTOOLS_LOGGER_LOG_EVENT env
136153
# WHEN logger is setup

0 commit comments

Comments
 (0)