1
1
from __future__ import unicode_literals
2
2
3
+ import glob
3
4
import os
4
5
import shutil
6
+ import sys
5
7
import tempfile
6
- import unittest
8
+
9
+ if sys .version_info < (2 , 7 ):
10
+ # We need the skip decorators from unittest2 on Python 2.6.
11
+ import unittest2 as unittest
12
+ else :
13
+ import unittest
14
+
7
15
8
16
from prometheus_client import core
9
17
from prometheus_client .core import (
10
18
CollectorRegistry ,
11
19
Counter ,
12
20
Gauge ,
13
21
Histogram ,
22
+ Sample ,
14
23
Summary ,
15
24
)
16
25
from prometheus_client .multiprocess import (
@@ -25,7 +34,7 @@ def setUp(self):
25
34
os .environ ['prometheus_multiproc_dir' ] = self .tempdir
26
35
core ._ValueClass = core ._MultiProcessValue (lambda : 123 )
27
36
self .registry = CollectorRegistry ()
28
- MultiProcessCollector (self .registry , self .tempdir )
37
+ self . collector = MultiProcessCollector (self .registry , self .tempdir )
29
38
30
39
def tearDown (self ):
31
40
del os .environ ['prometheus_multiproc_dir' ]
@@ -137,6 +146,113 @@ def test_counter_across_forks(self):
137
146
self .assertEqual (3 , self .registry .get_sample_value ('c_total' ))
138
147
self .assertEqual (1 , c1 ._value .get ())
139
148
149
+ @unittest .skipIf (sys .version_info < (2 , 7 ), "Test requires Python 2.7+." )
150
+ def test_collect (self ):
151
+ pid = 0
152
+ core ._ValueClass = core ._MultiProcessValue (lambda : pid )
153
+ labels = dict ((i , i ) for i in 'abcd' )
154
+
155
+ def add_label (key , value ):
156
+ l = labels .copy ()
157
+ l [key ] = value
158
+ return l
159
+
160
+ c = Counter ('c' , 'help' , labelnames = labels .keys (), registry = None )
161
+ g = Gauge ('g' , 'help' , labelnames = labels .keys (), registry = None )
162
+ h = Histogram ('h' , 'help' , labelnames = labels .keys (), registry = None )
163
+
164
+ c .labels (** labels ).inc (1 )
165
+ g .labels (** labels ).set (1 )
166
+ h .labels (** labels ).observe (1 )
167
+
168
+ pid = 1
169
+
170
+ c .labels (** labels ).inc (1 )
171
+ g .labels (** labels ).set (1 )
172
+ h .labels (** labels ).observe (5 )
173
+
174
+ metrics = dict ((m .name , m ) for m in self .collector .collect ())
175
+
176
+ self .assertEqual (
177
+ metrics ['c' ].samples , [Sample ('c_total' , labels , 2.0 )]
178
+ )
179
+ metrics ['g' ].samples .sort (key = lambda x : x [1 ]['pid' ])
180
+ self .assertEqual (metrics ['g' ].samples , [
181
+ Sample ('g' , add_label ('pid' , '0' ), 1.0 ),
182
+ Sample ('g' , add_label ('pid' , '1' ), 1.0 ),
183
+ ])
184
+
185
+ metrics ['h' ].samples .sort (
186
+ key = lambda x : (x [0 ], float (x [1 ].get ('le' , 0 )))
187
+ )
188
+ expected_histogram = [
189
+ Sample ('h_bucket' , add_label ('le' , '0.005' ), 0.0 ),
190
+ Sample ('h_bucket' , add_label ('le' , '0.01' ), 0.0 ),
191
+ Sample ('h_bucket' , add_label ('le' , '0.025' ), 0.0 ),
192
+ Sample ('h_bucket' , add_label ('le' , '0.05' ), 0.0 ),
193
+ Sample ('h_bucket' , add_label ('le' , '0.075' ), 0.0 ),
194
+ Sample ('h_bucket' , add_label ('le' , '0.1' ), 0.0 ),
195
+ Sample ('h_bucket' , add_label ('le' , '0.25' ), 0.0 ),
196
+ Sample ('h_bucket' , add_label ('le' , '0.5' ), 0.0 ),
197
+ Sample ('h_bucket' , add_label ('le' , '0.75' ), 0.0 ),
198
+ Sample ('h_bucket' , add_label ('le' , '1.0' ), 1.0 ),
199
+ Sample ('h_bucket' , add_label ('le' , '2.5' ), 1.0 ),
200
+ Sample ('h_bucket' , add_label ('le' , '5.0' ), 2.0 ),
201
+ Sample ('h_bucket' , add_label ('le' , '7.5' ), 2.0 ),
202
+ Sample ('h_bucket' , add_label ('le' , '10.0' ), 2.0 ),
203
+ Sample ('h_bucket' , add_label ('le' , '+Inf' ), 2.0 ),
204
+ Sample ('h_count' , labels , 2.0 ),
205
+ Sample ('h_sum' , labels , 6.0 ),
206
+ ]
207
+
208
+ self .assertEqual (metrics ['h' ].samples , expected_histogram )
209
+
210
+ @unittest .skipIf (sys .version_info < (2 , 7 ), "Test requires Python 2.7+." )
211
+ def test_merge_no_accumulate (self ):
212
+ pid = 0
213
+ core ._ValueClass = core ._MultiProcessValue (lambda : pid )
214
+ labels = dict ((i , i ) for i in 'abcd' )
215
+
216
+ def add_label (key , value ):
217
+ l = labels .copy ()
218
+ l [key ] = value
219
+ return l
220
+
221
+ h = Histogram ('h' , 'help' , labelnames = labels .keys (), registry = None )
222
+ h .labels (** labels ).observe (1 )
223
+ pid = 1
224
+ h .labels (** labels ).observe (5 )
225
+
226
+ path = os .path .join (os .environ ['prometheus_multiproc_dir' ], '*.db' )
227
+ files = glob .glob (path )
228
+ metrics = dict (
229
+ (m .name , m ) for m in self .collector .merge (files , accumulate = False )
230
+ )
231
+
232
+ metrics ['h' ].samples .sort (
233
+ key = lambda x : (x [0 ], float (x [1 ].get ('le' , 0 )))
234
+ )
235
+ expected_histogram = [
236
+ Sample ('h_bucket' , add_label ('le' , '0.005' ), 0.0 ),
237
+ Sample ('h_bucket' , add_label ('le' , '0.01' ), 0.0 ),
238
+ Sample ('h_bucket' , add_label ('le' , '0.025' ), 0.0 ),
239
+ Sample ('h_bucket' , add_label ('le' , '0.05' ), 0.0 ),
240
+ Sample ('h_bucket' , add_label ('le' , '0.075' ), 0.0 ),
241
+ Sample ('h_bucket' , add_label ('le' , '0.1' ), 0.0 ),
242
+ Sample ('h_bucket' , add_label ('le' , '0.25' ), 0.0 ),
243
+ Sample ('h_bucket' , add_label ('le' , '0.5' ), 0.0 ),
244
+ Sample ('h_bucket' , add_label ('le' , '0.75' ), 0.0 ),
245
+ Sample ('h_bucket' , add_label ('le' , '1.0' ), 1.0 ),
246
+ Sample ('h_bucket' , add_label ('le' , '2.5' ), 0.0 ),
247
+ Sample ('h_bucket' , add_label ('le' , '5.0' ), 1.0 ),
248
+ Sample ('h_bucket' , add_label ('le' , '7.5' ), 0.0 ),
249
+ Sample ('h_bucket' , add_label ('le' , '10.0' ), 0.0 ),
250
+ Sample ('h_bucket' , add_label ('le' , '+Inf' ), 0.0 ),
251
+ Sample ('h_sum' , labels , 6.0 ),
252
+ ]
253
+
254
+ self .assertEqual (metrics ['h' ].samples , expected_histogram )
255
+
140
256
141
257
class TestMmapedDict (unittest .TestCase ):
142
258
def setUp (self ):
0 commit comments