Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit e6a1b9e

Browse files
lzchenreyang
authored andcommitted
Standard Metrics - Processor Time (#720)
1 parent a86e5c8 commit e6a1b9e

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

contrib/opencensus-ext-azure/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ The exporter also includes a set of standard metrics that are exported to Azure
152152
Below is a list of standard metrics that are currently available:
153153

154154
- Available memory (bytes)
155+
- CPU Processor Time (percentage)
156+
- Process private bytes (bytes)
155157

156158
Trace
157159
~~~~~

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/standard_metrics.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616
import psutil
1717

18+
from opencensus.metrics.export.gauge import DerivedDoubleGauge
1819
from opencensus.metrics.export.gauge import DerivedLongGauge
1920
from opencensus.metrics.export.gauge import Registry
2021
from opencensus.metrics.export.metric_producer import MetricProducer
@@ -25,6 +26,7 @@
2526
# Namespaces used in Azure Monitor
2627
AVAILABLE_MEMORY = "\\Memory\\Available Bytes"
2728
PRIVATE_BYTES = "\\Process(??APP_WIN32_PROC??)\\Private Bytes"
29+
PROCESSOR_TIME = "\\Processor(_Total)\\% Processor Time"
2830

2931

3032
def get_available_memory():
@@ -75,6 +77,36 @@ def get_process_private_bytes_metric():
7577
return gauge
7678

7779

80+
def get_processor_time():
81+
cpu_times_percent = psutil.cpu_times_percent()
82+
return 100 - cpu_times_percent.idle
83+
84+
85+
def get_processor_time_metric():
86+
""" Returns a derived gauge for the processor time.
87+
88+
Processor time is defined as a float representing the current system
89+
wide CPU utilization minus idle CPU time as a percentage. Idle CPU
90+
time is defined as the time spent doing nothing. Return values range
91+
from 0.0 to 100.0 inclusive.
92+
93+
:rtype: :class:`opencensus.metrics.export.gauge.DerivedDoubleGauge`
94+
:return: The gauge representing the processor time metric
95+
"""
96+
gauge = DerivedDoubleGauge(
97+
PROCESSOR_TIME,
98+
'Processor time as a percentage',
99+
'percentage',
100+
[])
101+
gauge.create_default_time_series(get_processor_time)
102+
# From the psutil docs: the first time this method is called with interval
103+
# = None it will return a meaningless 0.0 value which you are supposed to
104+
# ignore. Call cpu_percent() once so that the subsequent calls from the
105+
# gauge will be meaningful.
106+
psutil.cpu_times_percent()
107+
return gauge
108+
109+
78110
class AzureStandardMetricsProducer(MetricProducer):
79111
"""Implementation of the producer of standard metrics.
80112
@@ -85,6 +117,7 @@ def __init__(self):
85117
self.registry = Registry()
86118
self.registry.add_gauge(get_available_memory_metric())
87119
self.registry.add_gauge(get_process_private_bytes_metric())
120+
self.registry.add_gauge(get_processor_time_metric())
88121

89122
def get_metrics(self):
90123
return self.registry.get_metrics()

contrib/opencensus-ext-azure/tests/test_azure_standard_metrics.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_producer_get_metrics(self):
3131
producer = standard_metrics.AzureStandardMetricsProducer()
3232
metrics = producer.get_metrics()
3333

34-
self.assertEqual(len(metrics), 2)
34+
self.assertEqual(len(metrics), 3)
3535

3636
def test_get_available_memory_metric(self):
3737
gauge = standard_metrics.get_available_memory_metric()
@@ -73,3 +73,18 @@ def test_get_process_private_bytes_exception(self, logger_mock):
7373
standard_metrics.get_process_private_bytes()
7474

7575
logger_mock.exception.assert_called()
76+
77+
def test_get_processor_time_metric(self):
78+
gauge = standard_metrics.get_processor_time_metric()
79+
80+
self.assertEqual(gauge.descriptor.name,
81+
'\\Processor(_Total)\\% Processor Time')
82+
83+
def test_get_processor_time(self):
84+
with mock.patch('psutil.cpu_times_percent') as processor_mock:
85+
cpu = collections.namedtuple('cpu', 'idle')
86+
cpu_times = cpu(idle=94.5)
87+
processor_mock.return_value = cpu_times
88+
processor_time = standard_metrics.get_processor_time()
89+
90+
self.assertEqual(processor_time, 5.5)

0 commit comments

Comments
 (0)