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 regex matching to resource attribute filters #535

Merged
merged 1 commit into from Nov 22, 2022

Conversation

damemi
Copy link
Member

@damemi damemi commented Nov 21, 2022

Fixes #447 by adding a Regex field to resource filters. There are no restrictions on this field, so for example it is ok to set Prefix and Regex. The code will simply do an OR check if one of them matches.

Copy link
Contributor

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

I would personally expect AND behavior if I put two conditions together, and OR behavior if I put two conditions separately.

exporter/collector/monitoredresource.go Outdated Show resolved Hide resolved
@dashpole
Copy link
Contributor

But then again, a regex is strictly more expressive than a prefix, so its probably fine to take either approach. Or we could only allow one of [Prefix, regex] and leave our options open for the future

Copy link
Contributor

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

I'll leave the decision up to you.

@damemi
Copy link
Member Author

damemi commented Nov 21, 2022

I like it. To your point about expressiveness, I can't think of a case where someone would want to AND a prefix+regex that couldn't be expressed in one regex. But I could see the separation making configs a little more readable, if someone chooses to do that.

Made the change, please double check my logic makes sense.

exporter/collector/monitoredresource.go Outdated Show resolved Hide resolved
Comment on lines 84 to 86
matchesRegex = matched
}
if strings.HasPrefix(k, resourceFilter.Prefix) && matchesRegex {
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
matchesRegex = matched
}
if strings.HasPrefix(k, resourceFilter.Prefix) && matchesRegex {
if !matched {
continue
}
}
if strings.HasPrefix(k, resourceFilter.Prefix) {

Seems simpler not to track matchesRegex separately

Copy link
Member Author

Choose a reason for hiding this comment

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

Taking out the error handling made it easier for me to just do both checks in the if. I had trouble reusing the matched variable from the regexp call because I was declaring a new err on the same line. It's better now

@dashpole dashpole self-requested a review November 21, 2022 20:52
@damemi damemi merged commit 97b3a35 into GoogleCloudPlatform:main Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Collector Exporter: Support regex matching for metric resource_filters
2 participants