@@ -10,22 +10,23 @@ import (
10
10
)
11
11
12
12
type PrometheusProcessMetrics struct {
13
- previousMetrics * ProcessMetrics
14
- cpuGauge prometheus.Gauge
15
- ramGauge prometheus.Gauge
16
- swapGauge prometheus.Gauge
17
- diskReadBytesGauge prometheus.Gauge
18
- diskWriteBytesGauge prometheus.Gauge
19
- diskReadCountGauge prometheus.Gauge
20
- diskWriteCountGauge prometheus.Gauge
13
+ registeredCollectors []prometheus.Collector
14
+ previousMetrics * ProcessMetrics
15
+ cpuGauge prometheus.Gauge
16
+ ramGauge prometheus.Gauge
17
+ swapGauge prometheus.Gauge
18
+ storageReadBytesGauge prometheus.Gauge
19
+ storageWriteBytesGauge prometheus.Gauge
20
+ storagediskReadCountGauge prometheus.Gauge
21
+ storageWriteCountGauge prometheus.Gauge
21
22
}
22
23
23
24
func newPrometheusProcessMetrics (proc ps.Process , descriptiveName , metricNamespace string ) (processMetrics * PrometheusProcessMetrics ) {
24
25
processMetrics = & PrometheusProcessMetrics {}
25
26
binaryName := filepath .Base (proc .Executable ())
26
27
pid := fmt .Sprintf ("%d" , proc .Pid ())
27
28
processMetrics .makeGauges (metricNamespace , pid , binaryName , descriptiveName )
28
- processMetrics .makeDiskGauges (metricNamespace , pid , binaryName , descriptiveName )
29
+ processMetrics .makeStorageGauges (metricNamespace , pid , binaryName , descriptiveName )
29
30
return processMetrics
30
31
}
31
32
@@ -50,81 +51,78 @@ func (pm *PrometheusProcessMetrics) makeGauges(metricNamespace, pid, binaryName,
50
51
})
51
52
}
52
53
53
- func (pm * PrometheusProcessMetrics ) makeDiskGauges (metricNamespace , pid , binaryName , descriptiveName string ) {
54
- pm .diskReadBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
54
+ func (pm * PrometheusProcessMetrics ) makeStorageGauges (metricNamespace , pid , binaryName , descriptiveName string ) {
55
+ pm .storageReadBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
55
56
Namespace : metricNamespace ,
56
- Name : "disk_read_bytes " ,
57
- Help : "Total read from disk (bytes)" ,
57
+ Name : "storage_read_bytes " ,
58
+ Help : "Total read from storage (bytes)" ,
58
59
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
59
60
})
60
- pm .diskWriteBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
61
+ pm .storageWriteBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
61
62
Namespace : metricNamespace ,
62
- Name : "disk_write_bytes " ,
63
- Help : "Total written to disk (bytes)" ,
63
+ Name : "storage_write_bytes " ,
64
+ Help : "Total written to storage (bytes)" ,
64
65
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
65
66
})
66
- pm .diskReadCountGauge = prometheus .NewGauge (prometheus.GaugeOpts {
67
+ pm .storagediskReadCountGauge = prometheus .NewGauge (prometheus.GaugeOpts {
67
68
Namespace : metricNamespace ,
68
- Name : "disk_reads " ,
69
- Help : "Total reads from disk " ,
69
+ Name : "storage_reads " ,
70
+ Help : "Total reads from storage " ,
70
71
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
71
72
})
72
- pm .diskWriteCountGauge = prometheus .NewGauge (prometheus.GaugeOpts {
73
+ pm .storageWriteCountGauge = prometheus .NewGauge (prometheus.GaugeOpts {
73
74
Namespace : metricNamespace ,
74
- Name : "disk_writes " ,
75
- Help : "Total writes to disk " ,
75
+ Name : "storage_writes " ,
76
+ Help : "Total writes to storage " ,
76
77
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
77
78
})
78
79
}
79
80
80
81
func (pm * PrometheusProcessMetrics ) Register () error {
81
- registeredCollectors := make ([]prometheus. Collector , 0 )
82
+ pm . Unregister ( )
82
83
var err error
83
84
for _ , collector := range []prometheus.Collector {
84
85
pm .cpuGauge ,
85
86
pm .ramGauge ,
86
87
pm .swapGauge ,
87
- pm .diskReadBytesGauge ,
88
- pm .diskWriteBytesGauge ,
89
- pm .diskReadCountGauge ,
90
- pm .diskWriteCountGauge ,
88
+ pm .storageReadBytesGauge ,
89
+ pm .storageWriteBytesGauge ,
90
+ pm .storagediskReadCountGauge ,
91
+ pm .storageWriteCountGauge ,
91
92
} {
92
93
err = prometheus .Register (collector )
93
94
if err != nil {
94
95
break
95
96
}
96
- registeredCollectors = append (registeredCollectors , collector )
97
+ pm . registeredCollectors = append (pm . registeredCollectors , collector )
97
98
}
98
99
if err != nil {
99
- for _ , collector := range registeredCollectors {
100
- prometheus .Unregister (collector )
101
- }
100
+ pm .Unregister ()
102
101
}
103
102
return err
104
103
}
105
104
106
105
func (pm * PrometheusProcessMetrics ) Unregister () {
107
- prometheus .Unregister (pm .cpuGauge )
108
- }
109
-
110
- func (pm * PrometheusProcessMetrics ) Update () {
111
- prometheus .Unregister (pm .cpuGauge )
106
+ for _ , collector := range pm .registeredCollectors {
107
+ prometheus .Unregister (collector )
108
+ }
109
+ pm .registeredCollectors = nil
112
110
}
113
111
114
112
func (pm * PrometheusProcessMetrics ) Set (processMetrics * ProcessMetrics ) {
115
113
if pm .previousMetrics != nil {
116
114
deltaTime := processMetrics .cpuSampleTime .Sub (pm .previousMetrics .cpuSampleTime ).Seconds ()
117
115
if deltaTime > 0 {
118
- pm .cpuGauge .Set ((processMetrics .cpuDuration - pm .previousMetrics .cpuDuration ) / deltaTime )
116
+ pm .cpuGauge .Set ((( processMetrics .cpuDuration - pm .previousMetrics .cpuDuration ) * 100 ) / deltaTime )
119
117
} else {
120
118
log .Warn ("deltaTime <= 0" )
121
119
}
122
120
}
123
121
pm .ramGauge .Set (float64 (processMetrics .ram ))
124
122
pm .swapGauge .Set (float64 (processMetrics .swap ))
125
- pm .diskReadBytesGauge .Set (float64 (processMetrics .diskReadBytes ))
126
- pm .diskWriteBytesGauge .Set (float64 (processMetrics .diskWriteBytes ))
127
- pm .diskReadCountGauge .Set (float64 (processMetrics .diskReadCount ))
128
- pm .diskWriteCountGauge .Set (float64 (processMetrics .diskWriteCount ))
123
+ pm .storageReadBytesGauge .Set (float64 (processMetrics .storageReadBytes ))
124
+ pm .storageWriteBytesGauge .Set (float64 (processMetrics .storageWriteBytes ))
125
+ pm .storagediskReadCountGauge .Set (float64 (processMetrics .storageReadCount ))
126
+ pm .storageWriteCountGauge .Set (float64 (processMetrics .storageWriteCount ))
129
127
pm .previousMetrics = processMetrics
130
128
}
0 commit comments