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 better filtering options #10775

Merged
merged 2 commits into from
Dec 3, 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
64 changes: 48 additions & 16 deletions iis/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,69 @@ files:
- template: instances
options:
- name: sites
required: false
display_priority: 1
description: |
The `sites` parameter allows you to specify a list of sites you want to
read metrics from. With sites specified, metrics are tagged with the
site name. If you don't define any sites, the check pulls all of the
sites, and tags each one with the site name.

On Agent versions 7.34 or higher, this may also be defined as a mapping
with `include` and/or `exclude` keys representing arrays of regular
expressions strings.
fanny-jiang marked this conversation as resolved.
Show resolved Hide resolved
value:
example:
- <WEB_SITE_1>
- <WEB_SITE_2>
type: array
compact_example: false
items:
type: string
include:
- <PATTERN_1>
- <PATTERN_2>
exclude:
- <PATTERN_1>
- <PATTERN_2>
anyOf:
- type: array
items:
type: string
- type: object
properties:
- name: include
type: array
items:
type: string
- name: exclude
type: array
items:
type: string
fanny-jiang marked this conversation as resolved.
Show resolved Hide resolved
- name: app_pools
required: false
display_priority: 1
description: |
The `app_pools` parameter allows you to specify a list of application pools you want to
read metrics from. With application pools specified, metrics are tagged with the
application pool name. If you don't define any application pools, the check pulls all of the
application pools, and tags each one with the application pool name.

On Agent versions 7.34 or higher, this may also be defined as a mapping
with `include` and/or `exclude` keys representing arrays of regular
expressions strings.
value:
example:
- <APP_POOL_1>
- <APP_POOL_2>
type: array
compact_example: false
items:
type: string
include:
- <PATTERN_1>
- <PATTERN_2>
exclude:
- <PATTERN_1>
- <PATTERN_2>
anyOf:
- type: array
items:
type: string
- type: object
properties:
- name: include
type: array
items:
type: string
- name: exclude
type: array
items:
type: string
- template: instances/perf_counters
- template: instances/default
- template: logs
Expand Down
16 changes: 13 additions & 3 deletions iis/datadog_checks/iis/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ def get_default_config(self):
for object_name, config in METRICS_CONFIG.items():
new_config = config.copy()

instance_config = []
include = []
exclude = []
if object_name == 'APP_POOL_WAS':
new_config['tag_name'] = 'app_pool'
include.extend(self.instance.get('app_pools', []))
instance_config = self.instance.get('app_pools', [])
elif object_name == 'Web Service':
new_config['tag_name'] = 'site'
include.extend(self.instance.get('sites', []))
instance_config = self.instance.get('sites', [])

if isinstance(instance_config, list):
include.extend(f'^{instance}$' for instance in instance_config)
elif isinstance(instance_config, dict):
include.extend(instance_config.get('include', []))
exclude.extend(instance_config.get('exclude', []))

if include:
new_config['include'] = [f'^{instance}$' for instance in include]
new_config['include'] = include
if exclude:
new_config['exclude'] = exclude

metrics_config[object_name] = new_config

Expand Down
20 changes: 18 additions & 2 deletions iis/datadog_checks/iis/config_models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
from . import defaults, validators


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

exclude: Optional[Sequence[str]]
include: Optional[Sequence[str]]


class Counter(BaseModel):
class Config:
extra = Extra.allow
Expand Down Expand Up @@ -81,11 +89,19 @@ class Config:
use_localized_counters: Optional[bool]


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

exclude: Optional[Sequence[str]]
include: Optional[Sequence[str]]


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

app_pools: Optional[Sequence[str]]
app_pools: Optional[Union[Sequence[str], AppPool]]
disable_generic_tags: Optional[bool]
empty_default_hostname: Optional[bool]
enable_health_service_check: Optional[bool]
Expand All @@ -97,7 +113,7 @@ class Config:
server: Optional[str]
server_tag: Optional[str]
service: Optional[str]
sites: Optional[Sequence[str]]
sites: Optional[Union[Sequence[str], Site]]
tags: Optional[Sequence[str]]
username: Optional[str]

Expand Down
30 changes: 23 additions & 7 deletions iis/datadog_checks/iis/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,41 @@ init_config:
instances:

-
## @param sites - list of strings - optional
## @param sites - list of strings or mapping - optional
## The `sites` parameter allows you to specify a list of sites you want to
## read metrics from. With sites specified, metrics are tagged with the
## site name. If you don't define any sites, the check pulls all of the
## sites, and tags each one with the site name.
##
## On Agent versions 7.34 or higher, this may also be defined as a mapping
## with `include` and/or `exclude` keys representing arrays of regular
## expressions strings.
#
# sites:
# - <WEB_SITE_1>
# - <WEB_SITE_2>

## @param app_pools - list of strings - optional
# include:
# - <PATTERN_1>
# - <PATTERN_2>
# exclude:
# - <PATTERN_1>
# - <PATTERN_2>

## @param app_pools - list of strings or mapping - optional
## The `app_pools` parameter allows you to specify a list of application pools you want to
## read metrics from. With application pools specified, metrics are tagged with the
## application pool name. If you don't define any application pools, the check pulls all of the
## application pools, and tags each one with the application pool name.
##
## On Agent versions 7.34 or higher, this may also be defined as a mapping
## with `include` and/or `exclude` keys representing arrays of regular
## expressions strings.
#
# app_pools:
# - <APP_POOL_1>
# - <APP_POOL_2>
# include:
# - <PATTERN_1>
# - <PATTERN_2>
# exclude:
# - <PATTERN_1>
# - <PATTERN_2>

## @param server - string - optional
## The server with which to connect, defaulting to the local machine.
Expand Down
88 changes: 88 additions & 0 deletions iis/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,91 @@ def test_check_specific(aggregator, dd_default_hostname, dd_run_check, mock_perf
)

aggregator.assert_all_metrics_covered()


def test_check_include_patterns(aggregator, dd_default_hostname, dd_run_check, mock_performance_objects):
mock_performance_objects(PERFORMANCE_OBJECTS)
check = IIS(
'iis',
{},
[{'host': dd_default_hostname, 'app_pools': {'include': ['^foo']}, 'sites': {'include': ['^foo']}}],
)
check.hostname = dd_default_hostname
dd_run_check(check)

global_tags = ['iis_host:{}'.format(dd_default_hostname)]
aggregator.assert_service_check('iis.windows.perf.health', ServiceCheck.OK, count=1, tags=global_tags)

app_pool_metrics_data, site_metrics_data = get_metrics_data()

for app_pool, value in (('foo-pool', 9000),):
tags = ['app_pool:{}'.format(app_pool)]
tags.extend(global_tags)
aggregator.assert_service_check(
'iis.app_pool_up', ServiceCheck.CRITICAL if value == 0 else ServiceCheck.OK, count=1, tags=tags
)

for metric_name, metric_type in app_pool_metrics_data:
aggregator.assert_metric_has_tag(metric_name, 'app_pool:bar-pool', count=0)
aggregator.assert_metric(
metric_name, value, metric_type=getattr(aggregator, metric_type.upper()), count=1, tags=tags
)

for site, value in (('foo.site', 9000),):
tags = ['site:{}'.format(site)]
tags.extend(global_tags)
aggregator.assert_service_check(
'iis.site_up', ServiceCheck.CRITICAL if value == 0 else ServiceCheck.OK, count=1, tags=tags
)

for metric_name, metric_type in site_metrics_data:
aggregator.assert_metric_has_tag(metric_name, 'site:bar.site', count=0)
aggregator.assert_metric(
metric_name, value, metric_type=getattr(aggregator, metric_type.upper()), count=1, tags=tags
)

aggregator.assert_all_metrics_covered()


def test_check_exclude_patterns(aggregator, dd_default_hostname, dd_run_check, mock_performance_objects):
mock_performance_objects(PERFORMANCE_OBJECTS)
check = IIS(
'iis',
{},
[{'host': dd_default_hostname, 'app_pools': {'exclude': ['^bar']}, 'sites': {'exclude': ['^bar']}}],
)
check.hostname = dd_default_hostname
dd_run_check(check)

global_tags = ['iis_host:{}'.format(dd_default_hostname)]
aggregator.assert_service_check('iis.windows.perf.health', ServiceCheck.OK, count=1, tags=global_tags)

app_pool_metrics_data, site_metrics_data = get_metrics_data()

for app_pool, value in (('foo-pool', 9000),):
tags = ['app_pool:{}'.format(app_pool)]
tags.extend(global_tags)
aggregator.assert_service_check(
'iis.app_pool_up', ServiceCheck.CRITICAL if value == 0 else ServiceCheck.OK, count=1, tags=tags
)

for metric_name, metric_type in app_pool_metrics_data:
aggregator.assert_metric_has_tag(metric_name, 'app_pool:bar-pool', count=0)
aggregator.assert_metric(
metric_name, value, metric_type=getattr(aggregator, metric_type.upper()), count=1, tags=tags
)

for site, value in (('foo.site', 9000),):
tags = ['site:{}'.format(site)]
tags.extend(global_tags)
aggregator.assert_service_check(
'iis.site_up', ServiceCheck.CRITICAL if value == 0 else ServiceCheck.OK, count=1, tags=tags
)

for metric_name, metric_type in site_metrics_data:
aggregator.assert_metric_has_tag(metric_name, 'site:bar.site', count=0)
aggregator.assert_metric(
metric_name, value, metric_type=getattr(aggregator, metric_type.upper()), count=1, tags=tags
)

aggregator.assert_all_metrics_covered()