@@ -21,33 +21,58 @@ const chai = require('chai');
21
21
const should = chai . should ( ) ;
22
22
const sinon = require ( 'sinon' ) ;
23
23
24
+ class FakeQueryClient {
25
+ static getByEncodedUrlCount = 0 ;
26
+
27
+ static reset ( ) {
28
+ FakeQueryClient . getByEncodedUrlCount = 0 ;
29
+ }
30
+
31
+ static setGetByEncodedUrlResponse ( ableToConnect , response ) {
32
+ FakeQueryClient . ableToConnect = ableToConnect ;
33
+ FakeQueryClient . response = response ;
34
+ }
35
+
36
+ async getByEncodedUrl ( ) {
37
+ FakeQueryClient . getByEncodedUrlCount ++ ;
38
+ if ( ! FakeQueryClient . ableToConnect ) {
39
+ throw new Error ( 'ECONNREFUSED' ) ;
40
+ }
41
+ return FakeQueryClient . response ;
42
+ }
43
+ }
44
+
24
45
describe ( 'Prometheus monitor implementation' , ( ) => {
25
46
26
- const fakeQueryClient = sinon . stub ( ) ;
27
- PrometheusMonitorRewire . __set__ ( 'PrometheusQueryClient' , fakeQueryClient ) ;
47
+ PrometheusMonitorRewire . __set__ ( 'PrometheusQueryClient' , FakeQueryClient ) ;
28
48
29
49
// Before/After
30
50
let clock ;
31
- beforeEach ( ( ) => {
51
+ beforeEach ( ( ) => {
32
52
clock = sinon . useFakeTimers ( ) ;
33
53
} ) ;
34
54
35
- afterEach ( ( ) => {
55
+ afterEach ( ( ) => {
36
56
clock . restore ( ) ;
37
57
} ) ;
38
58
39
59
// Test data
40
60
const monitorOptions = {
41
61
metrics : {
42
- include : [ 'peer' , 'pushgateway' , 'dev.*' ] ,
62
+ url : 'http://localhost:9090' ,
63
+ include : [ 'peer' , 'orderer' , 'dev.*' ] ,
43
64
queries : [
44
65
{
66
+ name : 'avg cpu' ,
67
+ label : 'name' ,
45
68
query : 'sum(rate(container_cpu_usage_seconds_total{name=~".+"}[$interval])) by (name) * 100' ,
46
- statistic : 'average '
69
+ statistic : 'avg '
47
70
} ,
48
71
{
72
+ name : 'max cpu' ,
73
+ label : 'name' ,
49
74
query : 'sum(rate(container_cpu_usage_seconds_total{name=~".+"}[$interval])) by (name) * 100' ,
50
- statistic : 'maximum '
75
+ statistic : 'max '
51
76
}
52
77
]
53
78
}
@@ -78,7 +103,7 @@ describe('Prometheus monitor implementation', () => {
78
103
} ) ;
79
104
} ) ;
80
105
81
- describe ( '#getQueryClient' , ( ) => {
106
+ describe ( '#getQueryClient' , ( ) => {
82
107
83
108
it ( 'should return the internal Query Client' , ( ) => {
84
109
const mon = new PrometheusMonitorRewire ( { } ) ;
@@ -92,7 +117,7 @@ describe('Prometheus monitor implementation', () => {
92
117
93
118
it ( 'should set the start time with the current time' , ( ) => {
94
119
clock . tick ( 42 ) ;
95
- const mon = new PrometheusMonitorRewire ( { push_url : '123' } ) ;
120
+ const mon = new PrometheusMonitorRewire ( { push_url : '123' } ) ;
96
121
mon . start ( ) ;
97
122
mon . startTime . should . equal ( 0.042 ) ;
98
123
} ) ;
@@ -102,7 +127,7 @@ describe('Prometheus monitor implementation', () => {
102
127
describe ( '#stop' , ( ) => {
103
128
it ( 'should remove startTime if it exists' , ( ) => {
104
129
clock . tick ( 42 ) ;
105
- const mon = new PrometheusMonitorRewire ( { push_url : '123' } ) ;
130
+ const mon = new PrometheusMonitorRewire ( { push_url : '123' } ) ;
106
131
mon . start ( ) ;
107
132
mon . startTime . should . equal ( 0.042 ) ;
108
133
mon . stop ( ) ;
@@ -114,7 +139,7 @@ describe('Prometheus monitor implementation', () => {
114
139
115
140
it ( 'should reset the start time' , ( ) => {
116
141
clock . tick ( 42 ) ;
117
- const mon = new PrometheusMonitorRewire ( { push_url : '123' } ) ;
142
+ const mon = new PrometheusMonitorRewire ( { push_url : '123' } ) ;
118
143
mon . start ( ) ;
119
144
clock . tick ( 42 ) ;
120
145
mon . restart ( ) ;
@@ -149,15 +174,83 @@ describe('Prometheus monitor implementation', () => {
149
174
const mon = new PrometheusMonitorRewire ( monitorOptions ) ;
150
175
mon . includeStatistic ( 'peer0.org0.example.com' ) . should . equal ( true ) ;
151
176
152
- mon . includeStatistic ( 'pushgateway' ) . should . equal ( true ) ;
177
+ mon . includeStatistic ( 'pushgateway' ) . should . equal ( false ) ;
153
178
154
179
mon . includeStatistic ( 'dev-org0.example.com' ) . should . equal ( true ) ;
155
180
156
181
mon . includeStatistic ( 'penuin' ) . should . equal ( false ) ;
157
182
158
- mon . includeStatistic ( 'orderer0.example.com' ) . should . equal ( false ) ;
183
+ mon . includeStatistic ( 'orderer0.example.com' ) . should . equal ( true ) ;
159
184
} ) ;
160
185
} ) ;
161
186
187
+ describe ( 'When getting statistics' , ( ) => {
188
+ const response = {
189
+ status : 'success' ,
190
+ data : {
191
+ resultType : 'matrix' ,
192
+ result : [
193
+ {
194
+ metric : {
195
+ name : 'orderer.example.com'
196
+ } ,
197
+ values : [
198
+ [
199
+ 1648125000.736 ,
200
+ '37318656'
201
+ ] ,
202
+ [
203
+ 1648125010.736 ,
204
+ '37318656'
205
+ ] ,
206
+ [
207
+ 1648125020.736 ,
208
+ '37318656'
209
+ ]
210
+ ]
211
+ } ,
212
+ {
213
+ metric : {
214
+ name : 'peer0.org1.example.com'
215
+ } ,
216
+ values : [
217
+ [
218
+ 1648125000.736 ,
219
+ '80855040'
220
+ ] ,
221
+ [
222
+ 1648125010.736 ,
223
+ '80855040'
224
+ ] ,
225
+ [
226
+ 1648125020.736 ,
227
+ '80855040'
228
+ ]
229
+ ]
230
+ }
231
+ ]
232
+ }
233
+ } ;
234
+
235
+ it ( 'should stop processing further queries and return an empty set of results if it fails to connect to prometheus ' , async ( ) => {
236
+ const prometheusMonitor = new PrometheusMonitorRewire ( monitorOptions ) ;
237
+ FakeQueryClient . reset ( ) ;
238
+ FakeQueryClient . setGetByEncodedUrlResponse ( false ) ;
239
+ const res = await prometheusMonitor . getStatistics ( ) ;
240
+ FakeQueryClient . getByEncodedUrlCount . should . equal ( 1 ) ;
241
+ res . resourceStats . length . should . equal ( 0 ) ;
242
+ res . chartStats . length . should . equal ( 0 ) ;
243
+ } ) ;
244
+
245
+ it ( 'should process all queries successfully if able to connect to prometheus' , async ( ) => {
246
+ const prometheusMonitor = new PrometheusMonitorRewire ( monitorOptions ) ;
247
+ FakeQueryClient . reset ( ) ;
248
+ FakeQueryClient . setGetByEncodedUrlResponse ( true , response ) ;
249
+ const res = await prometheusMonitor . getStatistics ( ) ;
250
+ FakeQueryClient . getByEncodedUrlCount . should . equal ( 2 ) ;
251
+ res . resourceStats . length . should . equal ( 4 ) ;
252
+ res . chartStats . length . should . equal ( 0 ) ;
253
+ } ) ;
254
+ } ) ;
162
255
163
256
} ) ;
0 commit comments