Skip to content

Commit 9000e6b

Browse files
chore(multiple): localize powertools_dev env logic and warning (#1570)
Co-authored-by: Leandro Damascena <[email protected]>
1 parent ab6f883 commit 9000e6b

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from aws_lambda_powertools.event_handler import content_types
1616
from aws_lambda_powertools.event_handler.exceptions import NotFoundError, ServiceError
1717
from aws_lambda_powertools.shared import constants
18-
from aws_lambda_powertools.shared.functions import resolve_truthy_env_var_choice
18+
from aws_lambda_powertools.shared.functions import powertools_dev_is_set, strtobool
1919
from aws_lambda_powertools.shared.json_encoder import Encoder
2020
from aws_lambda_powertools.utilities.data_classes import (
2121
ALBEvent,
@@ -453,9 +453,7 @@ def __init__(
453453
self._cors = cors
454454
self._cors_enabled: bool = cors is not None
455455
self._cors_methods: Set[str] = {"OPTIONS"}
456-
self._debug = resolve_truthy_env_var_choice(
457-
env=os.getenv(constants.EVENT_HANDLER_DEBUG_ENV, "false"), choice=debug
458-
)
456+
self._debug = self._has_debug(debug)
459457
self._strip_prefixes = strip_prefixes
460458
self.context: Dict = {} # early init as customers might add context before event resolution
461459

@@ -527,6 +525,22 @@ def resolve(self, event, context) -> Dict[str, Any]:
527525
def __call__(self, event, context) -> Any:
528526
return self.resolve(event, context)
529527

528+
@staticmethod
529+
def _has_debug(debug: Optional[bool] = None) -> bool:
530+
# It might have been explicitly switched off (debug=False)
531+
if debug is not None:
532+
return debug
533+
534+
# Maintenance: deprecate EVENT_HANDLER_DEBUG later in V2.
535+
env_debug = os.getenv(constants.EVENT_HANDLER_DEBUG_ENV)
536+
if env_debug is not None:
537+
warnings.warn(
538+
"POWERTOOLS_EVENT_HANDLER_DEBUG is set and will be deprecated in V2. Please use POWERTOOLS_DEV instead."
539+
)
540+
return strtobool(env_debug) or powertools_dev_is_set()
541+
542+
return powertools_dev_is_set()
543+
530544
@staticmethod
531545
def _compile_regex(rule: str):
532546
"""Precompile regex pattern

aws_lambda_powertools/logging/formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
1010

1111
from ..shared import constants
12-
from ..shared.functions import strtobool
12+
from ..shared.functions import powertools_dev_is_set
1313

1414
RESERVED_LOG_ATTRS = (
1515
"name",
@@ -116,7 +116,7 @@ def __init__(
116116
self.json_deserializer = json_deserializer or json.loads
117117
self.json_default = json_default or str
118118
self.json_indent = (
119-
constants.PRETTY_INDENT if strtobool(os.getenv("POWERTOOLS_DEV", "0")) else constants.COMPACT_INDENT
119+
constants.PRETTY_INDENT if powertools_dev_is_set() else constants.COMPACT_INDENT
120120
) # indented json serialization when in AWS SAM Local
121121
self.json_serializer = json_serializer or partial(
122122
json.dumps, default=self.json_default, separators=(",", ":"), indent=self.json_indent

aws_lambda_powertools/shared/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
# JSON indentation level
3737
PRETTY_INDENT: int = 4
3838
COMPACT_INDENT = None
39+
POWERTOOLS_DEV_ENV: str = "POWERTOOLS_DEV"

aws_lambda_powertools/shared/functions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import base64
22
import logging
3+
import os
4+
import warnings
35
from binascii import Error as BinAsciiError
46
from typing import Optional, Union
57

8+
from aws_lambda_powertools.shared import constants
9+
610
logger = logging.getLogger(__name__)
711

812

@@ -78,3 +82,12 @@ def bytes_to_string(value: bytes) -> str:
7882
return value.decode("utf-8")
7983
except (BinAsciiError, TypeError):
8084
raise ValueError("base64 UTF-8 decode failed")
85+
86+
87+
def powertools_dev_is_set() -> bool:
88+
is_on = strtobool(os.getenv(constants.POWERTOOLS_DEV_ENV, "0"))
89+
if is_on:
90+
warnings.warn("POWERTOOLS_DEV environment variable is enabled. Increasing verbosity across utilities.")
91+
return True
92+
93+
return False

docs/core/event_handler/api_gateway.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Like `compress` feature, the client must send the `Accept` header with the corre
388388

389389
### Debug mode
390390

391-
You can enable debug mode via `debug` param, or via `POWERTOOLS_EVENT_HANDLER_DEBUG` [environment variable](../../index.md#environment-variables).
391+
You can enable debug mode via `debug` param, or via `POWERTOOLS_DEV` [environment variable](../../index.md#environment-variables).
392392

393393
This will enable full tracebacks errors in the response, print request and responses, and set CORS in development mode.
394394

tests/functional/event_handler/test_api_gateway.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,16 @@ def test_debug_mode_environment_variable(monkeypatch):
686686
assert app._debug
687687

688688

689+
def test_powertools_dev_sets_debug_mode(monkeypatch):
690+
# GIVEN a debug mode environment variable is set
691+
monkeypatch.setenv(constants.POWERTOOLS_DEV_ENV, "true")
692+
app = ApiGatewayResolver()
693+
694+
# WHEN calling app._debug
695+
# THEN the debug mode is enabled
696+
assert app._debug
697+
698+
689699
def test_debug_json_formatting(json_dump):
690700
# GIVEN debug is True
691701
app = ApiGatewayResolver(debug=True)

0 commit comments

Comments
 (0)