Skip to content

Commit c3d1c3f

Browse files
authored
Add new cpu metrics to sampler. (#140)
1 parent 4dba56a commit c3d1c3f

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

newrelic/samplers/cpu_usage.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"""
1919

2020
import os
21-
import time
2221

2322
from newrelic.common.system_info import logical_processor_count
2423
from newrelic.common.stopwatch import start_timer
@@ -48,14 +47,25 @@ def __call__(self):
4847
return
4948

5049
new_times = os.times()
50+
elapsed_time = self._timer.restart_timer()
51+
elapsed_cpu_time = elapsed_time*logical_processor_count()
52+
5153
user_time = new_times[0] - self._times[0]
54+
user_utilization = user_time / elapsed_cpu_time
5255

53-
elapsed_time = self._timer.restart_timer()
54-
utilization = user_time / (elapsed_time*logical_processor_count())
56+
system_time = new_times[1] - self._times[1]
57+
system_utilization = system_time / elapsed_cpu_time
58+
59+
total_time = sum(new_times[0:4]) - sum(self._times[0:4])
60+
total_utilization = total_time / elapsed_cpu_time
5561

5662
self._times = new_times
5763

5864
yield ('CPU/User Time', user_time)
59-
yield ('CPU/User/Utilization', utilization)
65+
yield ('CPU/User/Utilization', user_utilization)
66+
yield ('CPU/System Time', system_time)
67+
yield ('CPU/System/Utilization', system_utilization)
68+
yield ('CPU/Total Time', total_time)
69+
yield ('CPU/Total/Utilization', total_utilization)
6070

6171
cpu_usage_data_source = _CPUUsageDataSource
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from newrelic.samplers.cpu_usage import cpu_usage_data_source
17+
18+
19+
@pytest.fixture
20+
def data_source():
21+
sampler = cpu_usage_data_source(settings=())["factory"](environ=())
22+
sampler.start()
23+
yield sampler
24+
sampler.stop()
25+
26+
27+
EXPECTED_METRICS = (
28+
"CPU/User Time",
29+
"CPU/User/Utilization",
30+
"CPU/System Time",
31+
"CPU/System/Utilization",
32+
"CPU/Total Time",
33+
"CPU/Total/Utilization",
34+
)
35+
36+
37+
def test_cpu_metrics_collection(data_source):
38+
metrics_table = set(m[0] for m in (data_source() or ()))
39+
40+
for metric in EXPECTED_METRICS:
41+
assert metric in metrics_table

0 commit comments

Comments
 (0)