Skip to content

Commit

Permalink
Merge branch 'master' into hanting-add-allow-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
HantingZhang2 committed Sep 20, 2021
2 parents 87a1fc8 + 8128160 commit 44efcea
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ coverage.xml
.pytest_cache/
.benchmarks/
.junit/
.docker/

# Translations
*.mo
Expand Down
32 changes: 16 additions & 16 deletions process/datadog_checks/process/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def find_pids(self, name, search_string, exact_match, ignore_ad=True):
self.last_ad_cache_ts[name] = time.time()
return matching_pids

def psutil_wrapper(self, process, method, accessors, try_sudo, *args, **kwargs):
def psutil_wrapper(self, process, method, accessors=None, *args, **kwargs):
"""
A psutil wrapper that is calling
* psutil.method(*args, **kwargs) and returns the result
Expand Down Expand Up @@ -264,7 +264,7 @@ def psutil_wrapper(self, process, method, accessors, try_sudo, *args, **kwargs):

return result

def get_process_state(self, name, pids, try_sudo):
def get_process_state(self, name, pids):
st = defaultdict(list)

# Remove from cache the processes that are not in `pids`
Expand Down Expand Up @@ -293,27 +293,27 @@ def get_process_state(self, name, pids, try_sudo):

p = self.process_cache[name][pid]

meminfo = self.psutil_wrapper(p, 'memory_info', ['rss', 'vms'], try_sudo)
meminfo = self.psutil_wrapper(p, 'memory_info', ['rss', 'vms'])
st['rss'].append(meminfo.get('rss'))
st['vms'].append(meminfo.get('vms'))

mem_percent = self.psutil_wrapper(p, 'memory_percent', None, try_sudo)
mem_percent = self.psutil_wrapper(p, 'memory_percent')
st['mem_pct'].append(mem_percent)

# will fail on win32 and solaris
shared_mem = self.psutil_wrapper(p, 'memory_info', ['shared'], try_sudo).get('shared')
shared_mem = self.psutil_wrapper(p, 'memory_info', ['shared']).get('shared')
if shared_mem is not None and meminfo.get('rss') is not None:
st['real'].append(meminfo['rss'] - shared_mem)
else:
st['real'].append(None)

ctxinfo = self.psutil_wrapper(p, 'num_ctx_switches', ['voluntary', 'involuntary'], try_sudo)
ctxinfo = self.psutil_wrapper(p, 'num_ctx_switches', ['voluntary', 'involuntary'])
st['ctx_swtch_vol'].append(ctxinfo.get('voluntary'))
st['ctx_swtch_invol'].append(ctxinfo.get('involuntary'))

st['thr'].append(self.psutil_wrapper(p, 'num_threads', None, try_sudo))
st['thr'].append(self.psutil_wrapper(p, 'num_threads'))

cpu_percent = self.psutil_wrapper(p, 'cpu_percent', None, try_sudo)
cpu_percent = self.psutil_wrapper(p, 'cpu_percent')
cpu_count = psutil.cpu_count()
if not new_process:
# psutil returns `0.` for `cpu_percent` the
Expand All @@ -324,12 +324,10 @@ def get_process_state(self, name, pids, try_sudo):
st['cpu_norm'].append(cpu_percent / cpu_count)
else:
self.log.debug('could not calculate the normalized cpu pct, cpu_count: %s', cpu_count)
st['open_fd'].append(self.psutil_wrapper(p, 'num_fds', None, try_sudo))
st['open_handle'].append(self.psutil_wrapper(p, 'num_handles', None, try_sudo))
st['open_fd'].append(self.psutil_wrapper(p, 'num_fds'))
st['open_handle'].append(self.psutil_wrapper(p, 'num_handles'))

ioinfo = self.psutil_wrapper(
p, 'io_counters', ['read_count', 'write_count', 'read_bytes', 'write_bytes'], try_sudo
)
ioinfo = self.psutil_wrapper(p, 'io_counters', ['read_count', 'write_count', 'read_bytes', 'write_bytes'])
st['r_count'].append(ioinfo.get('read_count'))
st['w_count'].append(ioinfo.get('write_count'))
st['r_bytes'].append(ioinfo.get('read_bytes'))
Expand All @@ -349,7 +347,7 @@ def get_process_state(self, name, pids, try_sudo):
st['cmajflt'].append(None)

# calculate process run time
create_time = self.psutil_wrapper(p, 'create_time', None, try_sudo)
create_time = self.psutil_wrapper(p, 'create_time')
if create_time is not None:
now = time.time()
run_time = now - create_time
Expand Down Expand Up @@ -383,7 +381,7 @@ def _get_child_processes(self, pids):
for child in children:
children_pids.add(child.pid)
except psutil.NoSuchProcess:
pass
self.log.debug("Unable to get children for process because process %s does not exist", pid)

return children_pids

Expand Down Expand Up @@ -441,7 +439,7 @@ def check(self, _):
if self.user:
pids = self._filter_by_user(self.user, pids)

proc_state = self.get_process_state(self.name, pids, self.try_sudo)
proc_state = self.get_process_state(self.name, pids)

# FIXME 8.x remove the `name` tag
tags.extend(['process_name:{}'.format(self.name), self.name])
Expand Down Expand Up @@ -482,6 +480,7 @@ def _get_pid_set(self, pid):
try:
return {psutil.Process(pid).pid}
except psutil.NoSuchProcess:
self.log.debug("Unable to get pid set, process %s does not exist", pid)
return set()

def _process_service_check(self, name, nb_procs, bounds, tags):
Expand Down Expand Up @@ -533,6 +532,7 @@ def _filter_by_user(self, user, pids):
else:
self.log.debug("Discarding pid %s not belonging to %s", pid, user)
except psutil.NoSuchProcess:
self.log.debug("Unable to filter pids by username, process %s does not exist", pid)
pass

return filtered_pids
2 changes: 1 addition & 1 deletion process/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.mark.usefixtures("dd_environment")
def test_check(aggregator, check, dd_run_check):
def test_integration(aggregator, check, dd_run_check):
dd_run_check(check)
for metric in common.EXPECTED_METRICS:
aggregator.assert_metric(metric, tags=common.EXPECTED_TAGS)
Expand Down
10 changes: 5 additions & 5 deletions process/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ def noop_get_pagefault_stats(pid):
def test_psutil_wrapper_simple(aggregator):
# Load check with empty config
process = ProcessCheck(common.CHECK_NAME, {}, [{}])
name = process.psutil_wrapper(get_psutil_proc(), 'name', None, False)
name = process.psutil_wrapper(get_psutil_proc(), 'name')
assert name is not None


def test_psutil_wrapper_simple_fail(aggregator):
# Load check with empty config
process = ProcessCheck(common.CHECK_NAME, {}, [{}])
name = process.psutil_wrapper(get_psutil_proc(), 'blah', None, False)
name = process.psutil_wrapper(get_psutil_proc(), 'blah')
assert name is None


def test_psutil_wrapper_accessors(aggregator):
# Load check with empty config
process = ProcessCheck(common.CHECK_NAME, {}, [{}])
meminfo = process.psutil_wrapper(get_psutil_proc(), 'memory_info', ['rss', 'vms', 'foo'], False)
meminfo = process.psutil_wrapper(get_psutil_proc(), 'memory_info', ['rss', 'vms', 'foo'])
assert 'rss' in meminfo
assert 'vms' in meminfo
assert 'foo' not in meminfo
Expand All @@ -92,7 +92,7 @@ def test_psutil_wrapper_accessors(aggregator):
def test_psutil_wrapper_accessors_fail(aggregator):
# Load check with empty config
process = ProcessCheck(common.CHECK_NAME, {}, [{}])
meminfo = process.psutil_wrapper(get_psutil_proc(), 'memory_infoo', ['rss', 'vms'], False)
meminfo = process.psutil_wrapper(get_psutil_proc(), 'memory_infoo', ['rss', 'vms'])
assert 'rss' not in meminfo
assert 'vms' not in meminfo

Expand Down Expand Up @@ -149,7 +149,7 @@ def mock_find_pid(name, search_string, exact_match=True, ignore_ad=True, refresh
return config_stubs[int(idx)]['mocked_processes']


def mock_psutil_wrapper(process, method, accessors, try_sudo, *args, **kwargs):
def mock_psutil_wrapper(method, accessors):
if method == 'num_handles': # win32 only
return None
if accessors is None:
Expand Down

0 comments on commit 44efcea

Please sign in to comment.