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 #8990

Merged
merged 2 commits into from
Jun 15, 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
2 changes: 0 additions & 2 deletions statsd/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ files:
- template: instances
options:
- name: host
required: true
description: Host to collect StatsD data from.
value:
example: localhost
type: string
- name: port
description: Port to use to collect StatsD data.
required: true
value:
example: 8126
type: integer
Expand Down
18 changes: 18 additions & 0 deletions statsd/datadog_checks/statsd/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
36 changes: 36 additions & 0 deletions statsd/datadog_checks/statsd/config_models/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# (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_service(field, value):
return get_default_field_value(field, value)


def instance_empty_default_hostname(field, value):
return False


def instance_host(field, value):
return 'localhost'


def instance_min_collection_interval(field, value):
return 15


def instance_port(field, value):
return 8126


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_timeout(field, value):
return 10
48 changes: 48 additions & 0 deletions statsd/datadog_checks/statsd/config_models/instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# (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

empty_default_hostname: Optional[bool]
host: Optional[str]
min_collection_interval: Optional[float]
port: Optional[int]
service: Optional[str]
tags: Optional[Sequence[str]]
timeout: Optional[int]

@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))
42 changes: 42 additions & 0 deletions statsd/datadog_checks/statsd/config_models/shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# (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

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

service: 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 statsd/datadog_checks/statsd/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)
9 changes: 5 additions & 4 deletions statsd/datadog_checks/statsd/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ init_config:
#
instances:

## @param host - string - required
-
## @param host - string - optional - default: localhost
## Host to collect StatsD data from.
#
- host: localhost
# host: localhost

## @param port - integer - required
## @param port - integer - optional - default: 8126
## Port to use to collect StatsD data.
#
port: 8126
# port: 8126

## @param timeout - integer - optional - default: 10
## Custom timeout for your check request.
Expand Down