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 metric_patterns options to filter all metric submission with a list of regexes #11508

Merged
merged 16 commits into from
Mar 11, 2022

Conversation

sarah-witt
Copy link
Contributor

@sarah-witt sarah-witt commented Feb 15, 2022

What does this PR do?

Adds instance-level options to the base check to filter all metric submission with a regex. The main option is metric_patterns with two sub-options are exclude and include.

Motivation

Allow users to have more control over what metrics are submitted for each integration

Additional Notes

  • These behave very similarly to exclude_labels and include_labels, where exclude takes precedence over include
  • These can either be strings or regexes.
  • They also use the same regex construction as exclude_metrics
  • The namespace is included in the regex
  • I could add a debug log line if a metric is skipped, but that would be noisy

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have changelog/ and integration/ labels attached

@codecov
Copy link

codecov bot commented Feb 15, 2022

Codecov Report

Merging #11508 (02b30f8) into master (31548f9) will increase coverage by 0.26%.
The diff coverage is 98.19%.

Flag Coverage Δ
activemq_xml 82.31% <ø> (ø)
ambari 85.75% <ø> (ø)
avi_vantage 91.92% <ø> (ø)
btrfs 82.91% <ø> (ø)
coredns 94.54% <ø> (ø)
directory 94.87% <ø> (ø)
ibm_i 80.65% <ø> (ø)
kube_proxy 100.00% <ø> (ø)
kube_scheduler 96.20% <ø> (ø)
kubelet 90.42% <ø> (+0.79%) ⬆️
kyototycoon 85.96% <ø> (ø)
linkerd 85.14% <ø> (+1.14%) ⬆️
nagios 89.53% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@sarah-witt sarah-witt marked this pull request as ready for review February 15, 2022 22:24
@sarah-witt sarah-witt requested review from a team as code owners February 15, 2022 22:24
Copy link
Contributor

@vickenty vickenty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing a single regular expression to match a collection of strings might not be the best UX for this. Would it make sense to let the user specify a list of glob patterns instead? (We could still use a compiled regular expression under the hood).

datadog_checks_base/datadog_checks/base/checks/base.py Outdated Show resolved Hide resolved
datadog_checks_base/datadog_checks/base/checks/base.py Outdated Show resolved Hide resolved
@sarah-witt sarah-witt requested a review from vickenty February 16, 2022 20:58
Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We should have both an inclusion and exclusion option
  2. They should be a list of patterns as @vickenty and @fanny-jiang suggested
  3. We should save like this
    if exclude_metrics_patterns:
    self.exclude_metrics_pattern = re.compile('|'.join(exclude_metrics_patterns))
    (see Improve performance of pattern matching in OpenMetrics #5764 (comment))

@sarah-witt
Copy link
Contributor Author

sarah-witt commented Feb 16, 2022

Ah thanks everyone- I'll update the field to be a list and also model the behavior of exclude_labels and include_labels by having both an inclusion and exclusion list as well as compiling everything faster.

@sarah-witt sarah-witt changed the title Add option to filter all metric submission with a regex Add options to filter all metric submission with a list of regexes Mar 2, 2022
@sarah-witt sarah-witt changed the title Add options to filter all metric submission with a list of regexes Add exclude_metrics_filters and include_metrics_filters options to filter all metric submission with a list of regexes Mar 2, 2022
Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

vickenty
vickenty previously approved these changes Mar 10, 2022
Copy link
Contributor

@vickenty vickenty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a small nit / suggestion.

if not isinstance(filters, list):
raise ConfigurationError('Setting `{}` must be an array'.format(option_name))

metrics_pattern = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
metrics_pattern = None

Comment on lines 305 to 307
if metrics_patterns:
metrics_pattern = re.compile('|'.join(metrics_patterns))
return metrics_pattern
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to return the values directly, instead of going through a variable?

Suggested change
if metrics_patterns:
metrics_pattern = re.compile('|'.join(metrics_patterns))
return metrics_pattern
if metrics_patterns:
return re.compile('|'.join(metrics_patterns))
return None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I agree it's a little more readable this way

Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

Last thing, WDYTA naming them (ex|in)clude_metric_patterns?

@sarah-witt sarah-witt changed the title Add exclude_metrics_filters and include_metrics_filters options to filter all metric submission with a list of regexes Add metric_filters options to filter all metric submission with a list of regexes Mar 10, 2022
@sarah-witt
Copy link
Contributor Author

Thanks, updated with the suggestion to have an overall param metric_filters

metric_filters:
  include:
    - ...
  exclude:

@sarah-witt sarah-witt changed the title Add metric_filters options to filter all metric submission with a list of regexes Add metric_patterns options to filter all metric submission with a list of regexes Mar 10, 2022
@sarah-witt sarah-witt merged commit ddcf5dc into master Mar 11, 2022
@sarah-witt sarah-witt deleted the sarah/filter-metrics-regex branch March 11, 2022 19:12
github-actions bot pushed a commit that referenced this pull request Mar 11, 2022
…list of regexes (#11508)

* Add option for global metrics filter

* Filter before context limiter

* Compile regex and check for invalid regex

* Add test for invalid regex

* Add exclude and include filters

* move regex logic down, add more tests

* Remove space

* Add more invalid tests

* Refactor to helper functions, address comments, and add more tests

* Add another test case

* Add test case for include glob

* Remove duplicate logic and further extract

* Don't use metric_pattern veriable

* Add overall metrics_filters param

* Update message and tests

* Rename to metric_patterns ddcf5dc
@CrashLaker
Copy link

hi all. i'm struggling to find how can this be added to the prometheusScrape section in helm values.yaml
can anyone please point me to the right direction/documentation? @fanny-jiang @ofek @sarah-witt
thanks,c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants