Skip to content

Commit 333acf7

Browse files
authored
Lazy load validation package on import of datadog_checks.checks (DataDog#23936)
* Load the security module only when a check needs it Importing datadog_checks.checks always pulls in the security module (datadog_checks.base.utils.models.validation.security), which accounts for about 1 MiB of memory, even for checks that never use it. Now it's lazy loaded, so the security module is only loaded on use. That saves about 1 MiB for checks that don't need it, with no change for the ones that do. * Add changelog entry for PR
1 parent b60d7eb commit 333acf7

4 files changed

Lines changed: 15 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lazy load the validation package (core, security, utils) on import of datadog_checks.checks.

datadog_checks_base/datadog_checks/base/checks/base.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
from datadog_checks.base.utils.common import ensure_bytes, to_native_string
2929
from datadog_checks.base.utils.fips import enable_fips
3030
from datadog_checks.base.utils.format import json
31-
from datadog_checks.base.utils.models.validation.security import (
32-
DEFAULT_TRUSTED_PROVIDERS,
33-
SecurityConfig,
34-
check_field_trusted_provider,
35-
)
31+
from datadog_checks.base.utils.models import validation
3632
from datadog_checks.base.utils.tagging import GENERIC_TAGS
3733
from datadog_checks.base.utils.tracing import traced_class
3834

@@ -431,22 +427,22 @@ def logs_enabled(self):
431427
return self.__logs_enabled
432428

433429
@property
434-
def security_config(self) -> SecurityConfig:
430+
def security_config(self) -> "validation.security.SecurityConfig":
435431
"""
436432
Returns the integration security configuration, loaded once and cached.
437433
438434
The security config controls file path validation for untrusted providers.
439435
"""
440436
if self.__security_config is None:
441437
trusted_providers = datadog_agent.get_config('integration_trusted_providers')
442-
self.__security_config = SecurityConfig(
438+
self.__security_config = validation.security.SecurityConfig(
443439
check_name=self.name,
444440
provider=self.provider,
445441
ignore_untrusted_file_params=bool(datadog_agent.get_config('integration_ignore_untrusted_file_params')),
446442
file_paths_allowlist=datadog_agent.get_config('integration_file_paths_allowlist') or [],
447443
trusted_providers=trusted_providers
448444
if trusted_providers is not None
449-
else list(DEFAULT_TRUSTED_PROVIDERS),
445+
else list(validation.security.DEFAULT_TRUSTED_PROVIDERS),
450446
excluded_checks=datadog_agent.get_config('integration_security_excluded_checks') or [],
451447
)
452448

@@ -689,7 +685,7 @@ def load_configuration_model(import_path, model_name, config, context):
689685
for field_name in GLOBAL_SECURE_FIELDS & configured_fields:
690686
value = config.get(field_name)
691687
if value is not None:
692-
check_field_trusted_provider(field_name, value, security_config)
688+
validation.security.check_field_trusted_provider(field_name, value, security_config)
693689
except ValueError as e:
694690
raise ConfigurationError(str(e)) from None
695691
return config_model
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# (C) Datadog, Inc. 2021-present
22
# All rights reserved
33
# Licensed under a 3-clause BSD style license (see LICENSE)
4-
from . import core, security, utils
4+
import lazy_loader
5+
6+
__getattr__, __dir__, __all__ = lazy_loader.attach_stub(__name__, __file__)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# (C) Datadog, Inc. 2021-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
from . import core, security, utils
5+
6+
__all__ = ['core', 'security', 'utils']

0 commit comments

Comments
 (0)