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

Do not compile regexes on each run #3949

Merged
merged 2 commits into from
Jun 20, 2019
Merged
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
25 changes: 18 additions & 7 deletions go_expvar/datadog_checks/go_expvar/go_expvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
class GoExpvar(AgentCheck):
def __init__(self, name, init_config, agentConfig, instances=None):
AgentCheck.__init__(self, name, init_config, agentConfig, instances)
self._regexes = {}
self._last_gc_count = defaultdict(int)

def _get_data(self, url, instance):
Expand Down Expand Up @@ -227,16 +228,26 @@ def deep_get(self, content, keys, traversed_path=None):
return [(traversed_path, content)]

key = keys[0]
regex = "".join(["^", key, "$"])
try:
key_rex = re.compile(regex)
except Exception:
self.warning("Cannot compile regex: %s" % regex)
return []
if key.isalnum():
Copy link
Member

Choose a reason for hiding this comment

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

👌

# key is not a regex, simply match for equality
matcher = key.__eq__
else:
# key might be a regex
key_regex = self._regexes.get(key)
if key_regex is None:
# we don't have it cached, compile it
regex = "^{}$".format(key)
try:
key_regex = re.compile(regex)
except Exception:
self.warning("Cannot compile regex: %s" % regex)
return []
self._regexes[key] = key_regex
matcher = key_regex.match

results = []
for new_key, new_content in self.items(content):
if key_rex.match(new_key):
if matcher(new_key):
results.extend(self.deep_get(new_content, keys[1:], traversed_path + [str(new_key)]))
return results

Expand Down