Skip to content

Commit

Permalink
Use regex in openmetrics v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-mez committed May 21, 2021
1 parent 4bd6b65 commit 23f8539
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,8 @@ def __init__(self, check, config):
# some tags can still generate unwanted metric contexts (e.g pod annotations as tags).
ignore_tags = config.get('ignore_tags', [])
if ignore_tags:
ignored_tag_patterns = set()
for ignored_tag in ignore_tags:
ignored_tag_patterns.add(fnmatch.translate(ignored_tag))

if ignored_tag_patterns:
ignored_tags_re = re.compile('|'.join(ignored_tag_patterns))
custom_tags = [tag for tag in custom_tags if not ignored_tags_re.search(tag)]
ignored_tags_re = re.compile('|'.join(set(ignore_tags)))
custom_tags = [tag for tag in custom_tags if not ignored_tags_re.search(tag)]

# 16 KiB seems optimal, and is also the standard chunk size of the Bittorrent protocol:
# https://www.bittorrent.org/beps/bep_0003.html
Expand Down
48 changes: 44 additions & 4 deletions datadog_checks_base/tests/openmetrics/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,22 +553,62 @@ def test_excluded_metric(self, aggregator, dd_run_check, mock_http_response):


class TestIgnoreTags:
def test(self, aggregator, dd_run_check, mock_http_response):
def test_simple_match(self, aggregator, dd_run_check, mock_http_response):
mock_http_response(
"""
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 6.396288e+06
"""
)
all_tags = ['foo', 'foobar', 'bar', 'bar:baz', 'bar:bar']
ignored_tags = ['foo', 'bar:baz']
wanted_tags = ['bar', 'bar:bar']
check = get_check({'metrics': ['go_memstats_alloc_bytes'], 'tags': all_tags, 'ignore_tags': ignored_tags})
dd_run_check(check)

aggregator.assert_metric(
'test.go_memstats_alloc_bytes', 6396288, metric_type=aggregator.GAUGE, tags=['endpoint:test'] + wanted_tags
)

aggregator.assert_all_metrics_covered()

def test_simple_wildcard(self, aggregator, dd_run_check, mock_http_response):
mock_http_response(
"""
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 6.396288e+06
"""
)
all_tags = ['foo', 'foobar', 'bar', 'bar:baz', 'bar:bar']
ignored_tags = ['foo*', '.*baz']
wanted_tags = ['bar', 'bar:bar']
check = get_check({'metrics': ['go_memstats_alloc_bytes'], 'tags': all_tags, 'ignore_tags': ignored_tags})
dd_run_check(check)

aggregator.assert_metric(
'test.go_memstats_alloc_bytes', 6396288, metric_type=aggregator.GAUGE, tags=['endpoint:test'] + wanted_tags
)

aggregator.assert_all_metrics_covered()

all_tags = ['foo', 'foofoo', 'bar', 'bar:baz']
ignored_tags = ['foo*', 'bar:baz']
def test_regex(self, aggregator, dd_run_check, mock_http_response):
mock_http_response(
"""
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 6.396288e+06
"""
)
all_tags = ['foo', 'foobar', 'bar:baz', 'bar:bar']
ignored_tags = ['^foo', '.+:bar$']
wanted_tags = ['bar:baz']
check = get_check({'metrics': ['go_memstats_alloc_bytes'], 'tags': all_tags, 'ignore_tags': ignored_tags})
dd_run_check(check)

aggregator.assert_metric(
'test.go_memstats_alloc_bytes', 6396288, metric_type=aggregator.GAUGE, tags=['endpoint:test', 'bar']
'test.go_memstats_alloc_bytes', 6396288, metric_type=aggregator.GAUGE, tags=['endpoint:test'] + wanted_tags
)

aggregator.assert_all_metrics_covered()
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def instance_ignore_metrics_by_labels(field, value):
return get_default_field_value(field, value)


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


def instance_kerberos_auth(field, value):
return 'disabled'

Expand Down Expand Up @@ -124,10 +128,18 @@ def instance_log_requests(field, value):
return False


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


def instance_min_collection_interval(field, value):
return 15


def instance_namespace(field, value):
return 'service'


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import annotations

from typing import Any, Mapping, Optional, Sequence
from typing import Any, Mapping, Optional, Sequence, Union

from pydantic import BaseModel, root_validator, validator

Expand Down Expand Up @@ -73,6 +73,7 @@ class Config:
health_service_check: Optional[bool]
ignore_metrics: Optional[Sequence[str]]
ignore_metrics_by_labels: Optional[IgnoreMetricsByLabels]
ignore_tags: Optional[Sequence[str]]
kerberos_auth: Optional[str]
kerberos_cache: Optional[str]
kerberos_delegate: Optional[bool]
Expand All @@ -84,9 +85,9 @@ class Config:
label_to_hostname: Optional[str]
labels_mapper: Optional[Mapping[str, Any]]
log_requests: Optional[bool]
metrics: Sequence[str]
metrics: Optional[Sequence[Union[str, Mapping[str, str]]]]
min_collection_interval: Optional[float]
namespace: str
namespace: Optional[str]
ntlm_domain: Optional[str]
password: Optional[str]
persist_connections: Optional[bool]
Expand Down

0 comments on commit 23f8539

Please sign in to comment.