Skip to content

Commit

Permalink
Support ignoring instance tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-mez committed May 20, 2021
1 parent a443dfa commit 618c4ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,19 @@ def create_scraper_configuration(self, instance=None):
# Custom tags that will be sent with each metric
config['custom_tags'] = instance.get('tags', [])

# Some tags can be ignored to reduce the cardinality.
# This can be useful for cost optimization in containerized environments when the openmetrics check is configured to collect custom metrics.
# Even when the Agent's Tagger is configured to add low-cardinality tags only, some tags can still generate unwanted metric contexts (e.g pod annotations as tags).
ignore_tags = instance.get('ignore_tags', default_instance.get('ignore_tags', []))
if ignore_tags:
ignored_tag_patterns = set()
for ignored_tag in ignore_tags:
ignored_tag_patterns.add(translate(ignored_tag))

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

# Additional tags to be sent with each metric
config['_metric_tags'] = []

Expand Down
16 changes: 16 additions & 0 deletions datadog_checks_base/tests/test_openmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2781,3 +2781,19 @@ def test_empty_namespace(aggregator, mocked_prometheus_check, text_data, ref_gau
check.process_metric(ref_gauge, config)

aggregator.assert_metric('process.vm.bytes', count=1)

def test_ignore_tags(aggregator, mocked_prometheus_check, ref_gauge):
"""
Test that tags matching ignored tag patterns are properly discarded.
"""
check = mocked_prometheus_check
instance = copy.deepcopy(PROMETHEUS_CHECK_INSTANCE)
instance['tags'] = ['foo', 'foofoo', 'bar', 'bar:baz']
instance['ignore_tags'] = ['foo*', 'bar:baz']

config = check.get_scraper_config(instance)
config['_dry_run'] = False

check.process_metric(ref_gauge, config)

aggregator.assert_metric('prometheus.process.vm.bytes', tags=['bar'], count=1)

0 comments on commit 618c4ce

Please sign in to comment.