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

Commit 7876105

Browse files
lzchenreyang
authored andcommitted
Refactor Standard Metrics (#724)
1 parent ff4c792 commit 7876105

File tree

7 files changed

+260
-187
lines changed

7 files changed

+260
-187
lines changed

contrib/opencensus-ext-azure/README.rst

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

154-
- Available memory (bytes)
154+
- Available Memory (bytes)
155155
- CPU Processor Time (percentage)
156156
- Process CPU Usage (percentage)
157-
- Process private bytes (bytes)
157+
- Process Private Bytes (bytes)
158158

159159
Trace
160160
~~~~~

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

Lines changed: 0 additions & 160 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2019, OpenCensus Authors
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+
from opencensus.metrics.export.gauge import Registry
16+
from opencensus.metrics.export.metric_producer import MetricProducer
17+
from opencensus.ext.azure.metrics_exporter.standard_metrics.cpu \
18+
import ProcessorTimeMetric
19+
from opencensus.ext.azure.metrics_exporter.standard_metrics.memory \
20+
import AvailableMemoryMetric
21+
from opencensus.ext.azure.metrics_exporter.standard_metrics.process \
22+
import ProcessCPUMetric
23+
from opencensus.ext.azure.metrics_exporter.standard_metrics.process \
24+
import ProcessMemoryMetric
25+
26+
# List of standard metrics to track
27+
STANDARD_METRICS = [AvailableMemoryMetric, ProcessCPUMetric,
28+
ProcessMemoryMetric, ProcessorTimeMetric]
29+
30+
31+
def register_metrics():
32+
registry = Registry()
33+
for standard_metric in STANDARD_METRICS:
34+
metric = standard_metric()
35+
registry.add_gauge(metric())
36+
return registry
37+
38+
39+
class AzureStandardMetricsProducer(MetricProducer):
40+
"""Implementation of the producer of standard metrics.
41+
42+
Includes Azure specific standard metrics, implemented
43+
using gauges.
44+
"""
45+
def __init__(self):
46+
self.registry = register_metrics()
47+
48+
def get_metrics(self):
49+
return self.registry.get_metrics()
50+
51+
52+
producer = AzureStandardMetricsProducer()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2019, OpenCensus Authors
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 psutil
16+
17+
from opencensus.metrics.export.gauge import DerivedDoubleGauge
18+
19+
20+
class ProcessorTimeMetric(object):
21+
NAME = "\\Processor(_Total)\\% Processor Time"
22+
@staticmethod
23+
def get_value():
24+
cpu_times_percent = psutil.cpu_times_percent()
25+
return 100 - cpu_times_percent.idle
26+
27+
def __call__(self):
28+
""" Returns a derived gauge for the processor time.
29+
30+
Processor time is defined as a float representing the current system
31+
wide CPU utilization minus idle CPU time as a percentage. Idle CPU
32+
time is defined as the time spent doing nothing. Return values range
33+
from 0.0 to 100.0 inclusive.
34+
35+
:rtype: :class:`opencensus.metrics.export.gauge.DerivedDoubleGauge`
36+
:return: The gauge representing the processor time metric
37+
"""
38+
gauge = DerivedDoubleGauge(
39+
ProcessorTimeMetric.NAME,
40+
'Processor time as a percentage',
41+
'percentage',
42+
[])
43+
gauge.create_default_time_series(ProcessorTimeMetric.get_value)
44+
# From the psutil docs: the first time this method is called with
45+
# interval = None it will return a meaningless 0.0 value which you are
46+
# supposed to ignore. Call cpu_percent() once so that the subsequent
47+
# calls from the gauge will be meaningful.
48+
psutil.cpu_times_percent()
49+
return gauge
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2019, OpenCensus Authors
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 psutil
16+
17+
from opencensus.metrics.export.gauge import DerivedLongGauge
18+
19+
20+
class AvailableMemoryMetric(object):
21+
NAME = "\\Memory\\Available Bytes"
22+
@staticmethod
23+
def get_value():
24+
return psutil.virtual_memory().available
25+
26+
def __call__(self):
27+
""" Returns a derived gauge for available memory
28+
29+
Available memory is defined as memory that can be given instantly to
30+
processes without the system going into swap.
31+
32+
:rtype: :class:`opencensus.metrics.export.gauge.DerivedLongGauge`
33+
:return: The gauge representing the available memory metric
34+
"""
35+
gauge = DerivedLongGauge(
36+
AvailableMemoryMetric.NAME,
37+
'Amount of available memory in bytes',
38+
'byte',
39+
[])
40+
gauge.create_default_time_series(AvailableMemoryMetric.get_value)
41+
return gauge

0 commit comments

Comments
 (0)