Skip to content
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

Add runtime configuration validation #8937

Merged
merged 5 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ignite/datadog_checks/ignite/config_models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from .instance import InstanceConfig
from .shared import SharedConfig


class ConfigMixin:
_config_model_instance: InstanceConfig
_config_model_shared: SharedConfig

@property
def config(self) -> InstanceConfig:
return self._config_model_instance

@property
def shared_config(self) -> SharedConfig:
return self._config_model_shared
112 changes: 112 additions & 0 deletions ignite/datadog_checks/ignite/config_models/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from datadog_checks.base.utils.models.fields import get_default_field_value


def shared_collect_default_metrics(field, value):
return False


def shared_conf(field, value):
return get_default_field_value(field, value)


def shared_new_gc_metrics(field, value):
return False


def shared_service(field, value):
return get_default_field_value(field, value)


def shared_service_check_prefix(field, value):
return get_default_field_value(field, value)


def instance_collect_default_jvm_metrics(field, value):
return True


def instance_disable_generic_tags(field, value):
return False


def instance_empty_default_hostname(field, value):
return False


def instance_is_jmx(field, value):
return False


def instance_java_bin_path(field, value):
return get_default_field_value(field, value)


def instance_java_options(field, value):
return get_default_field_value(field, value)


def instance_jmx_url(field, value):
return get_default_field_value(field, value)


def instance_key_store_password(field, value):
return get_default_field_value(field, value)


def instance_key_store_path(field, value):
return get_default_field_value(field, value)


def instance_min_collection_interval(field, value):
return 15


def instance_name(field, value):
return get_default_field_value(field, value)


def instance_password(field, value):
return get_default_field_value(field, value)


def instance_process_name_regex(field, value):
return get_default_field_value(field, value)


def instance_rmi_client_timeout(field, value):
return 15000


def instance_rmi_connection_timeout(field, value):
return 20000


def instance_rmi_registry_ssl(field, value):
return False


def instance_service(field, value):
return get_default_field_value(field, value)


def instance_tags(field, value):
return get_default_field_value(field, value)


def instance_tools_jar_path(field, value):
return get_default_field_value(field, value)


def instance_trust_store_password(field, value):
return get_default_field_value(field, value)


def instance_trust_store_path(field, value):
return get_default_field_value(field, value)


def instance_user(field, value):
return get_default_field_value(field, value)
65 changes: 65 additions & 0 deletions ignite/datadog_checks/ignite/config_models/instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import annotations

from typing import Optional, Sequence

from pydantic import BaseModel, root_validator, validator

from datadog_checks.base.utils.functions import identity
from datadog_checks.base.utils.models import validation

from . import defaults, validators


class InstanceConfig(BaseModel):
class Config:
allow_mutation = False

collect_default_jvm_metrics: Optional[bool]
disable_generic_tags: Optional[bool]
empty_default_hostname: Optional[bool]
host: str
is_jmx: Optional[bool]
java_bin_path: Optional[str]
java_options: Optional[str]
jmx_url: Optional[str]
key_store_password: Optional[str]
key_store_path: Optional[str]
min_collection_interval: Optional[float]
name: Optional[str]
password: Optional[str]
port: int
process_name_regex: Optional[str]
rmi_client_timeout: Optional[float]
rmi_connection_timeout: Optional[float]
rmi_registry_ssl: Optional[bool]
service: Optional[str]
tags: Optional[Sequence[str]]
tools_jar_path: Optional[str]
trust_store_password: Optional[str]
trust_store_path: Optional[str]
user: Optional[str]

@root_validator(pre=True)
def _initial_validation(cls, values):
return validation.core.initialize_config(getattr(validators, 'initialize_instance', identity)(values))

@validator('*', pre=True, always=True)
def _ensure_defaults(cls, v, field):
if v is not None or field.required:
return v

return getattr(defaults, f'instance_{field.name}')(field, v)

@validator('*')
def _run_validations(cls, v, field):
if not v:
return v

return getattr(validators, f'instance_{field.name}', identity)(v, field=field)

@root_validator(pre=False)
def _final_validation(cls, values):
return validation.core.finalize_config(getattr(validators, 'finalize_instance', identity)(values))
47 changes: 47 additions & 0 deletions ignite/datadog_checks/ignite/config_models/shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import annotations

from typing import Any, Mapping, Optional, Sequence

from pydantic import BaseModel, root_validator, validator

from datadog_checks.base.utils.functions import identity
from datadog_checks.base.utils.models import validation

from . import defaults, validators


class SharedConfig(BaseModel):
class Config:
allow_mutation = False

collect_default_metrics: Optional[bool]
conf: Optional[Sequence[Mapping[str, Any]]]
is_jmx: bool
new_gc_metrics: Optional[bool]
service: Optional[str]
service_check_prefix: Optional[str]

@root_validator(pre=True)
def _initial_validation(cls, values):
return validation.core.initialize_config(getattr(validators, 'initialize_shared', identity)(values))

@validator('*', pre=True, always=True)
def _ensure_defaults(cls, v, field):
if v is not None or field.required:
return v

return getattr(defaults, f'shared_{field.name}')(field, v)

@validator('*')
def _run_validations(cls, v, field):
if not v:
return v

return getattr(validators, f'shared_{field.name}', identity)(v, field=field)

@root_validator(pre=False)
def _final_validation(cls, values):
return validation.core.finalize_config(getattr(validators, 'finalize_shared', identity)(values))
3 changes: 3 additions & 0 deletions ignite/datadog_checks/ignite/config_models/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)