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

Fix batch implementation logic #2265

Merged
merged 2 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions vsphere/datadog_checks/vsphere/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,11 @@ def _process_mor_objects_queue(self, instance):
return

for resource_type in RESOURCE_TYPE_METRICS:
query_specs = []
# Batch size can prevent querying large payloads at once if the environment is too large
# If batch size is set to 0, process everything at once
batch_size = self.batch_morlist_size or self.mor_objects_queue.size(i_key, resource_type)
while self.mor_objects_queue.size(i_key, resource_type):
query_specs = []
for _ in xrange(batch_size):
mor = self.mor_objects_queue.pop(i_key, resource_type)
if mor is None:
Expand Down Expand Up @@ -832,8 +832,8 @@ def collect_metrics(self, instance):
# Request metrics for several objects at once. We can limit the number of objects with batch_size
# If batch_size is 0, process everything at once
batch_size = self.batch_morlist_size or n_mors
query_specs = []
for batch in self.mor_cache.mors_batch(i_key, batch_size):
query_specs = []
for mor_name, mor in batch.iteritems():
if mor['mor_type'] == 'vm':
vm_count += 1
Expand Down
19 changes: 19 additions & 0 deletions vsphere/tests/test_vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def test__cache_morlist_raw_async(vsphere, instance):

def test__process_mor_objects_queue(vsphere, instance):
vsphere.log = MagicMock()
vsphere._process_mor_objects_queue_async = MagicMock()
vsphere._process_mor_objects_queue(instance)
# Queue hasn't been initialized
vsphere.log.debug.assert_called_once_with(
Expand All @@ -248,6 +249,10 @@ def test__process_mor_objects_queue(vsphere, instance):
vsphere._process_mor_objects_queue(instance)
# Object queue should be empty after processing
assert sum(vsphere.mor_objects_queue.size(i_key, resource_type) for resource_type in RESOURCE_TYPE_METRICS) == 0
assert vsphere._process_mor_objects_queue_async.call_count == 2 # Once for each datacenter
for call_args in vsphere._process_mor_objects_queue_async.call_args_list:
# query_specs parameter should be a list of size 1 since the batch size is 1
assert len(call_args[0][1]) == 1


def test_collect_realtime_only(vsphere, instance):
Expand Down Expand Up @@ -343,6 +348,20 @@ def test_format_metric_name(vsphere):
assert vsphere.format_metric_name(counter) == "group.name.{}".format(short_rollup)


def test_collect_metrics(vsphere, instance):
with mock.patch('datadog_checks.vsphere.vsphere.vmodl'):
vsphere.batch_morlist_size = 1
vsphere._collect_metrics_async = MagicMock()
vsphere._cache_metrics_metadata(instance)
vsphere._cache_morlist_raw(instance)
vsphere._process_mor_objects_queue(instance)
vsphere.collect_metrics(instance)
assert vsphere._collect_metrics_async.call_count == 6 # One for each VM/host, datacenters are not collected
for call_args in vsphere._collect_metrics_async.call_args_list:
# query_specs parameter should be a list of size 1 since the batch size is 1
assert len(call_args[0][1]) == 1


def test__collect_metrics_async_compatibility(vsphere, instance):
server_instance = vsphere._get_server_instance(instance)
server_instance.content.perfManager.QueryPerf.return_value = [MagicMock(value=[MagicMock()])]
Expand Down