Skip to content

Commit 93f8a5c

Browse files
committed
fix: type annotation, clarify logger in docs
1 parent 5e9b208 commit 93f8a5c

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

aws_lambda_powertools/utilities/feature_flags/appconfig.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import logging
22
import traceback
3-
from typing import Any, Dict, Optional, cast
3+
from typing import Any, Dict, Optional, Union, cast
44

55
from botocore.config import Config
66

77
from aws_lambda_powertools.utilities.parameters import AppConfigProvider, GetParameterError, TransformParameterError
88

9+
from ... import Logger
910
from ...shared import jmespath_utils
1011
from .base import StoreProvider
1112
from .exceptions import ConfigurationStoreError, StoreClientError
@@ -23,7 +24,7 @@ def __init__(
2324
sdk_config: Optional[Config] = None,
2425
envelope: Optional[str] = "",
2526
jmespath_options: Optional[Dict] = None,
26-
logger=None,
27+
logger: Optional[Union[logging.Logger, Logger]] = None,
2728
):
2829
"""This class fetches JSON schemas from AWS AppConfig
2930
@@ -62,6 +63,9 @@ def get_raw_configuration(self) -> Dict[str, Any]:
6263
"""Fetch feature schema configuration from AWS AppConfig"""
6364
try:
6465
# parse result conf as JSON, keep in cache for self.max_age seconds
66+
self.logger.debug(
67+
"Fetching configuration from the store", extra={"param_name": self.name, "max_age": self.cache_seconds}
68+
)
6569
return cast(
6670
dict,
6771
self._conf_store.get(
@@ -95,6 +99,7 @@ def get_configuration(self) -> Dict[str, Any]:
9599
config = self.get_raw_configuration
96100

97101
if self.envelope:
102+
self.logger.debug("Envelope enabled; extracting data from config", extra={"envelope": self.envelope})
98103
config = jmespath_utils.extract_data_from_envelope(
99104
data=config, envelope=self.envelope, jmespath_options=self.jmespath_options
100105
)

aws_lambda_powertools/utilities/feature_flags/feature_flags.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import logging
2-
from typing import Any, Dict, List, Optional, cast
2+
from typing import Any, Dict, List, Optional, Union, cast
33

4+
from ... import Logger
45
from . import schema
56
from .base import StoreProvider
67
from .exceptions import ConfigurationStoreError
78

89

910
class FeatureFlags:
10-
def __init__(self, store: StoreProvider, logger=None):
11+
def __init__(self, store: StoreProvider, logger: Optional[Union[logging.Logger, Logger]] = None):
1112
"""Evaluates whether feature flags should be enabled based on a given context.
1213
1314
It uses the provided store to fetch feature flag rules before evaluating them.

aws_lambda_powertools/utilities/feature_flags/schema.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
22
from enum import Enum
3-
from typing import Any, Dict, List, Optional
3+
from typing import Any, Dict, List, Optional, Union
44

5+
from ... import Logger
56
from .base import BaseValidator
67
from .exceptions import SchemaValidationError
78

@@ -109,7 +110,7 @@ class SchemaValidator(BaseValidator):
109110
```
110111
"""
111112

112-
def __init__(self, schema: Dict[str, Any], logger=None):
113+
def __init__(self, schema: Dict[str, Any], logger: Optional[Union[logging.Logger, Logger]] = None):
113114
self.schema = schema
114115
self.logger = logger or logging.getLogger(__name__)
115116

@@ -125,8 +126,9 @@ def validate(self) -> None:
125126
class FeaturesValidator(BaseValidator):
126127
"""Validates each feature and calls RulesValidator to validate its rules"""
127128

128-
def __init__(self, schema: Dict):
129+
def __init__(self, schema: Dict, logger: Optional[Union[logging.Logger, Logger]] = None):
129130
self.schema = schema
131+
self.logger = logger or logging.getLogger(__name__)
130132

131133
def validate(self):
132134
for name, feature in self.schema.items():
@@ -148,10 +150,11 @@ def validate_feature(name, feature):
148150
class RulesValidator(BaseValidator):
149151
"""Validates each rule and calls ConditionsValidator to validate each rule's conditions"""
150152

151-
def __init__(self, feature: Dict[str, Any]):
153+
def __init__(self, feature: Dict[str, Any], logger: Optional[Union[logging.Logger, Logger]] = None):
152154
self.feature = feature
153155
self.feature_name = next(iter(self.feature))
154156
self.rules: Optional[Dict] = self.feature.get(RULES_KEY)
157+
self.logger = logger or logging.getLogger(__name__)
155158

156159
def validate(self):
157160
if not self.rules:
@@ -188,9 +191,10 @@ def validate_rule_default_value(rule: Dict, rule_name: str):
188191

189192

190193
class ConditionsValidator(BaseValidator):
191-
def __init__(self, rule: Dict[str, Any], rule_name: str):
194+
def __init__(self, rule: Dict[str, Any], rule_name: str, logger: Optional[Union[logging.Logger, Logger]] = None):
192195
self.conditions: List[Dict[str, Any]] = rule.get(CONDITIONS_KEY, {})
193196
self.rule_name = rule_name
197+
self.logger = logger or logging.getLogger(__name__)
194198

195199
def validate(self):
196200
if not self.conditions or not isinstance(self.conditions, list):

docs/utilities/feature_flags.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ Parameter | Default | Description
580580
**max_age** | `5` | Number of seconds to cache feature flags configuration fetched from AWS AppConfig
581581
**sdk_config** | `None` | [Botocore Config object](https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html){target="_blank"}
582582
**jmespath_options** | `None` | For advanced use cases when you want to bring your own [JMESPath functions](https://github.com/jmespath/jmespath.py#custom-functions){target="_blank"}
583-
**logger** | `None` | Logger to use. If None is supplied, then a logger will be created.
583+
**logger** | `logging.Logger` | Logger to use for debug. You can optionally supply an instance of Powertools Logger.
584584

585585
=== "appconfig_store_example.py"
586586

0 commit comments

Comments
 (0)