@@ -13,6 +13,8 @@ class Metrics {
13
13
this . funcName = options . funcName ;
14
14
this . namespace = options . namespace || 'default' ;
15
15
this . version = options . version || '$LATEST' ;
16
+ this . apigwServiceId = options . apigwServiceId ;
17
+ this . apigwEnvironment = options . apigwEnvironment ;
16
18
17
19
this . client = new slsMonitor ( this . credentials ) ;
18
20
this . timezone = options . timezone || '+08:00' ;
@@ -22,6 +24,7 @@ class Metrics {
22
24
return Object . freeze ( {
23
25
Base : 1 , // scf base metrics
24
26
Custom : 2 , // report custom metrics
27
+ Apigw : 4 , // apigw metrics
25
28
All : 0xffffffff ,
26
29
} ) ;
27
30
}
@@ -46,6 +49,26 @@ class Metrics {
46
49
}
47
50
}
48
51
52
+ async apigwMetrics ( startTime , endTime , period , serviceId , env ) {
53
+ const rangeTime = {
54
+ rangeStart : startTime ,
55
+ rangeEnd : endTime ,
56
+ } ;
57
+
58
+ try {
59
+ const responses = await this . client . getApigwMetrics (
60
+ this . region ,
61
+ rangeTime ,
62
+ period ,
63
+ serviceId ,
64
+ env ,
65
+ ) ;
66
+ return responses ;
67
+ } catch ( e ) {
68
+ throw new TypeError ( `API_METRICS_getApigwMetrics` , e . message , e . stack ) ;
69
+ }
70
+ }
71
+
49
72
async customMetrics ( startTime , endTime , period ) {
50
73
const rangeTime = {
51
74
rangeStart : startTime ,
@@ -138,9 +161,105 @@ class Metrics {
138
161
results = this . buildCustomMetrics ( results ) ;
139
162
response . metrics = response . metrics . concat ( results ) ;
140
163
}
164
+
165
+ if ( metricsType & Metrics . Type . Apigw ) {
166
+ if ( ! response ) {
167
+ response = {
168
+ rangeStart : startTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
169
+ rangeEnd : endTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
170
+ metrics : [ ] ,
171
+ } ;
172
+ }
173
+
174
+ results = await this . apigwMetrics (
175
+ startTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
176
+ endTime . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
177
+ period ,
178
+ this . apigwServiceId ,
179
+ this . apigwEnvironment ,
180
+ ) ;
181
+
182
+ results = this . buildApigwMetrics ( results ) ;
183
+ response . metrics = response . metrics . concat ( results . metrics ) ;
184
+ if ( results . startTime ) {
185
+ response . rangeStart = results . startTime ;
186
+ }
187
+ if ( results . endTime ) {
188
+ response . rangeEnd = results . endTime ;
189
+ }
190
+ }
141
191
return response ;
142
192
}
143
193
194
+ buildApigwMetrics ( datas ) {
195
+ const responses = {
196
+ startTime : '' ,
197
+ endTime : '' ,
198
+ metrics : [ ] ,
199
+ } ;
200
+
201
+ for ( let i = 0 ; i < datas . length ; i ++ ) {
202
+ const metric = datas [ i ] . Response ;
203
+ if ( metric . Error ) {
204
+ continue ;
205
+ }
206
+ responses . startTime = metric . StartTime ;
207
+ responses . endTime = metric . EndTime ;
208
+
209
+ let type = 'count' ;
210
+ const result = {
211
+ type : 'stacked-bar' ,
212
+ x : {
213
+ type : 'timestamp' ,
214
+ values : metric . DataPoints [ 0 ] . Timestamps . map ( ( ts ) => ts * 1000 ) ,
215
+ } ,
216
+ y : [ ] ,
217
+ } ;
218
+ switch ( metric . MetricName ) {
219
+ case 'NumOfReq' :
220
+ result . title = 'apigw total request num' ;
221
+ break ;
222
+ case 'ResponseTime' :
223
+ type = 'duration' ;
224
+ result . title = 'apigw request response time(ms)' ;
225
+ break ;
226
+ }
227
+
228
+ const item = {
229
+ name : metric . MetricName ,
230
+ type : type ,
231
+ values : metric . DataPoints [ 0 ] . Values ,
232
+ total : metric . DataPoints [ 0 ] . Values . reduce ( function ( a , b ) {
233
+ return a + b ;
234
+ } , 0 ) ,
235
+ } ;
236
+
237
+ if ( ! ( ~ ~ item . total == item . total ) ) {
238
+ item . total = parseFloat ( item . total . toFixed ( 2 ) , 10 ) ;
239
+ }
240
+
241
+ if ( result . x . values . length == 0 ) {
242
+ const startTime = moment ( responses . startTime ) ;
243
+ const endTime = moment ( responses . endTime ) ;
244
+
245
+ i = 0 ;
246
+ while ( startTime <= endTime ) {
247
+ result . x . values [ i ] = startTime . unix ( ) * 1000 ;
248
+ item . values [ i ] = 0 ;
249
+ i ++ ;
250
+ startTime . add ( metric . Period , 's' ) ;
251
+ }
252
+
253
+ item . total = 0 ;
254
+ }
255
+
256
+ result . y . push ( item ) ;
257
+ responses . metrics . push ( result ) ;
258
+ }
259
+
260
+ return responses ;
261
+ }
262
+
144
263
buildMetrics ( datas ) {
145
264
const filterMetricByName = function ( metricName , metrics ) {
146
265
const len = metrics . length ;
0 commit comments