1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import logging
1516import psutil
1617
1718from opencensus .metrics .export .gauge import DerivedLongGauge
1819from opencensus .metrics .export .gauge import Registry
1920from opencensus .metrics .export .metric_producer import MetricProducer
2021
22+ logger = logging .getLogger (__name__ )
23+
2124
2225# Namespaces used in Azure Monitor
2326AVAILABLE_MEMORY = "\\ Memory\\ Available Bytes"
27+ PRIVATE_BYTES = "\\ Process(??APP_WIN32_PROC??)\\ Private Bytes"
2428
2529
2630def get_available_memory ():
@@ -31,7 +35,7 @@ def get_available_memory_metric():
3135 """ Returns a derived gauge for available memory
3236
3337 Available memory is defined as memory that can be given instantly to
34- processes without the system going into swap
38+ processes without the system going into swap.
3539
3640 :rtype: :class:`opencensus.metrics.export.gauge.DerivedLongGauge`
3741 :return: The gauge representing the available memory metric
@@ -45,6 +49,32 @@ def get_available_memory_metric():
4549 return gauge
4650
4751
52+ def get_process_private_bytes ():
53+ try :
54+ process = psutil .Process ()
55+ return process .memory_info ().rss
56+ except Exception :
57+ logger .exception ('Error handling get process private bytes.' )
58+
59+
60+ def get_process_private_bytes_metric ():
61+ """ Returns a derived gauge for private bytes for the current process
62+
63+ Private bytes for the current process is measured by the Resident Set
64+ Size, which is the non-swapped physical memory a process has used.
65+
66+ :rtype: :class:`opencensus.metrics.export.gauge.DerivedLongGauge`
67+ :return: The gauge representing the private bytes metric
68+ """
69+ gauge = DerivedLongGauge (
70+ PRIVATE_BYTES ,
71+ 'Amount of memory process has used in bytes' ,
72+ 'byte' ,
73+ [])
74+ gauge .create_default_time_series (get_process_private_bytes )
75+ return gauge
76+
77+
4878class AzureStandardMetricsProducer (MetricProducer ):
4979 """Implementation of the producer of standard metrics.
5080
@@ -54,6 +84,7 @@ class AzureStandardMetricsProducer(MetricProducer):
5484 def __init__ (self ):
5585 self .registry = Registry ()
5686 self .registry .add_gauge (get_available_memory_metric ())
87+ self .registry .add_gauge (get_process_private_bytes_metric ())
5788
5889 def get_metrics (self ):
5990 return self .registry .get_metrics ()
0 commit comments