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

Support new implementation of Windows performance counters on Python 3 #10716

Merged
merged 2 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions hyperv/datadog_checks/hyperv/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from datadog_checks.base.checks.windows.perf_counters.base import PerfCountersBaseCheckWithLegacySupport

from .metrics import METRICS_CONFIG


class HypervCheckV2(PerfCountersBaseCheckWithLegacySupport):
__NAMESPACE__ = 'hyperv'

def get_default_config(self):
return {'metrics': METRICS_CONFIG}
10 changes: 10 additions & 0 deletions hyperv/datadog_checks/hyperv/hyperv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from six import PY3

from datadog_checks.base import PDHBaseCheck

from .metrics import DEFAULT_COUNTERS


class HypervCheck(PDHBaseCheck):
def __new__(cls, name, init_config, instances):
if PY3:
from .check import HypervCheckV2

return HypervCheckV2(name, init_config, instances)
else:
return super(HypervCheck, cls).__new__(cls)

def __init__(self, name, init_config, instances=None):
super(HypervCheck, self).__init__(name, init_config, instances=instances, counter_list=DEFAULT_COUNTERS)
52 changes: 52 additions & 0 deletions hyperv/datadog_checks/hyperv/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,55 @@
'gauge',
],
]

METRICS_CONFIG = {
'Hyper-V Dynamic Memory Balancer': {
'name': 'dynamic_memory_balancer',
'counters': [{'Available Memory': 'available_memory', 'Average Pressure': 'average_pressure'}],
},
'Hyper-V Virtual Network Adapter': {
'name': 'virtual_network_adapter',
'counters': [{'Bytes/sec': 'bytes_per_sec'}],
},
'Hyper-V Hypervisor Logical Processor': {
'name': 'hypervisor_logical_processor',
'counters': [
{
'% Guest Run Time': 'guest_run_time',
'% Hypervisor Run Time': 'hypervisor_run_time',
'% Idle Time': 'idle_time',
'% Total Run Time': 'total_run_time',
'Context Switches/sec': 'context_switches_per_sec',
}
],
},
'Hyper-V Hypervisor Root Virtual Processor': {
'name': 'hypervisor_root_virtual_processor',
'counters': [
{
'% Guest Run Time': 'guest_run_time',
'% Hypervisor Run Time': 'hypervisor_run_time',
'% Total Run Time': 'total_run_time',
}
],
},
'Hyper-V Hypervisor Virtual Processor': {
'name': 'hypervisor_virtual_processor',
'counters': [
{
'% Guest Run Time': 'guest_run_time',
'% Hypervisor Run Time': 'hypervisor_run_time',
'% Total Run Time': 'total_run_time',
}
],
},
'Hyper-V VM Vid Partition': {
'name': 'vm_vid_partition',
'counters': [
{
'Physical Pages Allocated': 'physical_pages_allocated',
'Remote Physical Pages': 'remote_physical_pages',
}
],
},
}
29 changes: 26 additions & 3 deletions hyperv/tests/test_hyperv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,44 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
import pytest

from datadog_checks.base.constants import ServiceCheck
from datadog_checks.dev.testing import requires_py2, requires_py3
from datadog_checks.hyperv import HypervCheck
from datadog_checks.hyperv.metrics import DEFAULT_COUNTERS


def test_check(aggregator, instance_refresh, dd_run_check):
@requires_py3
def test_check(aggregator, dd_default_hostname, dd_run_check):
check = HypervCheck('hyperv', {}, [{}])
check.hostname = dd_default_hostname

# Run twice for counters that require 2 data points
dd_run_check(check)
dd_run_check(check)

aggregator.assert_service_check(
'hyperv.windows.perf.health', ServiceCheck.OK, count=2, tags=['server:{}'.format(dd_default_hostname)]
)
_assert_metrics(aggregator)


@requires_py2
def test_check_legacy(aggregator, instance_refresh, dd_run_check):
Copy link
Contributor

@fanny-jiang fanny-jiang Nov 24, 2021

Choose a reason for hiding this comment

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

Somewhat unrelated, I don't see refresh_counters (from instance_refresh) in the pdh_legacy config template. Is this expected? Will the config spec be updated with the new perf_counters template later?

check = HypervCheck('hyperv', {}, [instance_refresh])
dd_run_check(check)

for counter_data in DEFAULT_COUNTERS:
aggregator.assert_metric(counter_data[3])
_assert_metrics(aggregator)


@pytest.mark.e2e
def test_check_e2e(dd_agent_check, instance_refresh):
aggregator = dd_agent_check(instance_refresh)

_assert_metrics(aggregator)


def _assert_metrics(aggregator):
for counter_data in DEFAULT_COUNTERS:
aggregator.assert_metric(counter_data[3])

aggregator.assert_all_metrics_covered()