-
Notifications
You must be signed in to change notification settings - Fork 45
Consolidate env reading to single config object. #600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
283e118
Consolidate env reading to single config object.
purple4reina 05398fc
Tests for reading config values.
purple4reina dfc8581
Lazy read env values on config.
purple4reina 40b666e
Tests correctly reset config object.
purple4reina fb20e39
Black.
purple4reina 55a47f5
More black.
purple4reina 5da0267
Use config.
purple4reina 9dd96f1
Correct config import.
purple4reina d48a546
???????????
purple4reina f0ad15d
Fix unittests.
purple4reina b89acab
Consolidate reset.
purple4reina ab67bd4
Flush to logs from config.
purple4reina 91887a9
Add logs_injection.
purple4reina c4c3d8d
Add merge_xray_traces.
purple4reina e2426f6
Use functionname.
purple4reina 2f640ef
Add service.
purple4reina 192b9c1
Add trace_extractor.
purple4reina ddd25c6
Add capture_payload_max_depth.
purple4reina 09adc0f
UNblack.
purple4reina 5c81289
Add profiling_enabled.
purple4reina 63b3d4f
Add llmobs_enabled.
purple4reina 62378bc
Add exception_replay_enabled.
purple4reina 76f5d2d
Add env.
purple4reina 47c808a
Add capture_payload_enabled.
purple4reina 0e35ee1
Add min_cold_start_trace_duration.
purple4reina 863fa9e
Add local_test.
purple4reina 3210240
Add cold_start_trace_skip_lib.
purple4reina a1e4993
Default should always be string.
purple4reina 2dfd8ad
Parametrize the parametrize.
purple4reina adfe3ec
Add make_inferred_span.
purple4reina 6308260
Add encode_authorizer_context.
purple4reina 9a23c2a
Add decode_authorizer_context.
purple4reina 831f368
Use config.cold_start_tracing.
purple4reina 51eb5a7
Rm unused.
purple4reina c84b698
Black.
purple4reina e405282
Rename.
purple4reina 2b2148c
reorder.
purple4reina 740be3f
Clean up depends on tests.
purple4reina f492d30
Cold start already depends on tracing.
purple4reina 23ac6b9
Testing for warning log.
purple4reina bbe0da4
More black.
purple4reina d8041b1
testing.x
purple4reina 603c0e0
Debugging.
purple4reina 92936f3
More debug.
purple4reina 1c32c44
Undebug.
purple4reina 20d733c
Fix multiple env accessing for same key.
purple4reina 2957287
Add data_streams_enabled.
purple4reina 76b8ce9
Shorter line.
purple4reina fad3d08
Fix tag_object.
purple4reina c90af27
Telemetry depends on tracing.
purple4reina 43e31fc
Remove unneeded comment.
purple4reina 7bc8751
Cut memory overhead.
purple4reina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
# Unless explicitly stated otherwise all files in this repository are licensed | ||
# under the Apache License Version 2.0. | ||
# This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
# Copyright 2019 Datadog, Inc. | ||
|
||
import logging | ||
import os | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _get_env(key, default=None, cast=None, depends_on_tracing=False): | ||
@property | ||
def _getter(self): | ||
if not hasattr(self, prop_key): | ||
val = self._resolve_env(key, default, cast, depends_on_tracing) | ||
setattr(self, prop_key, val) | ||
return getattr(self, prop_key) | ||
|
||
prop_key = f"_config_{key}" | ||
return _getter | ||
|
||
|
||
def as_bool(val): | ||
return val.lower() == "true" or val == "1" | ||
|
||
|
||
def as_list(val): | ||
return [val.strip() for val in val.split(",") if val.strip()] | ||
|
||
|
||
class Config: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False): | ||
if depends_on_tracing and not self.trace_enabled: | ||
return False | ||
val = os.environ.get(key, default) | ||
if cast is not None: | ||
try: | ||
val = cast(val) | ||
except (ValueError, TypeError): | ||
msg = ( | ||
"Failed to cast environment variable '%s' with " | ||
"value '%s' to type %s. Using default value '%s'." | ||
) | ||
logger.warning(msg, key, val, cast.__name__, default) | ||
val = default | ||
return val | ||
|
||
service = _get_env("DD_SERVICE") | ||
env = _get_env("DD_ENV") | ||
|
||
cold_start_tracing = _get_env( | ||
"DD_COLD_START_TRACING", "true", as_bool, depends_on_tracing=True | ||
) | ||
min_cold_start_trace_duration = _get_env("DD_MIN_COLD_START_DURATION", 3, int) | ||
cold_start_trace_skip_lib = _get_env( | ||
"DD_COLD_START_TRACE_SKIP_LIB", | ||
"ddtrace.internal.compat,ddtrace.filters", | ||
as_list, | ||
) | ||
|
||
capture_payload_max_depth = _get_env("DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH", 10, int) | ||
capture_payload_enabled = _get_env("DD_CAPTURE_LAMBDA_PAYLOAD", "false", as_bool) | ||
|
||
trace_enabled = _get_env("DD_TRACE_ENABLED", "true", as_bool) | ||
make_inferred_span = _get_env( | ||
"DD_TRACE_MANAGED_SERVICES", "true", as_bool, depends_on_tracing=True | ||
) | ||
encode_authorizer_context = _get_env( | ||
"DD_ENCODE_AUTHORIZER_CONTEXT", "true", as_bool, depends_on_tracing=True | ||
) | ||
decode_authorizer_context = _get_env( | ||
"DD_DECODE_AUTHORIZER_CONTEXT", "true", as_bool, depends_on_tracing=True | ||
) | ||
add_span_pointers = _get_env("DD_BOTOCORE_ADD_SPAN_POINTERS", "true", as_bool) | ||
trace_extractor = _get_env("DD_TRACE_EXTRACTOR") | ||
|
||
enhanced_metrics_enabled = _get_env("DD_ENHANCED_METRICS", "true", as_bool) | ||
|
||
flush_in_thread = _get_env("DD_FLUSH_IN_THREAD", "false", as_bool) | ||
flush_to_log = _get_env("DD_FLUSH_TO_LOG", "false", as_bool) | ||
logs_injection = _get_env("DD_LOGS_INJECTION", "true", as_bool) | ||
merge_xray_traces = _get_env("DD_MERGE_XRAY_TRACES", "false", as_bool) | ||
|
||
telemetry_enabled = _get_env( | ||
"DD_INSTRUMENTATION_TELEMETRY_ENABLED", | ||
"false", | ||
as_bool, | ||
depends_on_tracing=True, | ||
) | ||
otel_enabled = _get_env("DD_TRACE_OTEL_ENABLED", "false", as_bool) | ||
profiling_enabled = _get_env("DD_PROFILING_ENABLED", "false", as_bool) | ||
llmobs_enabled = _get_env("DD_LLMOBS_ENABLED", "false", as_bool) | ||
exception_replay_enabled = _get_env("DD_EXCEPTION_REPLAY_ENABLED", "false", as_bool) | ||
data_streams_enabled = _get_env( | ||
"DD_DATA_STREAMS_ENABLED", "false", as_bool, depends_on_tracing=True | ||
) | ||
|
||
is_gov_region = _get_env("AWS_REGION", "", lambda x: x.startswith("us-gov-")) | ||
|
||
local_test = _get_env("DD_LOCAL_TEST", "false", as_bool) | ||
integration_test = _get_env("DD_INTEGRATION_TEST", "false", as_bool) | ||
|
||
aws_lambda_function_name = _get_env("AWS_LAMBDA_FUNCTION_NAME") | ||
|
||
@property | ||
def function_name(self): | ||
if not hasattr(self, "_config_function_name"): | ||
if self.aws_lambda_function_name is None: | ||
self._config_function_name = "function" | ||
else: | ||
self._config_function_name = self.aws_lambda_function_name | ||
return self._config_function_name | ||
|
||
@property | ||
def is_lambda_context(self): | ||
if not hasattr(self, "_config_is_lambda_context"): | ||
self._config_is_lambda_context = bool(self.aws_lambda_function_name) | ||
return self._config_is_lambda_context | ||
|
||
@property | ||
def fips_mode_enabled(self): | ||
if not hasattr(self, "_config_fips_mode_enabled"): | ||
self._config_fips_mode_enabled = ( | ||
os.environ.get( | ||
"DD_LAMBDA_FIPS_MODE", | ||
"true" if self.is_gov_region else "false", | ||
).lower() | ||
== "true" | ||
) | ||
return self._config_fips_mode_enabled | ||
|
||
def _reset(self): | ||
for attr in dir(self): | ||
if attr.startswith("_config_"): | ||
delattr(self, attr) | ||
|
||
|
||
config = Config() | ||
|
||
if config.is_gov_region or config.fips_mode_enabled: | ||
logger.debug( | ||
"Python Lambda Layer FIPS mode is %s.", | ||
"enabled" if config.fips_mode_enabled else "not enabled", | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These globals are not required because the affected objects are lists.