@@ -10,15 +10,22 @@ mod linux;
10
10
11
11
#[ derive( Debug ) ]
12
12
pub struct ProcessCollector {
13
- namespace : Option < String > ,
13
+ namespace : String ,
14
14
#[ cfg( target_os = "linux" ) ]
15
15
system : linux:: System ,
16
16
}
17
17
18
18
impl ProcessCollector {
19
19
pub fn new ( namespace : Option < String > ) -> std:: io:: Result < Self > {
20
20
#[ cfg( target_os = "linux" ) ]
21
- let system = linux:: System :: load ( ) ?;
21
+ let system = linux:: System :: load ( namespace. clone ( ) ) ?;
22
+ let namespace = match namespace {
23
+ Some ( mut n) => {
24
+ n. push ( '_' ) ;
25
+ n
26
+ }
27
+ None => "" . to_string ( ) ,
28
+ } ;
22
29
23
30
Ok ( ProcessCollector {
24
31
namespace,
@@ -34,8 +41,9 @@ impl Collector for ProcessCollector {
34
41
. duration_since ( UNIX_EPOCH )
35
42
. map_err ( |_| std:: fmt:: Error ) ?;
36
43
let start_time = ConstGauge :: new ( start_time_from_epoch. as_secs_f64 ( ) ) ;
44
+ let metric_name = format ! ( "{}process_start_time" , self . namespace) ;
37
45
let start_time_metric = encoder. encode_descriptor (
38
- "process_start_time_seconds" ,
46
+ & metric_name ,
39
47
"Start time of the process since unix epoch in seconds." ,
40
48
Some ( & Unit :: Seconds ) ,
41
49
start_time. metric_type ( ) ,
@@ -52,11 +60,178 @@ impl Collector for ProcessCollector {
52
60
#[ cfg( test) ]
53
61
mod tests {
54
62
use super :: * ;
55
- use prometheus_client:: registry:: Registry ;
63
+ use prometheus_client:: { encoding :: text :: encode , registry:: Registry } ;
56
64
57
65
#[ test]
58
- fn register_process_collector ( ) {
66
+ fn register_start_time ( ) {
59
67
let mut registry = Registry :: default ( ) ;
60
- // registry.register_collector(Box::new(ProcessCollector::new(None)))
68
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
69
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
70
+ let mut encoded = String :: new ( ) ;
71
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
72
+
73
+ let start_time = "# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.\n " . to_owned ( ) +
74
+ "# TYPE process_start_time_seconds gauge\n " +
75
+ "# UNIT process_start_time_seconds seconds\n " +
76
+ "process_start_time_seconds " ;
77
+
78
+ assert ! (
79
+ encoded. contains( & start_time) ,
80
+ "encoded does not contain expected start_time"
81
+ ) ;
82
+ }
83
+
84
+ #[ test]
85
+ fn register_resident_memory ( ) {
86
+ let mut registry = Registry :: default ( ) ;
87
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
88
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
89
+ let mut encoded = String :: new ( ) ;
90
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
91
+
92
+ let resident_memory =
93
+ "# HELP process_resident_memory_bytes Resident memory size in bytes.\n " . to_owned ( )
94
+ + "# TYPE process_resident_memory_bytes gauge\n "
95
+ + "# UNIT process_resident_memory_bytes bytes\n "
96
+ + "process_resident_memory_bytes " ;
97
+
98
+ assert ! (
99
+ encoded. contains( & resident_memory) ,
100
+ "encoded does not contain expected resident_memory"
101
+ ) ;
102
+ }
103
+
104
+ #[ test]
105
+ fn register_virtual_memory ( ) {
106
+ let mut registry = Registry :: default ( ) ;
107
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
108
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
109
+ let mut encoded = String :: new ( ) ;
110
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
111
+
112
+ let virtual_memory = "# HELP process_virtual_memory_bytes Virtual memory size in bytes\n "
113
+ . to_owned ( )
114
+ + "# TYPE process_virtual_memory_bytes gauge\n "
115
+ + "# UNIT process_virtual_memory_bytes bytes\n "
116
+ + "process_virtual_memory_bytes " ;
117
+
118
+ assert ! (
119
+ encoded. contains( & virtual_memory) ,
120
+ "encoded does not contain expected virtual_memory"
121
+ ) ;
122
+ }
123
+
124
+ #[ test]
125
+ fn register_virtual_memory_max ( ) {
126
+ let mut registry = Registry :: default ( ) ;
127
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
128
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
129
+ let mut encoded = String :: new ( ) ;
130
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
131
+
132
+ let virtual_memory_max = "# HELP process_virtual_memory_max Maximum amount of virtual memory available in bytes.\n " . to_owned ( ) +
133
+ "# TYPE process_virtual_memory_max gauge\n " +
134
+ "process_virtual_memory_max " ;
135
+
136
+ assert ! (
137
+ encoded. contains( & virtual_memory_max) ,
138
+ "encoded does not contain expected virtual_memory_max"
139
+ ) ;
140
+ }
141
+
142
+ #[ test]
143
+ fn register_open_fds ( ) {
144
+ let mut registry = Registry :: default ( ) ;
145
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
146
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
147
+ let mut encoded = String :: new ( ) ;
148
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
149
+
150
+ let open_fds = "# HELP process_open_fds Number of open file descriptors.\n " . to_owned ( )
151
+ + "# TYPE process_open_fds counter\n "
152
+ + "process_open_fds_total " ;
153
+
154
+ assert ! (
155
+ encoded. contains( & open_fds) ,
156
+ "encoded does not contain expected open_fds"
157
+ ) ;
158
+ }
159
+
160
+ #[ test]
161
+ fn register_max_fds ( ) {
162
+ let mut registry = Registry :: default ( ) ;
163
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
164
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
165
+ let mut encoded = String :: new ( ) ;
166
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
167
+
168
+ let max_fds = "# HELP process_max_fds Maximum number of open file descriptors.\n "
169
+ . to_owned ( )
170
+ + "# TYPE process_max_fds gauge\n "
171
+ + "process_max_fds " ;
172
+
173
+ assert ! (
174
+ encoded. contains( & max_fds) ,
175
+ "encoded does not contain expected max_fds"
176
+ ) ;
177
+ }
178
+
179
+ #[ test]
180
+ fn register_cpu_seconds ( ) {
181
+ let mut registry = Registry :: default ( ) ;
182
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
183
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
184
+ let mut encoded = String :: new ( ) ;
185
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
186
+
187
+ let cpu_seconds =
188
+ "# HELP process_cpu_seconds Total user and system CPU time spent in seconds.\n "
189
+ . to_owned ( )
190
+ + "# TYPE process_cpu_seconds counter\n "
191
+ + "# UNIT process_cpu_seconds seconds\n "
192
+ + "process_cpu_seconds_total " ;
193
+
194
+ assert ! (
195
+ encoded. contains( & cpu_seconds) ,
196
+ "encoded does not contain expected cpu_seconds"
197
+ ) ;
198
+ }
199
+
200
+ #[ test]
201
+ fn register_network_receive ( ) {
202
+ let mut registry = Registry :: default ( ) ;
203
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
204
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
205
+ let mut encoded = String :: new ( ) ;
206
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
207
+
208
+ let network_receive = "# HELP process_network_receive_bytes Number of bytes received by the process over the network.\n " . to_owned ( ) +
209
+ "# TYPE process_network_receive_bytes counter\n " +
210
+ "# UNIT process_network_receive_bytes bytes\n " +
211
+ "process_network_receive_bytes_total " ;
212
+
213
+ assert ! (
214
+ encoded. contains( & network_receive) ,
215
+ "encoded does not contain expected network_receive"
216
+ ) ;
217
+ }
218
+
219
+ #[ test]
220
+ fn register_network_transmit ( ) {
221
+ let mut registry = Registry :: default ( ) ;
222
+ let processor_collector = ProcessCollector :: new ( None ) . unwrap ( ) ;
223
+ registry. register_collector ( Box :: new ( processor_collector) ) ;
224
+ let mut encoded = String :: new ( ) ;
225
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
226
+
227
+ let network_transmit = "# HELP process_network_transmit_bytes Number of bytes sent by the process over the network.\n " . to_owned ( ) +
228
+ "# TYPE process_network_transmit_bytes counter\n " +
229
+ "# UNIT process_network_transmit_bytes bytes\n " +
230
+ "process_network_transmit_bytes_total " ;
231
+
232
+ assert ! (
233
+ encoded. contains( & network_transmit) ,
234
+ "encoded does not contain expected network_transmit"
235
+ ) ;
61
236
}
62
237
}
0 commit comments