77namespace Prometheus . Client . Collectors
88{
99 /// <summary>
10- /// Collects metrics on standard Performance Counters
10+ /// Collects metrics on standard Performance Counters
1111 /// </summary>
1212 public class PerfCounterCollector : IOnDemandCollector
1313 {
1414 private const string MemCat = ".NET CLR Memory" ;
1515 private const string ProcCat = "Process" ;
16-
16+
1717 private static readonly string [ ] StandardPerfCounters =
1818 {
1919 MemCat , "Gen 0 heap size" ,
@@ -24,75 +24,46 @@ public class PerfCounterCollector : IOnDemandCollector
2424 ProcCat , "% Processor Time" ,
2525 ProcCat , "Private Bytes" ,
2626 ProcCat , "Working Set" ,
27- ProcCat , "Virtual Bytes" ,
27+ ProcCat , "Virtual Bytes"
2828 } ;
2929
30- readonly List < Tuple < Gauge , PerformanceCounter > > _collectors = new List < Tuple < Gauge , PerformanceCounter > > ( ) ;
30+ private readonly List < Tuple < Gauge , PerformanceCounter > > _collectors = new List < Tuple < Gauge , PerformanceCounter > > ( ) ;
3131 private readonly string _instanceName ;
3232 private Counter _perfErrors ;
3333
34- private static bool IsLinux ( )
35- {
36- switch ( Environment . OSVersion . Platform )
37- {
38- case PlatformID . Unix :
39- return true ;
40-
41- default :
42- return false ;
43- }
44- }
45-
34+ /// <summary>
35+ /// Constructor
36+ /// </summary>
4637 public PerfCounterCollector ( )
4738 {
48- Process currentProcess = Process . GetCurrentProcess ( ) ;
39+ var currentProcess = Process . GetCurrentProcess ( ) ;
4940 _instanceName = currentProcess . ProcessName ;
5041 if ( IsLinux ( ) )
51- {
52- //on mono/linux instance name should be pid
5342 _instanceName = currentProcess . Id . ToString ( ) ;
54- }
55- }
56-
57- private void RegisterPerfCounter ( string category , string name )
58- {
59- Gauge gauge = Metrics . CreateGauge ( GetName ( category , name ) , GetHelp ( name ) ) ;
60- _collectors . Add ( Tuple . Create ( gauge , new PerformanceCounter ( category , name , _instanceName ) ) ) ;
61- }
62-
63- private string GetHelp ( string name )
64- {
65- return name + " Perf Counter" ;
66- }
67-
68- private string GetName ( string category , string name )
69- {
70- return ToPromName ( category ) + "_" + ToPromName ( name ) ;
71- }
72-
73- private string ToPromName ( string name )
74- {
75- return name . Replace ( "%" , "pct" ) . Replace ( " " , "_" ) . Replace ( "." , "dot" ) . ToLowerInvariant ( ) ;
7643 }
7744
45+ /// <summary>
46+ /// Register metrics
47+ /// </summary>
7848 public void RegisterMetrics ( )
7949 {
80- for ( int i = 0 ; i < StandardPerfCounters . Length ; i += 2 )
50+ for ( var i = 0 ; i < StandardPerfCounters . Length ; i += 2 )
8151 {
8252 var category = StandardPerfCounters [ i ] ;
8353 var name = StandardPerfCounters [ i + 1 ] ;
8454
8555 RegisterPerfCounter ( category , name ) ;
8656 }
8757
88- _perfErrors = Metrics . CreateCounter ( "performance_counter_errors_total" ,
89- "Total number of errors that occured during performance counter collections" ) ;
58+ _perfErrors = Metrics . CreateCounter ( "performance_counter_errors_total" , "Total number of errors that occured during performance counter collections" ) ;
9059 }
9160
61+ /// <summary>
62+ /// Update metrics
63+ /// </summary>
9264 public void UpdateMetrics ( )
9365 {
9466 foreach ( var collector in _collectors )
95- {
9667 try
9768 {
9869 collector . Item1 . Set ( collector . Item2 . NextValue ( ) ) ;
@@ -101,7 +72,32 @@ public void UpdateMetrics()
10172 {
10273 _perfErrors . Inc ( ) ;
10374 }
104- }
75+ }
76+
77+ private static bool IsLinux ( )
78+ {
79+ return Environment . OSVersion . Platform == PlatformID . Unix ;
80+ }
81+
82+ private void RegisterPerfCounter ( string category , string name )
83+ {
84+ var gauge = Metrics . CreateGauge ( GetName ( category , name ) , GetHelp ( name ) ) ;
85+ _collectors . Add ( Tuple . Create ( gauge , new PerformanceCounter ( category , name , _instanceName ) ) ) ;
86+ }
87+
88+ private static string GetHelp ( string name )
89+ {
90+ return name + " Perf Counter" ;
91+ }
92+
93+ private static string GetName ( string category , string name )
94+ {
95+ return ToPromName ( category ) + "_" + ToPromName ( name ) ;
96+ }
97+
98+ private static string ToPromName ( string name )
99+ {
100+ return name . Replace ( "%" , "pct" ) . Replace ( " " , "_" ) . Replace ( "." , "dot" ) . ToLowerInvariant ( ) ;
105101 }
106102 }
107103}
0 commit comments