-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a few things to enable one to more easily customize what is captured via the SDK. 1. Should you capture any data statistics at all? 2. Max lengths of dicts and lists -> so they don't get too big. You can configure this via three means: 1. python constants 2. config file variables 3. environment variables There is a prescedence order. So there are default values in the module. You can then override them via config file variables. Which then can in turn be overriden by environment variables. Lastly the user can always modify the constants by directly changing the module variables. Note: we also skip logging metadata from datasavers and loaders if the CAPTURE_DATA_STATISTICS = False. We can fix this by special casing it, but for now I don't think people would complain with the current functionality.
- Loading branch information
Showing
5 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""This module contains constants for tracking. | ||
We then override these by: | ||
1. Looking for a configuration file and taking the section under `SDK_CONSTANTS`. | ||
2. Via environment variables. They should be prefixed with `HAMILTON_`. | ||
3. Lastly folks can manually adjust these values directly by importing the module and changing the value. | ||
Note: This module cannot import other Hamilton modules. | ||
""" | ||
|
||
import configparser | ||
import logging | ||
import os | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# The following are the default values for the tracking client | ||
CAPTURE_DATA_STATISTICS = True | ||
MAX_LIST_LENGTH_CAPTURE = 50 | ||
MAX_DICT_LENGTH_CAPTURE = 100 | ||
|
||
# Check for configuration file | ||
# TODO -- add configuration file support | ||
DEFAULT_CONFIG_URI = os.environ.get("HAMILTON_CONFIG_URI", "~/.hamilton.conf") | ||
DEFAULT_CONFIG_LOCATION = os.path.expanduser(DEFAULT_CONFIG_URI) | ||
|
||
|
||
def _load_config(config_location: str) -> configparser.ConfigParser: | ||
"""Pulls config if it exists. | ||
:param config_location: location of the config file. | ||
""" | ||
config = configparser.ConfigParser() | ||
try: | ||
with open(config_location) as f: | ||
config.read_file(f) | ||
except Exception: | ||
pass | ||
|
||
return config | ||
|
||
|
||
_constant_values = globals() | ||
file_config = _load_config(DEFAULT_CONFIG_LOCATION) | ||
# loads from config file and overwrites | ||
if "SDK_CONSTANTS" in file_config: | ||
for key, val in file_config["SDK_CONSTANTS"].items(): | ||
upper_key = key.upper() | ||
if upper_key not in _constant_values: | ||
continue | ||
# overwrite value | ||
_constant_values[upper_key] = val | ||
|
||
# Check for environment variables & overwrites | ||
# TODO automate this by pulling anything in with a prefix and checking | ||
# globals here and updating them. | ||
CAPTURE_DATA_STATISTICS = os.getenv("HAMILTON_CAPTURE_DATA_STATISTICS", CAPTURE_DATA_STATISTICS) | ||
if isinstance(CAPTURE_DATA_STATISTICS, str): | ||
CAPTURE_DATA_STATISTICS = CAPTURE_DATA_STATISTICS.lower() == "true" | ||
MAX_LIST_LENGTH_CAPTURE = int( | ||
os.getenv("HAMILTON_MAX_LIST_LENGTH_CAPTURE", MAX_LIST_LENGTH_CAPTURE) | ||
) | ||
MAX_DICT_LENGTH_CAPTURE = int( | ||
os.getenv("HAMILTON_MAX_DICT_LENGTH_CAPTURE", MAX_DICT_LENGTH_CAPTURE) | ||
) |
This file contains 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 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 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 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