@@ -9,26 +9,24 @@ import (
9
9
)
10
10
11
11
type PrometheusProcessMetrics struct {
12
- lastMetricsSnapshot * ProcessMetrics
13
- cpuGauge prometheus.Gauge
14
- ramGauge prometheus.Gauge
15
- swapGauge prometheus.Gauge
16
- diskReadBytesCounter prometheus.Counter
17
- diskWriteBytesCounter prometheus.Counter
18
- diskReadCountCounter prometheus.Counter
19
- diskWriteCountCounter prometheus.Counter
20
- networkInBytesCounter prometheus.Counter
21
- networkOutBytesCounter prometheus.Counter
12
+ cpuGauge prometheus.Gauge
13
+ ramGauge prometheus.Gauge
14
+ swapGauge prometheus.Gauge
15
+ diskReadBytesGauge prometheus.Gauge
16
+ diskWriteBytesGauge prometheus.Gauge
17
+ diskReadCountGauge prometheus.Gauge
18
+ diskWriteCountGauge prometheus.Gauge
19
+ networkInBytesGauge prometheus.Gauge
20
+ networkOutBytesGauge prometheus.Gauge
22
21
}
23
22
24
23
func newProcessMetrics (proc ps.Process , descriptiveName , metricNamespace string ) (processMetrics * PrometheusProcessMetrics ) {
25
24
processMetrics = & PrometheusProcessMetrics {}
26
- processMetrics .lastMetricsSnapshot = & ProcessMetrics {}
27
25
binaryName := filepath .Base (proc .Executable ())
28
26
pid := fmt .Sprintf ("%d" , proc .Pid ())
29
27
processMetrics .makeGauges (metricNamespace , pid , binaryName , descriptiveName )
30
- processMetrics .makeDiskCounters (metricNamespace , pid , binaryName , descriptiveName )
31
- processMetrics .makeNetworkCounters (metricNamespace , pid , binaryName , descriptiveName )
28
+ processMetrics .makeDiskGauges (metricNamespace , pid , binaryName , descriptiveName )
29
+ processMetrics .makeNetworkGauges (metricNamespace , pid , binaryName , descriptiveName )
32
30
return processMetrics
33
31
}
34
32
@@ -53,41 +51,41 @@ func (pm *PrometheusProcessMetrics) makeGauges(metricNamespace, pid, binaryName,
53
51
})
54
52
}
55
53
56
- func (pm * PrometheusProcessMetrics ) makeDiskCounters (metricNamespace , pid , binaryName , descriptiveName string ) {
57
- pm .diskReadBytesCounter = prometheus .NewCounter (prometheus.CounterOpts {
54
+ func (pm * PrometheusProcessMetrics ) makeDiskGauges (metricNamespace , pid , binaryName , descriptiveName string ) {
55
+ pm .diskReadBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
58
56
Namespace : metricNamespace ,
59
57
Name : "disk_read_bytes" ,
60
58
Help : "Total read from disk (bytes)" ,
61
59
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
62
60
})
63
- pm .diskWriteBytesCounter = prometheus .NewCounter (prometheus.CounterOpts {
61
+ pm .diskWriteBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
64
62
Namespace : metricNamespace ,
65
63
Name : "disk_write_bytes" ,
66
64
Help : "Total written to disk (bytes)" ,
67
65
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
68
66
})
69
- pm .diskReadCountCounter = prometheus .NewCounter (prometheus.CounterOpts {
67
+ pm .diskReadCountGauge = prometheus .NewGauge (prometheus.GaugeOpts {
70
68
Namespace : metricNamespace ,
71
69
Name : "disk_reads" ,
72
70
Help : "Total reads from disk" ,
73
71
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
74
72
})
75
- pm .diskWriteCountCounter = prometheus .NewCounter (prometheus.CounterOpts {
73
+ pm .diskWriteCountGauge = prometheus .NewGauge (prometheus.GaugeOpts {
76
74
Namespace : metricNamespace ,
77
75
Name : "disk_writes" ,
78
76
Help : "Total writes to disk" ,
79
77
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
80
78
})
81
79
}
82
80
83
- func (pm * PrometheusProcessMetrics ) makeNetworkCounters (metricNamespace , pid , binaryName , descriptiveName string ) {
84
- pm .networkInBytesCounter = prometheus .NewCounter (prometheus.CounterOpts {
81
+ func (pm * PrometheusProcessMetrics ) makeNetworkGauges (metricNamespace , pid , binaryName , descriptiveName string ) {
82
+ pm .networkInBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
85
83
Namespace : metricNamespace ,
86
84
Name : "net_read_bytes" ,
87
85
Help : "Total read from disk (bytes)" ,
88
86
ConstLabels : prometheus.Labels {"pid" : pid , "bin" : binaryName , "name" : descriptiveName },
89
87
})
90
- pm .networkOutBytesCounter = prometheus .NewCounter (prometheus.CounterOpts {
88
+ pm .networkOutBytesGauge = prometheus .NewGauge (prometheus.GaugeOpts {
91
89
Namespace : metricNamespace ,
92
90
Name : "net_write_bytes" ,
93
91
Help : "Total written to disk (bytes)" ,
@@ -102,12 +100,12 @@ func (pm *PrometheusProcessMetrics) Register() error {
102
100
pm .cpuGauge ,
103
101
pm .ramGauge ,
104
102
pm .swapGauge ,
105
- pm .diskReadBytesCounter ,
106
- pm .diskWriteBytesCounter ,
107
- pm .diskReadCountCounter ,
108
- pm .diskWriteCountCounter ,
109
- pm .networkInBytesCounter ,
110
- pm .networkOutBytesCounter ,
103
+ pm .diskReadBytesGauge ,
104
+ pm .diskWriteBytesGauge ,
105
+ pm .diskReadCountGauge ,
106
+ pm .diskWriteCountGauge ,
107
+ pm .networkInBytesGauge ,
108
+ pm .networkOutBytesGauge ,
111
109
} {
112
110
err = prometheus .Register (collector )
113
111
if err != nil {
@@ -135,11 +133,10 @@ func (pm *PrometheusProcessMetrics) Set(processMetrics *ProcessMetrics) {
135
133
pm .cpuGauge .Set (processMetrics .cpu )
136
134
pm .ramGauge .Set (float64 (processMetrics .ram ))
137
135
pm .swapGauge .Set (float64 (processMetrics .swap ))
138
- pm .diskReadBytesCounter .Add (float64 (processMetrics .diskReadBytes - pm .lastMetricsSnapshot .diskReadBytes ))
139
- pm .diskWriteBytesCounter .Add (float64 (processMetrics .diskWriteBytes - pm .lastMetricsSnapshot .diskWriteBytes ))
140
- pm .diskReadCountCounter .Add (float64 (processMetrics .diskReadCount - pm .lastMetricsSnapshot .diskReadCount ))
141
- pm .diskWriteCountCounter .Add (float64 (processMetrics .diskWriteCount - pm .lastMetricsSnapshot .diskWriteCount ))
142
- pm .networkInBytesCounter .Add (float64 (processMetrics .networkInBytes - pm .lastMetricsSnapshot .networkInBytes ))
143
- pm .networkOutBytesCounter .Add (float64 (processMetrics .networkOutBytes - pm .lastMetricsSnapshot .networkOutBytes ))
144
- pm .lastMetricsSnapshot = processMetrics
136
+ pm .diskReadBytesGauge .Set (float64 (processMetrics .diskReadBytes ))
137
+ pm .diskWriteBytesGauge .Set (float64 (processMetrics .diskWriteBytes ))
138
+ pm .diskReadCountGauge .Set (float64 (processMetrics .diskReadCount ))
139
+ pm .diskWriteCountGauge .Set (float64 (processMetrics .diskWriteCount ))
140
+ pm .networkInBytesGauge .Set (float64 (processMetrics .networkInBytes ))
141
+ pm .networkOutBytesGauge .Set (float64 (processMetrics .networkOutBytes ))
145
142
}
0 commit comments