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

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

Merged
merged 4 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 14 additions & 4 deletions prometheus_client/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,20 @@ def __init__(self, names, registry):
self._registry = registry

def collect(self):
for metric in self._registry.collect():
m = metric._restricted_metric(self._name_set)
if m:
yield m
names = copy.copy(self._name_set)
Copy link
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
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.

collectors = set()
with self._registry._lock:
if 'target_info' in names and self._registry._target_info:
yield self._registry._target_info_metric()
Copy link
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
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!

names.remove('target_info')
Copy link
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
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.

for name in names:
if name in self._registry._names_to_collectors:
collectors.add(self._registry._names_to_collectors[name])
for collector in collectors:
for metric in collector.collect():
m = metric._restricted_metric(self._name_set)
if m:
yield m


REGISTRY = CollectorRegistry(auto_describe=True)
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

from concurrent.futures import ThreadPoolExecutor
import sys
import time

import pytest
Expand Down Expand Up @@ -838,6 +839,20 @@ def test_target_info_restricted_registry(self):
m.samples = [Sample('target_info', {'foo': 'bar'}, 1)]
self.assertEqual([m], list(registry.restricted_registry(['target_info']).collect()))

@unittest.skipIf(sys.version_info < (3, 3), "Test requires Python 3.3+.")
def test_restricted_registry_does_not_call_extra(self):
from unittest.mock import MagicMock
registry = CollectorRegistry()
mock_collector = MagicMock()
mock_collector.describe.return_value = [Metric('foo', 'help', 'summary')]
registry.register(mock_collector)
Summary('s', 'help', registry=registry).observe(7)

m = Metric('s', 'help', 'summary')
m.samples = [Sample('s_sum', {}, 7)]
self.assertEqual([m], list(registry.restricted_registry(['s_sum']).collect()))
mock_collector.collect.assert_not_called()


if __name__ == '__main__':
unittest.main()