Skip to content

[#674, #675] Mitigate performance regression by filtering restricted registry collections#680

Merged
csmarchbanks merged 4 commits intoprometheus:masterfrom
pavel-lexyr:restricted_v2
Jul 6, 2021
Merged

[#674, #675] Mitigate performance regression by filtering restricted registry collections#680
csmarchbanks merged 4 commits intoprometheus:masterfrom
pavel-lexyr:restricted_v2

Conversation

@pavel-lexyr
Copy link
Copy Markdown
Contributor

#675 introduces a significant performance regression in restricted registry collecting. This attempts to reduce the regression by re-introducing filter code removed in the previous version.

@pavel-lexyr
Copy link
Copy Markdown
Contributor Author

py2.7 passes locally. What do the logs say on the CI?

@csmarchbanks
Copy link
Copy Markdown
Member

csmarchbanks commented Jul 1, 2021

==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_core.py ______________________
ImportError while importing test module '/home/circleci/project/tests/test_core.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_core.py:5: in <module>
    from unittest.mock import MagicMock
E   ImportError: No module named mock

You can use a skipIf and import the mock only for the test such as done in https://github.com/prometheus/client_python/blob/master/tests/openmetrics/test_parser.py#L492.

Signed-off-by: Pavel <pavel@lexyr.com>
@pavel-lexyr
Copy link
Copy Markdown
Contributor Author

Aha. That worked - thank you!

Copy link
Copy Markdown
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

This approach looks good to me, and I appreciate the test you added. I will give some time for @brian-brazil to review as well.

Comment thread prometheus_client/registry.py Outdated
m = metric._restricted_metric(self._name_set)
if m:
yield m
names = copy.copy(self._name_set)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this copy here? This set shouldn't be changing after the object is instantiated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Trying to do it in the same way collections used to be filtered with target_info. There used to be a set variable that was modified if target_info was found - in order not to change the class field in the same way, we copy it to names.

Comment thread prometheus_client/registry.py Outdated
collectors = set()
with self._registry._lock:
if 'target_info' in names and self._registry._target_info:
yield self._registry._target_info_metric()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think it is safe to yield under the lock.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Good catch!

Signed-off-by: Pavel <pavel@lexyr.com>
Comment thread prometheus_client/registry.py Outdated
with self._registry._lock:
if 'target_info' in names and self._registry._target_info:
yield_target_info = True
names.remove('target_info')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You don't need this, the registry should be preventing this from happening in the first place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure I understood it right - but done.

Comment thread prometheus_client/registry.py Outdated
yield m
names = copy.copy(self._name_set)
collectors = set()
yield_target_info = False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd have this as the thing to yield, rather than a bool for cleanliness. Also avoids a race.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Signed-off-by: Pavel <pavel@lexyr.com>
@brian-brazil
Copy link
Copy Markdown
Contributor

👍

@pavel-lexyr pavel-lexyr requested a review from csmarchbanks July 5, 2021 15:40
Copy link
Copy Markdown
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

Thanks for the additional review and fixes!

@csmarchbanks csmarchbanks merged commit c49e55a into prometheus:master Jul 6, 2021
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.

3 participants